0

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 スロットに移動するため、ここでは、緑のスロットに移動してから黄色に移動することを想定していますが、再び緑に移動することはありません。

誰か教えてください。

4

2 に答える 2

1

コメントを読む:

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);
}
于 2015-03-14T13:30:02.633 に答える
0

おそらく最も簡単に:

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

のメンバー関数show()およびは、それぞれおよび と同等です。hide()QWidgetsetVisible(true)setVisible(false)

開始ボタンを複数回押すと後で発生する可能性のある問題は、毎回新しいインターバル タイマーを生成するため、ボタンを押すたびに点滅が速くなるということです。これが発生したくない場合は、新しいタイマーの生成を制御する必要があります。

于 2015-03-14T13:27:21.803 に答える