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

  • the main does not have the signal-slot mechanism.


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

  • 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;;

    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()));

    sleep(1) ; //try to use sleep to make the main function wait, but fails. the main function is kinda of blocked here

        //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
        //this is a user
        cout<<"starts user"<<endl;
    return a.exec&#40;&#41;;

    } @

  • 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().

  • Thanks, Gerolf.

    I notice my mistake now.

Log in to reply

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