Results 1 to 6 of 6

Thread: custom events

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default custom events

    Hi,
    I want to have a simple custom event, just like QEvent::LanguageChange. I want to be able to handle this event in QObject::changeEvent(). What do I need to do to have all my widgets receive this custom event?

    Thanx in advance
    momesana

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: custom events

    LanguageChange is probably not the best example as it is not a custom event but an event sent by Qt to all objects when a new translator is installed on the application and this particular event is handled in QObject::changeEvent.

    Handling real custom events is another thing. All you need to do is to associate a QEvent subclass with an integer (between QEvent::User and QEvent::MaxUser) identifying your event and then simply create QEvent subclass instances that are identified by the number you have chosen. To make an object receive an event, you have to post or send it to the object. If you want all your objects to receive it, you'll need to post or send it to them all or apply an event filter from each of the objects on the application object and send/post the event to the application.

  3. #3
    Join Date
    Jan 2008
    Location
    Silicon valley
    Posts
    15
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: custom events

    Here's an example from my kchmviewer to handle custom events:

    Qt Code:
    1. //! Those events could be sent to main window to do useful things. See handleUserEvents()
    2. class KCHMUserEvent : public QEvent
    3. {
    4. public:
    5. KCHMUserEvent( const QString& action, const QStringList& args = QStringList())
    6. : QEvent( QEvent::User ), m_action(action), m_args(args) {};
    7.  
    8. QString m_action;
    9. QStringList m_args;
    10. };
    11.  
    12.  
    13. class KCHMMainWindow : public QMainWindow, public Ui::MainWindow
    14. {
    15. ...
    16. protected:
    17. bool event ( QEvent * e );
    18. };
    19.  
    20.  
    21. bool KCHMMainWindow::event( QEvent * e )
    22. {
    23. if ( e->type() == QEvent::User )
    24. return handleUserEvent( (KCHMUserEvent*) e );
    25.  
    26. return QWidget::event( e );
    27. }
    28.  
    29. // posting events
    30. qApp->postEvent( this, new KCHMUserEvent( "loadAndOpen", event_args ) );
    To copy to clipboard, switch view to plain text mode 

    Basically you:
    - create your own event class;
    - register the event number in its constructor (QEvent::User, QEvent::User+1, QEvent::User+2 etc);
    - handle the event in event() overriden function;
    - post the events using postEvent().

  4. The following user says thank you to seveninches for this useful post:

    luf (23rd May 2009)

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: custom events

    Thank you both for your input.
    Quote Originally Posted by wysota
    LanguageChange is probably not the best example as it is not a custom event but an event sent by Qt to all objects when a new translator is installed on the application and this particular event is handled in QObject::changeEvent.
    Apart from not being a custom event it exactly resembles what I want to do with my custom event. I want to have a ThemeChangeEvent upon which QWidgets can reload associated icons, background-images etc.

    @ seveninches
    The Qt documentation had already hinted me towards reimplementing QObject::event() but I had hoped that this would not be really necessary. Do I really have to reimplement this member in all classes that are supposed to be able to handle the event?

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: custom events

    Quote Originally Posted by momesana View Post
    Apart from not being a custom event it exactly resembles what I want to do with my custom event. I want to have a ThemeChangeEvent upon which QWidgets can reload associated icons, background-images etc.
    That's fine. I only wanted to mark, that you shouldn't post LanguageChange yourself.

    The Qt documentation had already hinted me towards reimplementing QObject::event() but I had hoped that this would not be really necessary. Do I really have to reimplement this member in all classes that are supposed to be able to handle the event?
    You can instead apply an event filter on every object and handle the event through that event filter, but this will be tedious. I think it will be simplest to either subclass QApplication and do the retheming directly there without propagating the event to widgets or to apply an event filter on the application object and to practically the same, just withouth subclassing.

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

    momesana (30th January 2008)

  8. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: custom events

    Quote Originally Posted by wysota View Post
    I think it will be simplest to either subclass QApplication and do the retheming directly there without propagating the event to widgets or to apply an event filter on the application object and to practically the same, just withouth subclassing.
    Thank you very much. I'll try that.

Similar Threads

  1. Capture events in a custom delegate
    By vfernandez in forum Qt Programming
    Replies: 9
    Last Post: 19th March 2008, 05:33
  2. Custom proxy model issue
    By Khal Drogo in forum Qt Programming
    Replies: 13
    Last Post: 30th November 2007, 12:41
  3. Posting custom events to a subclass of QThread
    By jpn in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2006, 15:49
  4. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.