有没有QApplication :: exec()使用Qt的方法? [英] Is there a way to use Qt without QApplication::exec()?
问题描述
我有多个不同的对象在多个资源上执行长时间的进程(至少有一个正在与Web应用程序服务器通信)。我正在制作一个GUI应用程序,提示用户在适当的时间输入这些不同的进程。我想让我的'流'逻辑 - 逻辑,决定下一步做什么 - 在一个地方,而不是一个GUI对象,如对话框类。我在想我可以这样做:
...
wait_dialog dlg;
dlg.setModal(false);
dlg.show(); //应该返回...
netobject.start_long_lived_process_that_happens_on_other_thread(& completion_callback);
while(!completion_callback_called())
{
qApp-> processEvents();
netobject.pump_callbacks();
magically_avoid_busywait_while_still_servicing_qt_somehow();
}
dlg.hide();
...
从Qt的角度来看,这是否安全?有没有一个好的方式实现 magically_avoid_busywait_while_still_servicing_qt_somehow()
?
我要在这里完成的是以最明确的方式写我们的处理流程。我想要一个单一的功能:
show_a_non_modal_wait_dialog()
start_some_processing_1()
wait_for_processing_1_to_finish()
dismiss_non_modal_wait_dialog()
show_modal_input_dialog()
如果(已取消)返回
show_a_non_modal_wait_dialog()
start_some_processing_2()
wait_for_processing_2_to_finish()
dismiss_non_modal_wait_dialog()
show_modal_input_dialog()
如果(取消)返回
...
我真正想避免的是在Qt窗口小部件和窗口中进行处理。此外,处理对象本身完全独立于Qt。我想我想要做的是在单个函数中创建一个控制器,其中包含几个帮助器回调和状态变量。
你想要的是一个事件循环,其他应用程序的主事件循环。这可以使用 QEventLoop
:
wait_dialog dlg;
dlg.setModal(false);
dlg.show(); //应该返回...
QEventLoop循环;
connect(& netobject,SIGNAL(done()),& loop,SLOT(quit()));
netobject.start_long_lived_process_that_happens_on_other_thread();
loop.exec(); // BLOCKING(非忙)直到quit()通过signal完成调用()
这是(在我眼中)干净的代码,这需要你的netobject类成为一个QObject,并实现一个信号 done()
(这比提供回调更干净) / p>
现在,您可以将整个代码包装在一个将自动阻止调用的函数中,因此如果您希望,可以从对话框返回一些结果。
Is there a safe way to use Qt without calling QApplication::exec()?
I have a number of different objects that are carrying out long-lived processes on multiple resources (at least one of them is communicating with a web application server). I'm making a GUI application that prompts the user for input at the right time for these different processes. I'd like to have my 'flow' logic - the logic that determines what to do next - in one place and not in a GUI object like a dialog class. I was thinking that I could do something like this:
...
wait_dialog dlg;
dlg.setModal( false );
dlg.show(); // Should return...
netobject.start_long_lived_process_that_happens_on_other_thread( &completion_callback );
while ( !completion_callback_called() )
{
qApp->processEvents();
netobject.pump_callbacks();
magically_avoid_busywait_while_still_servicing_qt_somehow();
}
dlg.hide();
...
Is this safe, from Qt's point of view? Is there a 'good' way of implementing magically_avoid_busywait_while_still_servicing_qt_somehow()
?
What I'm trying to accomplish here is to write our processing flow in the most explicit way possible. I'd like a single function that does this:
show_a_non_modal_wait_dialog()
start_some_processing_1()
wait_for_processing_1_to_finish()
dismiss_non_modal_wait_dialog()
show_modal_input_dialog()
if ( cancelled ) return
show_a_non_modal_wait_dialog()
start_some_processing_2()
wait_for_processing_2_to_finish()
dismiss_non_modal_wait_dialog()
show_modal_input_dialog()
if ( cancelled ) return
...
What I really want to avoid is kicking off and waiting for the processing inside Qt widgets and windows. Also, the processing objects themselves are entirely independent of Qt. I suppose what I'm trying to do is create a controller in a single function with a few helper callbacks and status variables.
What you want is an event loop other that the application's main event loop. This can be done using the QEventLoop
:
wait_dialog dlg;
dlg.setModal( false );
dlg.show(); // Should return...
QEventLoop loop;
connect(&netobject, SIGNAL(done()), &loop, SLOT(quit()));
netobject.start_long_lived_process_that_happens_on_other_thread();
loop.exec(); // BLOCKING (non-busy) until quit() is called via the signal done()
While this is (in my eyes) clean code, this requires your netobject class to be a QObject and implement a signal done()
(which is also cleaner than providing callbacks).
Now you can wrap this whole code in a function which will be a blocking call on itself, so it can return some results from your dialog if you want so.
这篇关于有没有QApplication :: exec()使用Qt的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!