Qt custom library in non Qt application



  • Hi all,
    I'm trying to develop a c++ library using Qt to be used in non Qt c++ applications without success.
    The main problem seems to be how to start the event loop in the Qt Library.

    My library has the following source code:

    engine.cpp

    #include "engine.h"
    
    #include <QDebug>
    
    int Engine::argc = 1;
    char  *Engine::argv[] = {"Untitled", NULL};
    QCoreApplication * Engine::app=NULL;
    QThread * Engine::thread = NULL;
    
    
    Engine::Engine(QObject *parent) : QObject(parent)
    {
        if (thread == NULL)
        {
            thread = new QThread();
            connect(thread, SIGNAL(started()), this, SLOT(onStarted()), Qt::DirectConnection);
            thread->start();
        }
    }
    void Engine::onStarted()
    {
       if (QCoreApplication::instance() == NULL)
       {
           app = new QCoreApplication(argc, argv);
           app->exec();
       }
    //    startTimer(1000);
    }
    void Engine::timerEvent(QTimerEvent *e)
    {
        qDebug() << "Engine::timerEvent";
    }
    
    

    untitled.cpp

    #include "untitled.h"
    #include <QDebug>
    #include "engine.h"
    Untitled::Untitled()
    {
        e = new Engine();
    }
    

    and in the application that link to the library I have:

    #include <iostream>
    #include <unistd.h>
    #include "untitled.h"
    using namespace std;
    int main()
    {
        Untitled *u = new Untitled();
        while (1) {
            usleep(1000000);
            cout << "Hello World!" << endl;
        }
        return 0;
    }
    

    When running the application I get:

    QApplication was not created in the main() thread.
    

    Is there a solution?

    Thanks

    Luca


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.