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. Updating label content every 2 minutes

Updating label content every 2 minutes

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 4.1k Views 1 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.
  • N Offline
    N Offline
    nadales56
    wrote on last edited by aha_1980
    #1

    Hi!!!
    I'm a newbie in QT Creator. I would like to implement a very simple graphical interface based on a Label and a pushButtom.
    When pressing the button in the label the content of a txt file appears.
    The thing is that this same file will be changing every 2 minutes, so I would need that automatically and without pressing the button again, in the label this new content will appear, the change does not have to be done simultaneously.
    What function or functions should I turn to?
    Thanks in advance.

    JonBJ 1 Reply Last reply
    0
    • N nadales56

      Hi!!!
      I'm a newbie in QT Creator. I would like to implement a very simple graphical interface based on a Label and a pushButtom.
      When pressing the button in the label the content of a txt file appears.
      The thing is that this same file will be changing every 2 minutes, so I would need that automatically and without pressing the button again, in the label this new content will appear, the change does not have to be done simultaneously.
      What function or functions should I turn to?
      Thanks in advance.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @nadales56
      You will use https://doc.qt.io/qt-5/qtimer.html to set up a timer that repeatedly ticks every two minutes, and have its slot action update the text on the label. You will use https://doc.qt.io/qt-5/qiodevice.html#readAll to read the file's content. See how you go from there.

      If you later wish to be "clever", you might use https://doc.qt.io/qt-5/qfilesystemwatcher.html to watch the file for changes to its content and only re-read at that point, rather than every two minutes regardless.

      1 Reply Last reply
      3
      • N Offline
        N Offline
        nadales56
        wrote on last edited by
        #3

        Thank you JonB!
        I have just make the text file content in the label!! =)

        I have some doubts about where or when should i use qtimer. I mean, first i clicked the button, so the information appear in the label, then i have to use qtimer to update the text automatically without clicking the buttom?

        mrjjM 1 Reply Last reply
        0
        • N nadales56

          Thank you JonB!
          I have just make the text file content in the label!! =)

          I have some doubts about where or when should i use qtimer. I mean, first i clicked the button, so the information appear in the label, then i have to use qtimer to update the text automatically without clicking the buttom?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @nadales56
          Hi
          yes, setting up timer will call your slot after the time you set.
          In that way, the timer can do what button does automatically.

          1 Reply Last reply
          2
          • N Offline
            N Offline
            nadales56
            wrote on last edited by
            #5

            So, in the mainwindow i have put something like:

            while(1==1){
            -> i call on_butttom_clicked
            -> i define the timer in 2 minuts

            } //kind of infinite while

            ?

            mrjjM 1 Reply Last reply
            0
            • N nadales56

              So, in the mainwindow i have put something like:

              while(1==1){
              -> i call on_butttom_clicked
              -> i define the timer in 2 minuts

              } //kind of infinite while

              ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @nadales56
              hi
              No, no infinite loop. That will kill the whole app.
              Use the QTimer. Then no loop is needed. Timer is the loop so to speak.

              1 Reply Last reply
              4
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi
                Do like this

                class MainWindow : public QMainWindow
                {
                Q_OBJECT
                QTimer *timer; // NEW
                public:
                void readFile(); // NEW to read the file and set to label
                ....
                private slots:
                void TimerSlot(); // NEW slot
                private:
                    Ui::MainWindow *ui;
                };
                
                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                    timer = new QTimer(this); // create it
                    connect(timer, &QTimer::timeout, this, &MainWindow::TimerSlot ); // connect it
                }
                
                void MainWindow::readFile()
                {
                    // QFile readALL() etc and set to ui->label
                }
                
                void MainWindow::on_pushButton_released()
                {
                readFile(); // read file
                timer->start(120000); // 2 mins timer
                }
                
                void MainWindow::TimerSlot()
                {
                    readFile(); // read file again
                }
                
                
                1 Reply Last reply
                4
                • N Offline
                  N Offline
                  nadales56
                  wrote on last edited by
                  #8

                  @mrjj
                  Thank you so much for your help, i really appreciate it! =)
                  It Works really well, as i want to be worked!

                  mrjjM 1 Reply Last reply
                  1
                  • N nadales56

                    @mrjj
                    Thank you so much for your help, i really appreciate it! =)
                    It Works really well, as i want to be worked!

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @nadales56
                    Hi
                    Np
                    Please dont forget to mark it as solved then :)

                    1 Reply Last reply
                    0

                    • Login

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