PDA

View Full Version : custom events



momesana
29th January 2008, 02:28
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

wysota
29th January 2008, 03:25
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.

seveninches
29th January 2008, 03:43
Here's an example from my kchmviewer to handle custom events:



//! Those events could be sent to main window to do useful things. See handleUserEvents()
class KCHMUserEvent : public QEvent
{
public:
KCHMUserEvent( const QString& action, const QStringList& args = QStringList())
: QEvent( QEvent::User ), m_action(action), m_args(args) {};

QString m_action;
QStringList m_args;
};


class KCHMMainWindow : public QMainWindow, public Ui::MainWindow
{
...
protected:
bool event ( QEvent * e );
};


bool KCHMMainWindow::event( QEvent * e )
{
if ( e->type() == QEvent::User )
return handleUserEvent( (KCHMUserEvent*) e );

return QWidget::event( e );
}

// posting events
qApp->postEvent( this, new KCHMUserEvent( "loadAndOpen", event_args ) );



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

momesana
30th January 2008, 04:15
Thank you both for your input.


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 (http://doc.trolltech.com/latest/qobject.html#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?:(

wysota
30th January 2008, 10:06
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.

momesana
31st January 2008, 00:20
I think it will be simplest to either subclass QApplication (http://doc.trolltech.com/latest/qapplication.html) 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.