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
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
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.
Here's an example from my kchmviewer to handle custom events:
Qt Code:
//! Those events could be sent to main window to do useful things. See handleUserEvents() { public: QString m_action; QStringList m_args; }; { ... protected: }; { return handleUserEvent( (KCHMUserEvent*) e ); } // posting events 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().
luf (23rd May 2009)
Thank you both for your input.
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.Originally Posted by wysota
@ 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?
That's fine. I only wanted to mark, that you shouldn't post LanguageChange yourself.
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.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?
momesana (30th January 2008)
Bookmarks