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);
}
また、タイマーが 2 回実行されないように、クリックしたときに [開始] ボタンを無効にします。
したがって、メインのウィンドウ UI には、基本的にユーザーの入力を受け取るスピンボックスがあります。これは、それを Qtimer に渡す時間です。次に、間隔で表示および非表示にする必要がある赤、緑、黄色の 3 つの画像があります。 . それで、ほとんど手動ループのようなものを作成しました。Qtimer が起動して緑を表示し、次に ShowYellow スロット、次に showRed スロットに移動するため、ここでは、緑のスロットに移動してから黄色に移動することを想定していますが、再び緑に移動することはありません。
誰か教えてください。