Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how can i create two Widgets in different thread?
Forum Updated to NodeBB v4.3 + New Features

how can i create two Widgets in different thread?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Everton FonsecaE Offline
    Everton FonsecaE Offline
    Everton Fonseca
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by
      #2

      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

      Everton FonsecaE 1 Reply Last reply
      3
      • M mchinand

        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

        Everton FonsecaE Offline
        Everton FonsecaE Offline
        Everton Fonseca
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mchinand
          wrote on last edited by
          #4

          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."

          1 Reply Last reply
          3
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            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.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved