Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way works on Windows is to start <code>QApplication</code> in a separate <code>QThread</code>. It's not portable -- it doesn't work on OS X (I'm researching a fix).</p> <p>But, you don't need a separate thread. If you inject your code into a running application, it already has an event loop. You only need to create a global <code>QApplication</code> object and you're done. The event loop is already running, so you don't need to call <code>exec()</code>. Qt's windows integrate with the native event loop, and everything is good on that front.</p> <p>You <a href="https://stackoverflow.com/q/16540743/1329652">do need</a> to call <code>QCoreApplication::processEvents</code> once. It will integrate the current application instance into the windows event loop, and that's it.</p> <p>Thus, your startup code could look as follows:</p> <pre><code>static struct Data { int argc = 1; char *argv[2] = {strdup("dummy"), {}}; QApplication app{argc, argv}; MainWindow win; } *d; static void startup() { d = new Data; d-&gt;win.show(); d-&gt;app.processEvents(); } static void shutdown() { delete d; } </code></pre> <p>The <code>startup()</code> and <code>shutdown()</code> should be called at appropriate times (on process attach and detach).</p> <hr> <p>Old answer follows. This is not completely up to date anymore.</p> <p>A short example is below, for a complete self-contained example see my <a href="https://stackoverflow.com/a/10847072/1329652">other answer</a>.</p> <p>It is not portable and that's why Qt documentation advises against it. It works just fine on Windows. The main thread is not magic -- not on Windows. Cocoa on OS X is clumsy in a way and makes it impossible, apparently :(.</p> <p>Note that if the application that loads the DLL already uses Qt, then there's nothing further for you to do. Ensure you compile your DLL with the same C++ compiler, link against the same C++ runtime, and use a version of Qt that's binary compatible with the one used by application. You then don't need your own instance of <code>QApplication</code>. To get some useful work done, show a Widget or instantiate some <code>QObjects</code> with timers that will get them busy. You can also use <code>QMetaObject::invokeMethod(obj, "mySlot", Qt::QueuedConnection)</code> instead of using timers: the call will be made when control returns to the event loop.</p> <p>If that's not possible, then the below is your only option. Works just fine, as far as I can tell.</p> <p>Note that I'm a bit sarcastic here: The conditions in the previous paragraph will be met reliably maybe if you are the author of the application that uses the DLL. Otherwise -- forget about it.</p> <pre><code>class AppThread : public QThread { int &amp; argc; char ** argv; int result; void run() { QApplication a(argc, argv); Dialog d; d.show(); result = a.exec(); } public: AppThread(int &amp; argc, char ** argv) : argc(argc), argv(argv) {} ~AppThread() { quit(); wait(); } } extern "C" int __stdcall DLLStart(void) { auto *thread = new AppThread(argc, argv); thread-&gt;start(); return 0; } extern "C" void __stdcall DLLStop(void) { delete qApp-&gt;thread(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload