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. Can I use show()/exec() in a Secondary thread

Can I use show()/exec() in a Secondary thread

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 1.9k Views 3 Watching
  • 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.
  • T Offline
    T Offline
    Toocold
    wrote on last edited by
    #1

    I use the QDialog::show() in the GUI-thread ans a secondary thread, and it was crashed.
    So shouldn't I use that in a secondary thread?

    void func()
    {
    	QDialog *dialog = new QDialog(nullptr);
    	dialog->setAttribute(Qt::WA_DeleteOnClose);
    	dialog->setWindowTitle(QObject::tr("Hello, dialog!"));
    	dialog->show();
    }
    
    int main(int argc, char** argv)
    {
    	QApplication a(argc, argv);
    
    	QDialog *w = new QDialog(nullptr);
    	w->setWindowTitle(QObject::tr("Hello, dialog!"));
    	
    	auto ThreadInitResult = std::async(std::launch::async, [=]() {
    		func();
    	});
    
    	w->show();
    
    	return a.exec();
    }
    
    JKSHJ 1 Reply Last reply
    0
    • T Toocold

      I use the QDialog::show() in the GUI-thread ans a secondary thread, and it was crashed.
      So shouldn't I use that in a secondary thread?

      void func()
      {
      	QDialog *dialog = new QDialog(nullptr);
      	dialog->setAttribute(Qt::WA_DeleteOnClose);
      	dialog->setWindowTitle(QObject::tr("Hello, dialog!"));
      	dialog->show();
      }
      
      int main(int argc, char** argv)
      {
      	QApplication a(argc, argv);
      
      	QDialog *w = new QDialog(nullptr);
      	w->setWindowTitle(QObject::tr("Hello, dialog!"));
      	
      	auto ThreadInitResult = std::async(std::launch::async, [=]() {
      		func();
      	});
      
      	w->show();
      
      	return a.exec();
      }
      
      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @Toocold said in Can I use show()/exec() in a Secondary thread:

      So shouldn't I use that in a secondary thread?

      Correct. You cannot call GUI class functions in a secondary thread.

      Can I use show()/exec() in a Secondary thread

      No.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      T 1 Reply Last reply
      3
      • JKSHJ JKSH

        @Toocold said in Can I use show()/exec() in a Secondary thread:

        So shouldn't I use that in a secondary thread?

        Correct. You cannot call GUI class functions in a secondary thread.

        Can I use show()/exec() in a Secondary thread

        No.

        T Offline
        T Offline
        Toocold
        wrote on last edited by
        #3

        @JKSH Thanks for your reply. So I might use the signal/slot instead of?

        JKSHJ 1 Reply Last reply
        0
        • T Toocold

          @JKSH Thanks for your reply. So I might use the signal/slot instead of?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @Toocold said in Can I use show()/exec() in a Secondary thread:

          So I might use the signal/slot instead of?

          What do you want to do in the secondary thread?

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          T 1 Reply Last reply
          0
          • JKSHJ JKSH

            @Toocold said in Can I use show()/exec() in a Secondary thread:

            So I might use the signal/slot instead of?

            What do you want to do in the secondary thread?

            T Offline
            T Offline
            Toocold
            wrote on last edited by
            #5

            @JKSH I need call GUI class functions in a secondary thread, like the Dialog::show().
            Is it safe to connect a signal(like a function named notify()) to a slot(like a function named Onnotify(), ans Onnotify() will call the Dialog::show()) and emit the notify() in the secondary thread.

            KroMignonK SGaistS JKSHJ 3 Replies Last reply
            0
            • T Toocold

              @JKSH I need call GUI class functions in a secondary thread, like the Dialog::show().
              Is it safe to connect a signal(like a function named notify()) to a slot(like a function named Onnotify(), ans Onnotify() will call the Dialog::show()) and emit the notify() in the secondary thread.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #6

              @Toocold said in Can I use show()/exec() in a Secondary thread:

              I need call GUI class functions in a secondary thread, like the Dialog::show()

              If you only want to ensure calling Dialog::show() in the right thread, you can:

              • use signal/slot connection
              • use QTimer::singleShot() when your class instance in available in the calling object, like this: QTimer::singleShot(0, &m_myDialog, &Dialog::show);

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              JonBJ JKSHJ 2 Replies Last reply
              1
              • KroMignonK KroMignon

                @Toocold said in Can I use show()/exec() in a Secondary thread:

                I need call GUI class functions in a secondary thread, like the Dialog::show()

                If you only want to ensure calling Dialog::show() in the right thread, you can:

                • use signal/slot connection
                • use QTimer::singleShot() when your class instance in available in the calling object, like this: QTimer::singleShot(0, &m_myDialog, &Dialog::show);
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @KroMignon

                QTimer::singleShot(0, &m_myDialog, &Dialog::show);

                Wow, that's a bit cheaty ;-) So you get a thread to cause the UI thread (that's where the static QTimer::singleShot runs?) to do the open for you (immediately), without having to create a specific signal/slot from the thread to the UI to implement?

                KroMignonK 1 Reply Last reply
                0
                • T Toocold

                  @JKSH I need call GUI class functions in a secondary thread, like the Dialog::show().
                  Is it safe to connect a signal(like a function named notify()) to a slot(like a function named Onnotify(), ans Onnotify() will call the Dialog::show()) and emit the notify() in the secondary thread.

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  @Toocold said in Can I use show()/exec() in a Secondary thread:

                  @JKSH I need call GUI class functions in a secondary thread, like the Dialog::show().

                  That doesn't answer @JKSH question: what do you want to do with that dialog ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @KroMignon

                    QTimer::singleShot(0, &m_myDialog, &Dialog::show);

                    Wow, that's a bit cheaty ;-) So you get a thread to cause the UI thread (that's where the static QTimer::singleShot runs?) to do the open for you (immediately), without having to create a specific signal/slot from the thread to the UI to implement?

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #9

                    @JonB said in Can I use show()/exec() in a Secondary thread:

                    Wow, that's a bit cheaty ;-) So you get a thread to cause the UI thread (that's where the static QTimer::singleShot runs?) to do the open for you (immediately), without having to create a specific signal/slot from the thread to the UI to implement?

                    QTimer::singleShot() will:

                    • create a temporary QTimer object, which runs in the current thread
                    • connect to QTimer::timeout signal to the passed slot (and to deleteLater() of the temporary timer)
                    • start the temporary timer

                    This done under the hood.

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    0
                    • KroMignonK KroMignon

                      @Toocold said in Can I use show()/exec() in a Secondary thread:

                      I need call GUI class functions in a secondary thread, like the Dialog::show()

                      If you only want to ensure calling Dialog::show() in the right thread, you can:

                      • use signal/slot connection
                      • use QTimer::singleShot() when your class instance in available in the calling object, like this: QTimer::singleShot(0, &m_myDialog, &Dialog::show);
                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #10

                      @KroMignon said in Can I use show()/exec() in a Secondary thread:

                      • use QTimer::singleShot() when your class instance in available in the calling object, like this: QTimer::singleShot(0, &m_myDialog, &Dialog::show);

                      Or QMetaObject::invokeMethod(myDialog, "show");

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • T Toocold

                        @JKSH I need call GUI class functions in a secondary thread, like the Dialog::show().
                        Is it safe to connect a signal(like a function named notify()) to a slot(like a function named Onnotify(), ans Onnotify() will call the Dialog::show()) and emit the notify() in the secondary thread.

                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #11

                        @Toocold said in Can I use show()/exec() in a Secondary thread:

                        Is it safe to connect a signal(like a function named notify()) to a slot(like a function named Onnotify(), ans Onnotify() will call the Dialog::show()) and emit the notify() in the secondary thread.

                        Yes, it is safe to connect signals and slots betwen different threads.

                        However, you must make sure your QDialog is constructed in your QApplication thread.

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        T 1 Reply Last reply
                        1
                        • JKSHJ JKSH

                          @Toocold said in Can I use show()/exec() in a Secondary thread:

                          Is it safe to connect a signal(like a function named notify()) to a slot(like a function named Onnotify(), ans Onnotify() will call the Dialog::show()) and emit the notify() in the secondary thread.

                          Yes, it is safe to connect signals and slots betwen different threads.

                          However, you must make sure your QDialog is constructed in your QApplication thread.

                          T Offline
                          T Offline
                          Toocold
                          wrote on last edited by
                          #12

                          @JKSH Thank you for your answer. The QMetaObject::invokeMethod is helpful.

                          1 Reply Last reply
                          0

                          • Login

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