PDA

View Full Version : Why event functions in most cases are Protected and Virtual?



blue_sky
20th March 2014, 09:36
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..

vikaspachdha
20th March 2014, 09:55
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/qtcore/qcoreapplication.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.

blue_sky
20th March 2014, 12:21
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 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/qtcore/qcoreapplication.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.

stampede
20th March 2014, 13:03
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:


void QWidget::event(QEvent * event){
....
switch (event->type()) {
case QEvent::MouseMove:
mouseMoveEvent((QMouseEvent*)event);
break;

case QEvent::MouseButtonPress:
mousePressEvent((QMouseEvent*)event);
break;

case QEvent::Wheel:
wheelEvent((QWheelEvent*)event);
break;

....
}

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.

blue_sky
20th March 2014, 13:20
So you mean- event handlers are always called by Base class pointer( QWidget* ) holding derived class object ( my own class )?Am I right?

Its all about pointers and virtual methods - event() method switches on the event type and calls proper event handler, like this:


void QWidget::event(QEvent * event){
....
switch (event->type()) {
case QEvent::MouseMove:
mouseMoveEvent((QMouseEvent*)event);
break;

case QEvent::MouseButtonPress:
mousePressEvent((QMouseEvent*)event);
break;

case QEvent::Wheel:
wheelEvent((QWheelEvent*)event);
break;

....
}

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.

anda_skoa
20th March 2014, 13:38
Actually event() is called on a QObject pointer which points to your derived class's object.

Cheers,
_

blue_sky
21st March 2014, 06:55
So its all depends on QObject class event functions(Which should be virtual and they are).So what is the need of defining it virtual in my own class?Any idea?


Actually event() is called on a QObject pointer which points to your derived class's object.

Cheers,
_

ChrisW67
21st March 2014, 07:57
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:


class Floober: public QObject {
...
protected:
virtual void childEvent ( QChildEvent * event );
// is exactly the same as
// void childEvent ( QChildEvent * event );
...
};

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.

anda_skoa
21st March 2014, 10:35
Some people include "virtual" as an aide-mémoire to those who need to maintain the software later.

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



class MyWidget : public QWdget
{
QOBJECT

protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
};


Cheers,
_