在我的 Qt 程序中,我有模态 QDialogs
,它旨在停止一切并且在代码被关闭之前不会继续执行代码。它适用于它所在的函数——我在 qDialog::exec()
之后的下一行代码中放置了一个断点,直到我关闭对话框后它才会中断。
但是,有一个 QTimer
在超时时连接到一个插槽,即使模式对话框启动并执行其插槽中的代码,它也会继续运行。
我想我可以在显示模态对话框
之前停止计时器。但是,在某些情况下,对话框可能与计时器属于完全不同的类。有没有办法真正停止程序的执行,直到 QDialog
被关闭?
例子:
QTimer* pTestTimer = new QTimer( this );
connect( pTestTimer , SIGNAL( timeout() ), this, SLOT( timerSlot() ) );
//Slot code elsewhere
void cMyClass::deleteMeTimerSlot()
{
qDebug() << "See me during modal?";
}
//starting a modal dialog
pTestTimer->start( 1000 );
QDialog* pModalDlg = new QDialog( this, Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint );
pModalDlg->setModal(true);
pMOdalDlg->exec();
输出仍然显示“See me during modal?”而在 exec()
;
最佳答案
I suppose I could stop the timer before showing the modal dialog. However, there may be cases when the dialog is in a totally different class than the timer.
是的,您可以对父上下文中可访问的所有计时器进行操作(看起来您正在寻找通用解决方案)。
// Halt all the timers within parent context.
// If all the timers have parent maybe the top app widget pointer
// should be used for parentObj
QList<QTimer*> listOfTimers = parentObj->findChildren<QTimer*>(
QString(), Qt::FindChildrenRecursively);
for(QTimer* pTimer : listOfTimers)
pTimer->stop();
Is there a way to truly halt execution of the program until the QDialog is dismissed?
除非你的意思是停止所有线程:不,QDialog 是程序的一部分,并不意味着停止任何执行,但模态对话框在自己的事件循环中运行,只会阻止用户操作其他程序 UI。我想您可以在退出模态对话框后重新启动所有停止的计时器。
pModalDlg->exec();
// restart all the timers within parent context
for(QTimer* pTimer : listOfTimers)
pTimer->start();
您还可以查看 Qt 源代码(QDialog 和它使用的事件循环)并创建您自己的带有非常特定事件循环的复杂对话框类。
关于qt - Modal QDialog 仍然允许定时器调用槽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31486130/