Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. <Solved>to get current time to display in a text edit box.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cdbean04
    wrote on 10 Jun 2015, 18:38 last edited by cdbean04
    #1

    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.
    code/
    #include "heatmode.h"
    #include "ui_heatmode.h"
    #include "wiringPi.h"
    #include <QTime>
    #include <QTextEdit>

    heatmode::heatmode(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::heatmode)
    {
    ui->setupUi(this);
    }

    heatmode::~heatmode()
    {
    delete ui;
    }

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

    //display time every 5 seconds
    ui->tempTimeBoxHeatMode->QTextEdit::setText(time);
    

    }
    /code

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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Jun 2015, 18:59 last edited by
      #2

      Hi,

      QTime::toString is what you are looking for

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • C Offline
        C Offline
        cdbean04
        wrote on 10 Jun 2015, 20:26 last edited by
        #3

        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.
        ex.
        currentTime
        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;
        do
        {
        QTime time = QTime::currentTime();
        QString sTime = time.toString("hh:mm:mm");
        ui->tempTimeNoHeatMode->append(sTime);
        delay(5000);
        
         i--;
        }while(i>0);
        

        }

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 10 Jun 2015, 20:28 last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cdbean04
            wrote on 10 Jun 2015, 20:42 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 10 Jun 2015, 20:44 last edited by
              #6

              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 ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                cdbean04
                wrote on 12 Jun 2015, 14:01 last edited by
                #7

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

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 12 Jun 2015, 21:35 last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  1/8

                  10 Jun 2015, 18:38

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved