【发布时间】:2018-02-07 16:01:23
【问题描述】:
目前我正在使用 Qt 5.9.2(MSVC 2015 编译器)开发一个 Windows DLL,它应该由现有的商业 MFC 应用程序加载。应此应用程序的请求,应显示QDialog 的模态实例。
由于QApplication::exec() 会阻塞整个应用程序,我使用以下代码“模拟”事件循环:
void Core::createQApplicationInstance()
{
// Check, if there's already a 'QApplication' instance running (unlikely)
if (!QApplication::instance())
{
int argc = 1;
// Create a new 'QApplication' instance
m_app = new QApplication(argc, nullptr);
// Create a 'QTimer' instance to call 'processEvents' periodically:
// We can't run 'm_app->exec()' because it would block everything,
// so we'll use this 'hacky-whacky' method here
m_timer = new QTimer;
// Connect the timer's timeout to the app's 'processEvents' via a lambda
QObject::connect(
m_timer,
&QTimer::timeout,
[&]()
{
m_app->processEvents();
}
);
// Start the timer with the fixed 'message' interval
m_timer->start(kMsgInterval);
}
}
如果我的 DLL 现在应该显示一个模式对话框,它(部分)使用以下代码:
{...}
case eUserIGeneral:
{
qDebug() << "<< eUserIGeneral";
QDialog w;
w.setModal(true);
w.exec();
// --> Code here is executed AFTER the dialog has been closed
}
break;
//-------------------------------------------------------------------
{...}
w.exec() 之后的代码实际上将在对话框关闭后执行(如预期的那样)。但是,主应用程序仍然保持响应,并且不受我的对话框模式的影响,这与我预期的不一样。
如何确保在调用模态 DLL 对话框时锁定主应用程序中的输入?
【问题讨论】:
-
M... 除了在自己的新线程上创建 Qt 应用程序实例并旋转事件循环之外,我无法清楚地看到其他方式。这将如何与 MFC UI 线程对话是另一回事和有趣的效果。
-
您基本上只是在显示对话框之前禁用所有者窗口。例如。
EnableWindow(hWndOwner, FALSE);并在模态结束后再次启用它。
标签: c++ qt mfc event-loop