开发者

Qt: Why is event() declared protected on QWidget

I was working on some Qt code and I needed to propagate a key press event into a child widget (in a QScrollArea). I cannot call keyPressEvent() directly because it is protected, so I figured I would just call event() and let it process the event (note that this is an existing event in the containing scroll area's keyPressEvent() override). The event() method is public and virtual on the base class QObject, but it is protected on QWidget. So, I ended up doing something like this:

((QObject*)(childWidget())->event(e);

That works, and AFAICT is perfectly OK to do in Qt. I could probably also call QCoreApplication::postEvent() or similar, but I would think that would take more overhead (and might even cause an infinite loop, if the event came back from the child to the parent, which would re-post it, etc).

My question is why does Qt make event() protected on the derived class QWidget while it is public on the base class QObject? This is really more a design question than implementation.

As a related question, I know that Qt strives to maintain binary compatibility between releases -- wo开发者_开发百科uld making that method public change binary compatibility? The method is already declared virtual, so I don't think it would change any binary signatures. This is really for my own edification -- I don't have any pull in Qt or anything.


Ordinarily events get propagated from child widgets to parent widgets since child widgets are typically, if not always, in front of their parent. This propagation is done for you internally in the widget code and there is therefore no need to do it manually.

I suspect one reason the method may be protected at the widget level is to make you think twice about trying to propagate the event in reverse order i.e. parent to child. Why do you need to do it the other way around?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜