1

QTimer を使用して 5 秒ごとに現在の時刻を表示するテキスト編集ボックスを取得しようとしています。別のメソッドで現在の時刻を計算してから、QTimer にそのメソッドを呼び出して現在の時刻を表示させています。setCurrentTime メソッドから QTimer に変数を渡す方法を一生理解できません。それは本当に簡単な修正だと確信していますが、私はそれを理解できません。これが私のコードです。

void noheatmode::setCurrentTime()
{
   QTime time = QTime::currentTime();
   QString sTime = time.toString("hh:mm:mm");
   // ui->tempTimeNoHeatMode->append(sTime);

}

void noheatmode::on_timeButton_clicked()
{


    QTimer *timer =new QTimer(this);
    connect(timer,SIGNAL(timeout()), this, SLOT(setCurrentTime()));
    timer->start(5000);
    ui->tempTimeNoHeatMode->append(sTime);
}
4

2 に答える 2

0

あなたのコードで:

void noheatmode::on_timeButton_clicked()
{
    QTimer *timer =new QTimer(this);
    connect(timer,SIGNAL(timeout()), this, SLOT(setCurrentTime()));
    timer->start(5000);
    ui->tempTimeNoHeatMode->append(sTime);
}

これは、内の関数がミリ秒SLOTごとに呼び出されることを意味し5000ます。これは = 5 秒です。setCurrentTime()その場合にできることは、呼び出されるたびにテキストボックスを更新するように関数を設定することです。

例:

void Class::setCurrentTime()
{
    QTime times = (QTime::currentTime());
    QString currentTime=times.toString("hh:mm:ss");
    ui->label->setText(currentTime); 
    //Assuming you are using a label to output text, else substitute for what you are using instead
    //Every time this function is called, it will receive the current time 
    //and update label to display the time received
}
于 2015-06-11T19:00:38.320 に答える
0

問題が正しければ、秒ではなく分の変数しかありません。「hh:mm:mm」を「hh:mm:ss」に変更するだけです

void noheatmode::setCurrentTime()
{
    QTime time = QTime::currentTime();
    QString sTime = time.toString("hh:mm:ss");
    ui->tempTimeNoHeatMode->append(sTime);
}
于 2015-06-11T16:46:56.457 に答える