问如何实现流程循环
Qt how to implement a process loop?
我刚开始使用Qt,目前正在调整一个命令行程序,使其与GUI一起使用。
我正在构建这样的GUI:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
我想永久处理一些事件。在命令行中,我使用了while循环,它工作得很好。使用Qt,我不知道如何正确处理这些事件。所以我尝试使用std::线程,但当我尝试从线程修改GUI时,我的Qt应用程序崩溃了。使用QThread也有同样的问题。我不需要线程,所以如果我能把我的代码放在Qt的主线程中,那就太好了。
有人能帮我吗?
您可以使用连接到MainWindow类中插槽的QTimer来周期性地运行一个函数,如下所示:
MainWindow::MainWindow()
{
myTimer = new QTimer();
myTimer->setSingleShot(false);
myTimer->start(intervalInMilliseconds);
connect(myTimer, &QTimer::timeout, this, &MainWindow::handleMyEvents);
}
void MainWindow::handleMyEvents()
{
// Your code here
}
您也可以使用线程,但请注意,您不能从任何非QApplication线程的线程调用任何GUI代码,这可能是您的尝试失败的原因。
相关文章:
- 有没有办法重复循环循环?
- 在其他线程中循环访问该concurrent_vector时调用 concurrency::concurrent_vect
- C++线程退出循环条件
- 虽然第三次循环循环,尽管在我眼中没有满足它的条件,因此打印了一个空字符串
- 避免以不同的迭代剂的循环循环避免几乎相同的代码重复
- 循环循环保存到数组,然后访问;
- 通过线程分发循环迭代
- 对于循环循环仅 3 次
- OpenMP-每个线程的循环/数组界限
- 线程在循环的中间随机停止
- 构建和同步多线程游戏循环
- C++线程&无限循环
- msleep() 是否为其他线程提供循环
- 使用线程C++停止循环
- 使用提升线程和循环缓冲区挂起的生产者/使用者
- C++线程-在循环中生成一个对象(类)
- OpenMP计算每个线程的循环迭代次数
- 创建qt线程事件循环
- 用于存储指针的线程安全循环缓冲区
- 使用openMP在多线程for循环中创建线程