how can i create two Widgets in different thread?



  • I need to create 2 widgets, they need to be each in a different thread in QApplication



  • Why do you need them in different threads? I'm pretty sure it's not possible, see http://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread



  • @mchinand
    #include "mainwindow.h"
    #include <QApplication>
    #include <pthread.h>

    struct InputArgs{

    int argc;
    char **argv;
    

    };

    void *StartQAppThread(void *threadArg) {

    InputArgs *args = (struct InputArgs*) threadArg;
    
    QApplication app(args->argc, args->argv);
    MainWindow w;
    w.show();
    
    app.exec();
    pthread_exit(NULL);
    

    }

    pthread_t* startAppThread(InputArgs &args) {

    pthread_t* thread_handles = (pthread_t*) malloc(sizeof(pthread_t));
    int res = pthread_create(thread_handles, NULL,StartQAppThread, (void*)&args);
    
    return thread_handles;
    

    }

    int main(int argc, char *argv[])
    {
    InputArgs input = {argc,argv};

    pthread_t* t1 = startAppThread(input);
    pthread_t* t2 = startAppThread(input);
    
    pthread_join(*t1,NULL);
    
    return 0;
    

    }

    WARNING: QApplication was not created in the main() thread.
    ASSERT failure in QCoreApplication: "there should be only one application object", file kernel\qcoreapplication.cpp, line 769
    Invalid parameter passed to C runtime function.
    Invalid parameter passed to C runtime function.
    The program has unexpectedly finished.



  • What are you trying to achieve by trying to do this?

    http://doc.qt.io/qt-5/qapplication.html#details

    "For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. For non-QWidget based Qt applications, use QGuiApplication instead, as it does not depend on the QtWidgets library."



  • Not sure about the requirement. It is quite strange that you are trying to create two windows in two different threads. Y do u want do like this ? If the visual elements are own in different threads, u have more problems than solution. So creation of visual elements in two different threads is not allowed. To create visual elements you need to have QApplication object. This need be one instance per application. You can create two windows in only one threads. Threads can pass the data to different windows.


Log in to reply
 

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