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.
Thanks
-
Maybe "QMutex":http://doc.qt.nokia.com/4.7/qmutex.html#details ?
-
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;
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();
} @
-
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.