我想知道如何在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
发布评论