Results 1 to 9 of 9

Thread: Why event functions in most cases are Protected and Virtual?

  1. #1
    Join Date
    Sep 2013
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Exclamation Why event functions in most cases are Protected and Virtual?

    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..

  2. #2
    Join Date
    Nov 2011
    Location
    India
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Why event functions in most cases are Protected and Virtual?

    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.

  3. #3
    Join Date
    Sep 2013
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: Why event functions in most cases are Protected and Virtual?

    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.
    Quote Originally Posted by vikaspachdha View Post
    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.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Why event functions in most cases are Protected and Virtual?

    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:
    Qt Code:
    1. void QWidget::event(QEvent * event){
    2. ....
    3. switch (event->type()) {
    4. case QEvent::MouseMove:
    5. mouseMoveEvent((QMouseEvent*)event);
    6. break;
    7.  
    8. case QEvent::MouseButtonPress:
    9. mousePressEvent((QMouseEvent*)event);
    10. break;
    11.  
    12. case QEvent::Wheel:
    13. wheelEvent((QWheelEvent*)event);
    14. break;
    15.  
    16. ....
    17. }
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #5
    Join Date
    Sep 2013
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Exclamation Re: Why event functions in most cases are Protected and Virtual?

    So you mean- event handlers are always called by Base class pointer( QWidget* ) holding derived class object ( my own class )?Am I right?
    Quote Originally Posted by stampede View Post
    Its all about pointers and virtual methods - event() method switches on the event type and calls proper event handler, like this:
    Qt Code:
    1. void QWidget::event(QEvent * event){
    2. ....
    3. switch (event->type()) {
    4. case QEvent::MouseMove:
    5. mouseMoveEvent((QMouseEvent*)event);
    6. break;
    7.  
    8. case QEvent::MouseButtonPress:
    9. mousePressEvent((QMouseEvent*)event);
    10. break;
    11.  
    12. case QEvent::Wheel:
    13. wheelEvent((QWheelEvent*)event);
    14. break;
    15.  
    16. ....
    17. }
    To copy to clipboard, switch view to plain text mode 
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why event functions in most cases are Protected and Virtual?

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

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    blue_sky (20th March 2014)

  8. #7
    Join Date
    Sep 2013
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: Why event functions in most cases are Protected and Virtual?

    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?

    Quote Originally Posted by anda_skoa View Post
    Actually event() is called on a QObject pointer which points to your derived class's object.

    Cheers,
    _

  9. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why event functions in most cases are Protected and Virtual?

    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:
    Qt Code:
    1. class Floober: public QObject {
    2. ...
    3. protected:
    4. virtual void childEvent ( QChildEvent * event );
    5. // is exactly the same as
    6. // void childEvent ( QChildEvent * event );
    7. ...
    8. };
    To copy to clipboard, switch view to plain text mode 
    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.

  10. The following user says thank you to ChrisW67 for this useful post:

    blue_sky (24th March 2014)

  11. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why event functions in most cases are Protected and Virtual?

    Quote Originally Posted by ChrisW67 View Post
    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

    Qt Code:
    1. class MyWidget : public QWdget
    2. {
    3. QOBJECT
    4.  
    5. protected:
    6. void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
    7. };
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. C++ virtual functions
    By sonulohani in forum Newbie
    Replies: 3
    Last Post: 23rd May 2012, 09:17
  2. Virtual protected and private var
    By maddog_fr in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2010, 16:14
  3. Problem with virtual functions
    By eekhoorn12 in forum General Programming
    Replies: 4
    Last Post: 8th April 2010, 13:52
  4. QSharedDataPointer and Data with virtual functions
    By niko in forum Qt Programming
    Replies: 0
    Last Post: 2nd February 2010, 08:23
  5. Replies: 10
    Last Post: 12th February 2007, 17:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.