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 to make the main thread wait for a certain action is performed in the UI
Qt 6.11 is out! See what's new in the release blog

How to make the main thread wait for a certain action is performed in the UI

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 5.6k Views 1 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.
  • D Offline
    D Offline
    dmmzy
    wrote on last edited by
    #1

    the main does not have the signal-slot mechanism.

    Thanks

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

      Maybe "QMutex":http://doc.qt.nokia.com/4.7/qmutex.html#details ?

      There are 10 types. Those who understand binary and those who don't .)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        Can you elaborate? The information you gave are not sufficient to see what the problem is (the general answer would be "don't do nothing -- return to the event loop").

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dmmzy
          wrote on last edited by
          #4

          My bad.
          It's like My program has 3 UI Windows: Welcome.ui, Server.ui and Client.ui.
          User can specify her identity as one of the two roles in the welcome window.
          Base on the role chosen, the main function run the corresponding children thread which keeps listening for messages.

          My main function is like:
          @int main(int argc, char *argv[])
          {
          QApplication a(argc, argv);
          EC w;
          w.show();

          AdminWindow aw;
          UserWindow uw;
          
          QObject::connect(&w, SIGNAL(server_go()), &aw, SLOT(on_server_go()));
          QObject::connect(&w, SIGNAL(user_go()),&uw,SLOT(on_user_go()));
          

          while(!aw.if_show&&!uw.if_show){
          sleep(1) ; //try to use sleep to make the main function wait, but fails. the main function is kinda of blocked here
          }

          if(aw.if_show){
              //this is a sever
              cout<<"starts server"<<endl;
          
              SThread *s_thread = new SThread();   //server thread 
              QObject::connect(s_thread, SIGNAL(test_go(QString)), &aw, SLOT(on_test_go(QString))); //communications between the children thread and UI in main thread
          
              s_thread->start();
          
          
          
          }
          
          if(uw.if_show){
              //this is a user
              cout<<"starts user"<<endl;
          
          }
          
          return a.exec&#40;&#41;;
          

          } @

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            Hi dmmzy,

            you use the wrong pattern.
            You should display the first window and spin the event loop. When the user enters his/her value and clicks a button, show the second window snd start the needed thread.

            The UI will not work without a.exec().

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dmmzy
              wrote on last edited by
              #6

              Thanks, Gerolf.

              I notice my mistake now.

              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