QT 应用程序的代码应该放在哪里?
我从 QT4 开始,我想知道在哪里放置我的应用程序代码。
这里?
void MainWindow::changeEvent(QEvent *e) {...}
还是这里? (具体在哪里?)
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
如果我希望我的应用程序不仅对用户事件做出反应,而且定期循环执行,我应该将循环放在哪里?
I'm starting with QT4, I'm wondering where to put my application code.
Here?
void MainWindow::changeEvent(QEvent *e) {...}
Or here? (where exactly?)
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
If I want my app not only react to user events, but to execute regularly on a loop, where do I put the loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您在非 gui 线程中循环,否则您将通过循环(在隐式主 gui 线程中)阻塞 GUI。这里有几种不同的方法:
还有其他不同的方法,例如使用 processEvents() 但我个人推荐线程方法。
Unless you loop within a non-gui thread, you will block the GUI by looping (in the implicit main gui thread). Here's a couple of different approaches:
There are other different approaches, such as using processEvents() but I'd personally recommend the threads approach.
Qt 有自己的主循环,您可以使用 QTimer 类连接到它。
如果您想提供自己的事件循环,可以使用 QApplication::processEvents( )向 Qt 发送信号以处理其事件(并保持负责任的 UI)。
另外, QAbstractEventDispatcher 可能对您有用。
这个问题可能也有用。
Qt has its own main loop, you can connect to it using the QTimer class.
If you want to provide your own event loop, you can use QApplication::processEvents() to signal Qt to process it's events (and keep a responsible UI).
Also, QAbstractEventDispatcher might be useful to you.
This question might also be useful.