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

Update GUI at different time interval in QT

本文关键字:时间 更新 GUI QT      更新时间:2023-10-16

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

我需要使用多线程吗?

我尝试了pyqt,但失败了,请参阅"ui_mainwindow';对象没有属性';connect';"

以下是我将如何实现它,在MainWindow的类中,定义一个将QLabel设置为下一个映像的插槽:

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; 
}

MainWindow的构造函数中,创建一个具有所需间隔的QTimer,并将其timeout()信号连接到上定义的NextImage插槽

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
}

请记住将currentImageNonumberOfImagestimer声明并初始化为MainWindow类的成员。