0

I need to open QDialog by using QTimer singleShot and wait for a status flag. If this variable is true then continue.

This is my code

StatusFlag = FALSE;

void MainWindow::OpenWindow()
{
   qDebug("OpenWindow"); 
   NewGUI *gui=new NewGUI();
   gui->setModal(true);
   gui->exec();
}
void MainWindow::mWait(ulong milliSeconds)
{
    QEventLoop loop;
    QTimer::singleShot(milliSeconds, &loop, SLOT(quit()));
    loop.exec();
}

In NewGUI constructor StatusFlag is set to TRUE

QTimer::singleShot(0, this, SLOT(OpenWindow()));
while(StatusFlag == FALSE)
{
   //Wait until StatusFlag is TRUE.
   qDebug("Enter"); 
   mWait(1);
   qDebug("Exit");         
}

if(StatusFlag == TRUE)
{
   //Do something
   qDebug("Do something");  
}

Current Output is

Enter 
OpenWindow

Expected Output is

Enter 
Exit
OpenWindow
Do something

If i comment the line

QTimer::singleShot(0, this, SLOT(OpenWindow()));

Then the output is

Enter 
Exit.. still continuing

Do you have any suggestion?

4

1 回答 1

1

在您的代码中,gui->exec();启动一个新的本地事件循环,这样您就永远不会退出OpenWindow(). 因此,您的 while 循环将阻塞,mWait(1);直到您关闭对话框。

您可以在 NewGUI 构造函数中发出信号,而不是设置标志,您可以将 MainWindow 的插槽连接到该信号。您可以在此插槽中做任何您需要做的工作。

于 2015-02-27T11:49:50.753 回答