在QT中以不同的时间间隔更新GUI

编程入门 行业动态 更新时间:2024-10-23 09:38:44
本文介绍了在QT中以不同的时间间隔更新GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道如何在QT的不同时间间隔更新GUI,最好的是我可以控制时间间隔。我知道QTimer可以在相同的时间间隔更新GUI,但我需要控制时间间隔,并将它们设置为不同的值。

我需要使用多线程吗? / p>

我试过pyqt但失败,请参阅 ui_mainwindow'对象没有属性'connect'

解决方案

,在 MainWindow 的类中,定义一个插槽,将 QLabel 设置为下一个图像:

void MainWindow :: NextImage(){ switch(currentImageNo){ case 0: // set first image break; case 1: //设置第二个图像 break; case 2: //设置第三张图片 break; ... } timer.setInterval(/ *您希望的基于currentImageNo * /的新间隔) currentImageNo ++; //为了在显示最后一个图像后循环回第一个图像 currentImageNo = currentImageNo%numberOfImages; MainWindow 中的<}

<以所需的间隔创建 QTimer ,并将 timeout()信号连接到 NextImage 上面定义的槽

MainWindow :: MainWindow(){ .. 。 timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(NextImage())); timer.setSingleShot(false); timer.setInterval(5000); // 5秒为第一个图像 timer.start(); NextImage(); //最初加载第一个图像}

请记住声明并初始化 currentImageNo , numberOfImages 和 timer > MainWindow class。

I want to know how to update the GUI at different time interval in QT, the best is that I could control the time intervals. I know QTimer could update GUI at the same time interval but I need to control the time intervals and set them to be different values.

Do I need to use multithreading?

I tried pyqt but Failed, please see "ui_mainwindow' object has no attribute 'connect'"

解决方案

Here's how I would implement it, in your MainWindow's class, define a slot that sets the QLabel to the next image:

void MainWindow::NextImage(){ switch(currentImageNo){ case 0: //set 1st image break; case 1: //set 2nd image break; case 2: //set 3rd image break; ... } timer.setInterval( /*your desired new interval based on currentImageNo*/ ); currentImageNo++; //in order to loop back to the first image after the last image is shown currentImageNo= currentImageNo % numberOfImages; }

in your MainWindow's constructor, create a QTimer with your desired interval and connect its timeout() signal to the NextImage slot defined above

MainWindow::MainWindow(){ ... timer= new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(NextImage())); timer.setSingleShot(false); timer.setInterval(5000); //5 secs for first image timer.start(); NextImage(); //to load the first image initially }

remember to declare and initialize currentImageNo, numberOfImages and timer as members of your MainWindow class.

更多推荐

在QT中以不同的时间间隔更新GUI

本文发布于:2023-07-18 04:37:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间隔   中以   时间   QT   GUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!