I want to know - how the event is dispatched to child class if it has same virtual protected event function as that of parent class.
Is virtual protected has significance in event dispatch.Please let me understand.
Thank you..
I want to know - how the event is dispatched to child class if it has same virtual protected event function as that of parent class.
Is virtual protected has significance in event dispatch.Please let me understand.
Thank you..
Its Virtual so that it can be overridden and protected as it is not supposed to be called from outside the class hierarchy.
Check http://qt-project.org/doc/qt-5.0/qtc...on.html#notify to see how event mechanism works.
Primarily its QCoreApplication that delegates the event call to QObject and since QCoreApplication is Friend to QObject, it can call the protected/private methods.
I got why it is protected.But, What is the need of overriding, if the function is already protected.Can you please mention the cases when overriding is useful in the event scenario? because the event object is dispatched to Object directly,hence no pointer and virtualization comes into picture.
Its all about pointers and virtual methods - event() method switches on the event type and calls proper event handler, like this:the event object is dispatched to Object directly,hence no pointer and virtualization comes into picture.
so if you reimplement one of the event handlers in your subclass (or the event() method itself), then your version will be called, because they are virtual.Qt Code:
.... switch (event->type()) { break; break; break; .... }To copy to clipboard, switch view to plain text mode
Actually event() is called on a QObject pointer which points to your derived class's object.
Cheers,
_
blue_sky (20th March 2014)
If you derive from a QObject, directly or through something else that does, then your class inherits all the virtual event handler functions. In your class declaration you can chose to use the 'virtual' keyword or not but the result is the same, the function will be virtual either way. That is:
Some people include "virtual" as an aide-mémoire to those who need to maintain the software later. I have seen claims that the ARM CC compiler issues a warning if the "virtual" is not present in derived classes: cannot vouch for that.Qt Code:
... protected: // is exactly the same as // void childEvent ( QChildEvent * event ); ... };To copy to clipboard, switch view to plain text mode
blue_sky (24th March 2014)
To improve on that use case C++11 adds the override keyword.
It can be put after a method's signature to indicate that it is overriding a base implementation. Has the additional benefit of resulting in a compiler error if it does not, e.g. if the signature is different or virtual is removed from the base class's method declaration.
Since unfortunately not all compilers are capabable of that yet, a macro was added to Qt5 that checks for support and either uses the keyword or expands to nothing: Q_DECL_OVERRIDE
Qt Code:
class MyWidget : public QWdget { QOBJECT protected: };To copy to clipboard, switch view to plain text mode
Cheers,
_
Bookmarks