在 QTimer 重置时触发不同的事件
Trigger different event on QTimer reset
我正在构建一个使用 QTimer 显示交通信号灯图像的小程序。所以我设置了我的计时器,一切正常。但我无法弄清楚,每次达到定时器间隔时,如何让机器人灯进入 ->show() 和 ->hide()。我可能错了,我还在学习,所以请指教。
mainwindow.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
mainwindow.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);
}
我还禁用了单击时的“开始”按钮,因此计时器不能 运行 两次。
所以主要是 Window UI 我基本上有一个 Spinbox,它接受用户的输入,这是我将其传递给 Qtimer 的时间,然后我有 3 个图像红绿黄的灯,在间隔中有显有隐。所以我创建了几乎类似于手动循环的东西。 Qtimer 启动并显示 Green,然后转到 ShowYellow Slot,然后显示 Red 插槽,所以现在,它应该转到 Green 插槽,然后转到黄色,但它不会再次转到 Green。
谁能告诉我为什么不呢。
也许最简单:
void MainWindow::showHide(){
ui->green->setVisible(!ui->green->isVisible());
}
QWidget
的show()
和hide()
成员函数分别等价于setVisible(true)
和setVisible(false)
。
您以后可能 运行 遇到的一个问题是,如果您多次按下开始按钮,您每次都会生成一个新的间隔计时器,导致每次按下按钮时闪烁速度更快。如果这不是您想要的,您将不得不控制新计时器的生成。
阅读评论:
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);
}
我正在构建一个使用 QTimer 显示交通信号灯图像的小程序。所以我设置了我的计时器,一切正常。但我无法弄清楚,每次达到定时器间隔时,如何让机器人灯进入 ->show() 和 ->hide()。我可能错了,我还在学习,所以请指教。
mainwindow.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
mainwindow.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);
}
我还禁用了单击时的“开始”按钮,因此计时器不能 运行 两次。
所以主要是 Window UI 我基本上有一个 Spinbox,它接受用户的输入,这是我将其传递给 Qtimer 的时间,然后我有 3 个图像红绿黄的灯,在间隔中有显有隐。所以我创建了几乎类似于手动循环的东西。 Qtimer 启动并显示 Green,然后转到 ShowYellow Slot,然后显示 Red 插槽,所以现在,它应该转到 Green 插槽,然后转到黄色,但它不会再次转到 Green。
谁能告诉我为什么不呢。
也许最简单:
void MainWindow::showHide(){
ui->green->setVisible(!ui->green->isVisible());
}
QWidget
的show()
和hide()
成员函数分别等价于setVisible(true)
和setVisible(false)
。
您以后可能 运行 遇到的一个问题是,如果您多次按下开始按钮,您每次都会生成一个新的间隔计时器,导致每次按下按钮时闪烁速度更快。如果这不是您想要的,您将不得不控制新计时器的生成。
阅读评论:
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);
}