【问题标题】:Trigger different event on QTimer reset在 QTimer 重置时触发不同的事件
【发布时间】:2015-03-15 00:49:26
【问题描述】:

我正在构建一个使用 QTimer 显示交通信号灯图像的小程序。所以我设置了我的计时器,一切正常。但我不知道,每次达到定时器间隔时,如何让机器人灯 ->show() 和 ->hide()。我可以把这一切都错了,我还在学习,所以请指教。

主窗口.h

 #ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QTimer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void timer();
    QTimer *mytimer;



private:
    Ui::MainWindow *ui;
    int timerValue;

private slots:
    void showGreen();
    void showYellow();
    void showRed();
    void on_startButton_clicked();

};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   ui->green->setVisible(false);
    ui->yellow->setVisible(false);
     ui->red->setVisible(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::timer(){
    ui->green->setVisible(true);
    mytimer = new QTimer(this);
    connect(mytimer,SIGNAL(timeout()),this,SLOT(showYellow()));
    mytimer->start(timerValue = (ui->spinBox->value())*1000);

}

void MainWindow::showYellow(){
    ui->yellow->setVisible(true);
    mytimer = new QTimer(this);
    connect(mytimer,SIGNAL(timeout()),this,SLOT(showRed()));
    mytimer->start(timerValue);
}

void MainWindow::showRed(){
   ui->red->setVisible(true);
    mytimer = new QTimer(this);
    connect(mytimer,SIGNAL(timeout()),this,SLOT(showGreen));
    mytimer->start(timerValue);
}
void MainWindow::showGreen(){
    ui->green->setVisible(true);
    mytimer = new QTimer(this);
    connect(mytimer,SIGNAL(timeout()),this,SLOT(showYellow()));
    mytimer->start(timerValue);
}
void MainWindow::on_startButton_clicked()
{
    timer();
    ui->startButton->setEnabled(false);
}

我还禁用了单击时的“开始”按钮,因此计时器无法运行两次。

所以在主窗口 UI 上,我基本上有一个 Spinbox,它接受用户的输入,也就是我将其传递给 Qtimer 的时间,然后我有 3 个带有灯红色绿色黄色的图像,它们必须显示和隐藏在间隔。所以我创建了几乎类似于手动循环的东西。 Qtimer 启动并显示绿色,然后进入 ShowYellow Slot,然后显示 Red 插槽,所以现在,它假设进入 Green 插槽然后变为黄色,但它不会再次进入 Green。

谁能告诉我为什么不。

【问题讨论】:

    标签: c++ qt qtimer


    【解决方案1】:

    阅读cmets:

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
        ...
        mytimer = new QTimer(this); // create QTimer once
    }
    
    void MainWindow::timer() {
        timerValue = ui->spinBox->value() * 1000;
    
        showGreen(); // reuse showGreen()
    }
    
    void MainWindow::showYellow() {
        // this is how toggling can be done
        ui->yellow->setVisible(!ui->yellow->isVisible());
    
        mytimer->disconnect(); // disconnect before making a new connection
        connect(mytimer ,SIGNAL(timeout()), this, SLOT(showRed()));
        mytimer->start(timerValue);
    }
    
    void MainWindow::showRed() {
        ui->red->setVisible(!ui->red->isVisible());
    
        mytimer->disconnect();
        connect(mytimer ,SIGNAL(timeout()), this, SLOT(showGreen()));
        mytimer->start(timerValue);
    }
    
    void MainWindow::showGreen() {
        ui->green->setVisible(!ui->green->isVisible());
    
        mytimer->disconnect();
        connect(mytimer ,SIGNAL(timeout()), this, SLOT(showYellow()));
        mytimer->start(timerValue);
    }
    

    【讨论】:

    • 我并没有真正听从你的话。这会解决我显示和隐藏图像的问题吗?或者这只是我的程序的更好实践?
    • 让多个计时器调用同一个插槽不仅是一种不好的做法,而且是个问题。 @Wintermute 展示了如何切换图像可见性。
    • 我通过在用户单击按钮时禁用按钮来解决该问题。根据分配,除非关闭,否则计时器应该无法停止,所以现在很好。我还有一个问题,您介意对我对问题/代码所做的新编​​辑发表评论
    • 与 cmets 一起阅读。
    • 哇非常感谢。不知道我只能创建一个计时器。但是现在该程序由于某种原因跳过了黄色。知道为什么吗?
    【解决方案2】:

    也许最简单:

    void MainWindow::showHide(){
        ui->green->setVisible(!ui->green->isVisible());
    }
    

    QWidget 的成员函数show()hide() 分别等效于setVisible(true)setVisible(false)

    如果您多次按下开始按钮,您以后可能会遇到的问题是,您每次都会生成一个新的间隔计时器,从而导致每次按下按钮时都会更快地闪烁。如果这不是您想要发生的,您将不得不控制新计时器的生成。

    【讨论】:

      猜你喜欢
      • 2015-09-25
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      相关资源
      最近更新 更多