Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Calling a shared library that creates/uses Qt GUI

$
0
0
Hello all… I’m trying to call a shared library (specifically, a Mac dylib) from a main program that knows nothing of Qt. Both the main program and Qt dynamic library are in Qt Creator. I have the entry point into the library working just fine, my main program calls it with no issues. The problem I’m having is: at what point do I create the QApplication to get the ball rolling in the library? This is complicated by my wanting to use a QThread to spawn off a thread to run the Qt-based code in the shared library. I’m hoping I can then return to the main program and let the Qt GUI run. Ultimately, I’d like to put entry points in the library to let the main program stop the Qt thread. I’m hoping this is possible, and that nobody’s head has exploded at the thought of what I’m trying to do. ;) If there’s some document that gives hints on how to do this, that’d be great, otherwise, any pearls of wisdom would be appreciated. And no question would be complete without some code snippets… The library entry point looks like this:   static Runner *theObject;     QString dialogString = "Using default label";     // create QApplication   int argc = 0;   const QApplication *a = new QApplication(argc, nullptr);     // OK, set up the thread and let it loose...   theObject = new Runner(dialogString, *a);     // Conect the Runner object to a new thread   QThread* thread = new QThread;     // This code is taken from http://qt-project.org/wiki/QThreads_general_usage   theObject->moveToThread(thread);   theObject->connect(thread, SIGNAL(started()), theObject, SLOT(process()));     theObject->connect(theObject, SIGNAL(finished()), thread, SLOT(quit()));   theObject->connect(theObject, SIGNAL(finished()), theObject, SLOT(deleteLater()));   theObject->connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));   thread->start(); } The Runner object looks like this: class Runner : public QObject {   Q_OBJECT   public:   Runner(const QString label, const QApplication &a)   {     thisQApp = &a;     messageLabel = label;   }     ~Runner()   {   }   public slots:   void process();   signals:   void finished();   void error(QString err);   private:   QString messageLabel;   const QApplication *thisQApp;   }; and the process method of the Runner object looks like this: void Runner::process() {   PoppyUpper *thePup;     thePup = new PoppyUpper();   thePup->setRandomColors();   thePup->setLabel(messageLabel);     thePup->show();   thisQApp->exec();   delete thePup;   emit finished(); } PoppyUpper is the function in the library does does all the GUI goodness. Everything works find if I don’t put QThreads into the mix. What happens currently (in the debugger) is that Runner::process() never gets called, and the thread->start() returns with no error message and the whole program exits. Oh, and in case it makes any difference, the fact that it’s a dynamic library is entirely optional. It could just as well be static-linked, in case that makes any difference. Hopefully, I’m just overlooking something… Thanks! -Eric

Viewing all articles
Browse latest Browse all 13965

Trending Articles