6

I would like to code a DLL which should be loaded by a non-QT application. As a result when my DLL is loaded I dont have any QApplication/QCoreApplication. As a result my Signal/Slot mechanism is not working.

I searched deeply in Qt forums but couldnt achieved a good answer yet about how to handle such a problem. I created a QThread moved my QObjects to that thread and created a fake QApplicationCore and called its exec() function inside QThread's run() function. This way my Signal Slot mechanism worked but I am not happy with that indirect solution. I should be able to activate my threads slots from the main non-Qt thread's execution space. What is the correct way of working with such dll plugins? Direct answers and reading source reccomandations are welcomed. Thank you

Note: The external application loading my DLL is a Windows app. It's a third party application and I cannot touch it.

Arpit
  • 6,212
  • 8
  • 38
  • 69
karadaga
  • 61
  • 1
  • 2

1 Answers1

6

Qt signals & slots require an event loop to be running. You have to run an event loop in the thread your QObjects exist. Of course you cannot run it in main thread of your application (as it does not use Qt), so, right, starting a QThread, moving your QObjects to this thread (or create these objects in run() method) and running exec() in thread's run() method is a correct solution.

Archie
  • 2,644
  • 21
  • 21
  • 1
    Dear Archie,Thank you for your response. The interesting point is that calling exec() function of thread is not working. Unless i call exec() function of a fake QApplication nothing works! – karadaga Dec 28 '12 at 14:48
  • 1
    QCoreApplication does a lot of things under the hood (like handling system messages). Some Qt objects also rely on QCoreApplication instance. So, I suppose you have to create QCoreApplication instance anyway on DLL_PROCESS_ATTACH for example. Also take a look at this thread: http://stackoverflow.com/questions/2150488/using-a-qt-based-dll-in-a-non-qt-application – Archie Dec 28 '12 at 15:56
  • 1
    small comment: for direct connections, QCOreApplication isn't necessary, iirc – mlvljr Oct 14 '13 at 07:58