Creating Custom Message / Event Using Qt

I have an RPC thread that accesses me from this thread. I need to somehow tell Qt that it needs to make a function call from the main thread. In direct Windows, I could do this using a special message, and then sending this message to the message queue, for example, I could create a WM_CALLFUNCTION message and pass the function pointer through wParam , and the parameter (class pointer) through lParam .

Does anyone know how I can do this with Qt? I came across QCustomEvent , but I have no idea how to use it or how to handle it. Any help would be greatly appreciated!

Edit:

In the end, I went with QMetaObject :: invokeMethod , which works fine.

+4
source share
2 answers

In Qt 3, the usual way to communicate with a GUI thread from a non-GUI thread was sent by a user event to a QObject in the GUI thread. In Qt 4, this still works and can be generalized to the case where one thread should interact with any other thread that has an event loop.

To facilitate programming, Qt 4 also allows you to set up a signal connection slot for streaming. Over scenes, these connections are implemented using an event. If the signal has any parameters, this is also stored in the event. similar to earlier, if the sender and receiver live in the same topic, Qt makes a direct function call.

- http://doc.qt.nokia.com/qq/qq14-threading.html#signalslotconnectionsacrossthreads

+5
source

Using custom events typically involves creating your own QEvent subclass, overriding customEvent () in the QObject class, which will receive the event (often this is the main window class) and some code that "sends" the event from your stream to the receiver.

I like to implement event publishing code as a receiver class method. Thus, the caller should know only about the recevier object, and not about the specifics of Qt. The caller will call this method, which will then essentially send the message to itself. We hope that the code below will become more clear.

 // MainWindow.h ... // Define your custom event identifier const QEvent::Type MY_CUSTOM_EVENT = static_cast<QEvent::Type>(QEvent::User + 1); // Define your custom event subclass class MyCustomEvent : public QEvent { public: MyCustomEvent(const int customData1, const int customData2): QEvent(MY_CUSTOM_EVENT), m_customData1(customData1), m_customData2(customData2) { } int getCustomData1() const { return m_customData1; } int getCustomData2() const { return m_customData2; } private: int m_customData1; int m_customData2; }; public: void postMyCustomEvent(const int customData1, const int customData2); .... protected: void customEvent(QEvent *event); // This overrides QObject::customEvent() ... private: void handleMyCustomEvent(const MyCustomEvent *event); 

customData1 and customData2 should demonstrate how you can pass some data in your event. They should not be int s.

 // MainWindow.cpp ... void MainWindow::postMyCustomEvent(const int customData1, const int customData2) { // This method (postMyCustomEvent) can be called from any thread QApplication::postEvent(this, new MyCustomEvent(customData1, customData2)); } void MainWindow::customEvent(QEvent * event) { // When we get here, we've crossed the thread boundary and are now // executing in the Qt object thread if(event->type() == MY_CUSTOM_EVENT) { handleMyCustomEvent(static_cast<MyCustomEvent *>(event)); } // use more else ifs to handle other custom events } void MainWindow::handleMyCustomEvent(const MyCustomEvent *event) { // Now you can safely do something with your Qt objects. // Access your custom data using event->getCustomData1() etc. } 

I hope I haven’t left anything. With this in place, the code in some other thread just needs to get a pointer to the MainWindow object (call it MainWindow ) and call

 mainWindow->postMyCustomEvent(1,2); 

where, just for our example, 1 and 2 can be any integer data.

+6
source

All Articles