【问题标题】:Update GUI at different time interval in QT [closed]在 QT 中以不同的时间间隔更新 GUI [关闭]
【发布时间】:2016-05-06 00:53:40
【问题描述】:

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

我需要使用多线程吗?

我试过pyqt但是失败了,请看“ui_mainwindow' object has no attribute 'connect'

【问题讨论】:

  • 好像你使用 pyqt ,你为什么用c++标记你的问题?
  • 请更具体。
  • @Mike 是一名 C++ 程序员。但我正在学习pyqt。无论哪种方式对我来说都很好。
  • @Fabio 例如,我想将 Qtlabel 内容更新为从时间 t0 到 t1 的 image1,从时间 t1 到 t2 的 image2,...,从时间 t(n-1) 到的 imagen t(n)。
  • 你有类似问题的可用代码吗?

标签: python c++ multithreading qt pyqt


【解决方案1】:

这是我将如何实现它,在您的 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 类的成员。

【讨论】:

  • 定时器是 MainWindow 类的成员吗?
  • 你有整套代码吗?
  • 它似乎正在工作!你有这段代码的pyqt版本吗?再次感谢您的代码。
  • @Feng ,是的 timercurrentImageNonumberOfImages 都是班级成员 MainWindow
猜你喜欢
  • 2017-09-16
  • 1970-01-01
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多