0

I'm having a QCoreApplication that loads different QtPlugins during startup that are getting data from various sources. This data is pushed back to my QCoreApplication through a registered callback function (equal for each plugin). The callback is registered as follows:

// m_manager is a class variable of the class registering the callback...
pluginInterface->registerCallback(std::bind(&Manager::handleData, std::ref(m_manager), _1));

Now I'm having the following problem: In one of the plugins data is received by a third-party library via network. The data will be pushed to a static member function of my plugin class together with a self-defined struct that also contains the this pointer to the plugin class. When I'm receiving the data in the plugin I now want to push it to my QCoreApplication through the registered callback. Additionally, I want to create new objects (like QTimer) in the callback function. However, it seems like no event loop is running when calling the callback function from the static member function through the this pointer of the plugin class. When looking through the members of the created QTimer object, its QAbstractEventDispatcher pointer is 0. Thus, no signals can be emitted by the QTimer object, which is what I desperately need.

I thought that calling the callback function through the this pointer of the plugin class (where a QEventLoop is definitely running, since I can use signals/slots inside the plugin and from other non-static member functions of the plugin to my QCoreApplication without a problem). However, it seems like I'm wrong. Does anyone have an explanation for the described behavior? If there are any other details needed, just ask for them and I'll try to provide them.

Regards, Robert

Robert
  • 767
  • 8
  • 17
  • Please show how do you perform network requests using the third-party library. More specifically, is this library asynchronous? And why not use Qt network module instead? – Pavel Strakhov Jul 24 '13 at 20:59
  • Do you load the plugins after calling app.exec()? Side note: Signal/slots don't require an event loop as long as the connections are direct (the default). They are just synchronous function calls then. Only queued connections need an event loop. – Frank Osterfeld Jul 24 '13 at 21:47
  • @FrankOsterfeld Yes, the plugins are loaded after the call to app.exec(). – Robert Jul 25 '13 at 09:48
  • @FrankOsterfeld When trying to create and start a QTimer I'm getting this warning "qtimer can only be used with threads started with qthread" and the timeout signal will never be emitted by the QTimer. Both the loaded plugin as well as the Manager class are running in separate threads. The threads are basically both created and started as shown here [QThread](http://supportforums.blackberry.com/t5/Cascades-Development-Knowledge/The-Recommended-Way-to-Use-QThread/ta-p/1803765). The newly created timer in the Manager will run in the context of the plugin thread, but as I said has no valid loop – Robert Jul 25 '13 at 10:07
  • @Riateche I'm not doing the network requests directly myself. This is buried deep down in the third party lib. I'm only calling a function provided by the lib that does all the heavy lifting and calling back a static member function of my class (static void receiveData(void* data, void* callbackData). In the callbackData I'm storing e.g. the this pointer. The library is synchronous. The reason why I'm not using the Qt network lib is that "receiving some data" was oversimplified just for the sake of explanation. The lib is doing tons of specialized work that I don't want to do manually – Robert Jul 25 '13 at 10:20
  • If the library is synchronous and doesn't return control flow to the event loop, it keeps being busy and can't invoke slots. You need to create a thread and perform synchronous operations in that thread. – Pavel Strakhov Jul 25 '13 at 10:25
  • @Riateche Ouch! That one was obvious. Thanks for your help, that was the problem. – Robert Jul 25 '13 at 11:56

1 Answers1

0

If the library is synchronous and doesn't return control flow to the event loop, it keeps being busy and can't invoke slots. You need to create a thread and perform synchronous operations in that thread.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127