Important: Please read the Qt Code of Conduct -

<Solved>to get current time to display in a text edit box.

  • I am trying to have a text edit box display a time stamp with the current time on the push of a button called start.. Also I want to put it in a loop so that it will create a running list of current times. Here is the code I have so far, clearly it is wrong.
    #include "heatmode.h"
    #include "ui_heatmode.h"
    #include "wiringPi.h"
    #include <QTime>
    #include <QTextEdit>

    heatmode::heatmode(QWidget *parent) :
    ui(new Ui::heatmode)

    delete ui;

    void heatmode::on_startButtonHeatMode_clicked()
    QTime time = QTime::currentTime();

    //display time every 5 seconds


    I have edited out other parts of code that are not relevant to the time.
    Thanks for your help.

  • Lifetime Qt Champion


    QTime::toString is what you are looking for

  • Thanks SGaist I got the current time to display, now I am having an issue getting it to keep populating. I am trying to get it to display the current time every 5 seconds, with it keeping a running list.
    currentTime 5 seconds later
    currentTime 5 seconds after that
    currentTime etc.
    But it is not working, I have tried several different types of loops to try and get the desired results, to no avail. Here is my current code.

    void noheatmode::on_timeButton_clicked()
    //Get Current time and convert it to a string

    int i = 30;
    QTime time = QTime::currentTime();
    QString sTime = time.toString("hh:mm:mm");


    I am trying to display it in a text edit box. Any ideas?

  • Lifetime Qt Champion

    You are blocking the main thread with your loop. Take a look at QTimer, there's even a clock example you can find in Qt's examples

  • After glancing over QTime I am not sure if it is what I am looking for. It is clearly a timer, what I ultimately am doing is taking the temperature of a heating device and outputting its temperature with a time stamp on it. I plan to check the temperature every few seconds, 5 seconds was just an arbitrary time i used. I do not have the heating device I am measuring off yet so I was just trying to get the time display working so later I can just add to temperature out put to it.

  • Lifetime Qt Champion

    No QTime, QTimer. If all you want to do is get a reading every five second, just connect the timeout signal of a QTimer to a slot that will simulate the reading and you're good to go, aren't you ?

  • Yep it works.
    Thanks again, you always come through SGaist.

  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

    Don't forget to up-vote the answers that helped you, that will also help other users to find them more quickly.

Log in to reply