passing a current time variable

848 views Asked by At

I am trying to get a text edit box to display the current time every 5 seconds using QTimer. I am having the current time figured in a separate method and then having the QTimer call that method and display the current time. I can not for the life of me figure out how to pass the variable from the setCurrentTime method to the QTimer. Im sure it a really easy fix but I cant figure it out. Here is my code.

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);
}
2

There are 2 answers

1
Henrich Glaser-Opitz On BEST ANSWER

If I got your problem right, you just have minutes variable instead of a seconds. Just change "hh:mm:mm" to "hh:mm:ss"

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

With your code:

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

This means that the function within SLOT will be called every 5000 milliseconds which = 5 seconds. What could be done then is that you set your function setCurrentTime() to update your text box every time it is called.

Example:

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
}