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. How can i use 2 QTimer? One is for counting and the other one is displaying
Forum Updated to NodeBB v4.3 + New Features

How can i use 2 QTimer? One is for counting and the other one is displaying

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 2.4k 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.
  • ? This user is from outside of this forum
    ? This user is from outside of this forum
    Guest
    wrote on last edited by koahnig
    #1

    I want to Timer with c++ usin qt. I have one timer in my code, but the time display on the screen is slow. timer -> start(500) must update every 20 seconds i guess.

    I want to use 2 timer for it. One is for update and count the time, the other timer will display on the screen. How can i do that, I would be very happy if you can help. Here is my code for it

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QTimer>
    #include <QPushButton>
    
    
    QTimer *timer = new QTimer();
    
     QTimer *timer2 = new QTimer();
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    
     // QTimer *timer = new QTimer(this);
      //timer->start(5);
    
        connect(timer,
                SIGNAL(timeout()),
                this,
                SLOT(functionstart())
                    );
    
    }
    
    void MainWindow::functionStart()
    {
    
    int count;   
    count = ui->lcdNumber->value();
    count++;
    ui->lcdNumber->display(count);
    }
    
    MainWindow::~MainWindow()
    {
       delete ui;
    }
    void MainWindow::on_pushButton_clicked()
    {
     if(timer->isActive()){
        timer->stop();
     ui->pushButton->setText("Start");
     }
     else {
        timer->start(500); 
         ui->pushButton->setText("Stop");
        
    }
    }
    

    [edit koahnig: code tags corrected]

    K 1 Reply Last reply
    0
    • ? Guest

      I want to Timer with c++ usin qt. I have one timer in my code, but the time display on the screen is slow. timer -> start(500) must update every 20 seconds i guess.

      I want to use 2 timer for it. One is for update and count the time, the other timer will display on the screen. How can i do that, I would be very happy if you can help. Here is my code for it

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QTimer>
      #include <QPushButton>
      
      
      QTimer *timer = new QTimer();
      
       QTimer *timer2 = new QTimer();
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
      
       // QTimer *timer = new QTimer(this);
        //timer->start(5);
      
          connect(timer,
                  SIGNAL(timeout()),
                  this,
                  SLOT(functionstart())
                      );
      
      }
      
      void MainWindow::functionStart()
      {
      
      int count;   
      count = ui->lcdNumber->value();
      count++;
      ui->lcdNumber->display(count);
      }
      
      MainWindow::~MainWindow()
      {
         delete ui;
      }
      void MainWindow::on_pushButton_clicked()
      {
       if(timer->isActive()){
          timer->stop();
       ui->pushButton->setText("Start");
       }
       else {
          timer->start(500); 
           ui->pushButton->setText("Stop");
          
      }
      }
      

      [edit koahnig: code tags corrected]

      K Offline
      K Offline
      koahnig
      wrote on last edited by koahnig
      #2

      Guest said in How can i use 2 QTimer? One is for counting and the other one is displaying:

      I want to Timer with c++ usin qt. I have one timer in my code, but the time display on the screen is slow. timer -> start(500) must update every 20 seconds i guess.

      I want to use 2 timer for it. One is for update and count the time, the other timer will display on the screen. How can i do that, I would be very happy if you can help. Here is my code for it

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QTimer>
      #include <QPushButton>
      
      
      QTimer *timer = new QTimer();
      
       QTimer *timer2 = new QTimer();
      
      int Count = 0;
      

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

      // QTimer *timer = new QTimer(this);
      //timer->start(5);

      connect(timer,
              SIGNAL(timeout()),
              this,
              SLOT(functionstart())
                  );
      
      connect(timer2,
                  SIGNAL(timeout()),
                  this,
                  SLOT(functionstart2())
                      );
      
      timer2->start(20000);
      }
      
      void MainWindow::functionStart()
      {
      
      // int count;   
      // count = ui->lcdNumber->value();
      Count++;
       }
      
      
      void MainWindow::functionStart2()
       {
       ui->lcdNumber->display(Count);
       }
      

      MainWindow::~MainWindow()
      {
      delete ui;
      }
      void MainWindow::on_pushButton_clicked()
      {
      if(timer->isActive()){
      timer->stop();
      ui->pushButton->setText("Start");
      }
      else {
      timer->start(500);
      ui->pushButton->setText("Stop");

      }
      }

      
      With minimal changes that should work in your case. 
      
      Note: Not tested brain to keyboard only ;)

      Vote the answer(s) that helped you to solve your issue(s)

      D 1 Reply Last reply
      2
      • K koahnig

        Guest said in How can i use 2 QTimer? One is for counting and the other one is displaying:

        I want to Timer with c++ usin qt. I have one timer in my code, but the time display on the screen is slow. timer -> start(500) must update every 20 seconds i guess.

        I want to use 2 timer for it. One is for update and count the time, the other timer will display on the screen. How can i do that, I would be very happy if you can help. Here is my code for it

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QTimer>
        #include <QPushButton>
        
        
        QTimer *timer = new QTimer();
        
         QTimer *timer2 = new QTimer();
        
        int Count = 0;
        

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

        // QTimer *timer = new QTimer(this);
        //timer->start(5);

        connect(timer,
                SIGNAL(timeout()),
                this,
                SLOT(functionstart())
                    );
        
        connect(timer2,
                    SIGNAL(timeout()),
                    this,
                    SLOT(functionstart2())
                        );
        
        timer2->start(20000);
        }
        
        void MainWindow::functionStart()
        {
        
        // int count;   
        // count = ui->lcdNumber->value();
        Count++;
         }
        
        
        void MainWindow::functionStart2()
         {
         ui->lcdNumber->display(Count);
         }
        

        MainWindow::~MainWindow()
        {
        delete ui;
        }
        void MainWindow::on_pushButton_clicked()
        {
        if(timer->isActive()){
        timer->stop();
        ui->pushButton->setText("Start");
        }
        else {
        timer->start(500);
        ui->pushButton->setText("Stop");

        }
        }

        
        With minimal changes that should work in your case. 
        
        Note: Not tested brain to keyboard only ;)
        D Offline
        D Offline
        deleted286
        wrote on last edited by deleted286
        #3

        @koahnig im new on this platform and qt. Sorry for my mistakes. I run the code but when i clicked the start it didnt count :(

        K 1 Reply Last reply
        0
        • D deleted286

          @koahnig im new on this platform and qt. Sorry for my mistakes. I run the code but when i clicked the start it didnt count :(

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @suslucoder

          You would need to publish all files.

          Probably your push button is not connected to the slot function.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deleted286
            wrote on last edited by
            #5

            I've solved to problem. Thanks a lot. What if i want to connect my timer to a chart, which is read the data from txt file and according to timer create a chart

            mrjjM 1 Reply Last reply
            0
            • D deleted286

              I've solved to problem. Thanks a lot. What if i want to connect my timer to a chart, which is read the data from txt file and according to timer create a chart

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

              @suslucoder
              Hi
              You dont really connect the timer to a chart.
              You connect it to a slot.
              In that slot you can access a chart you have on your main window to add new data for it to plot.

              So the plan is to only read a line from the file each timer triggers ?
              Im asking as normally one would read whole file and plot at once so i
              wonder what the goal/task is.

              D 1 Reply Last reply
              3
              • mrjjM mrjj

                @suslucoder
                Hi
                You dont really connect the timer to a chart.
                You connect it to a slot.
                In that slot you can access a chart you have on your main window to add new data for it to plot.

                So the plan is to only read a line from the file each timer triggers ?
                Im asking as normally one would read whole file and plot at once so i
                wonder what the goal/task is.

                D Offline
                D Offline
                deleted286
                wrote on last edited by
                #7

                @mrjj actually i want to a real time chart. Datas can read at once. But they should add into chart in a time interval. For example, in every sec, add 20 data on chart. On, in 20 sec add 20 data to the chart. It should be real time

                mrjjM 1 Reply Last reply
                0
                • D deleted286

                  @mrjj actually i want to a real time chart. Datas can read at once. But they should add into chart in a time interval. For example, in every sec, add 20 data on chart. On, in 20 sec add 20 data to the chart. It should be real time

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

                  @suslucoder
                  Hi
                  How big is this file ?

                  You could read it to a StringList and then use a variable to keep track of how far into the list you are
                  so each time , timer triggers, then you take next 20 from list and give to the chart.

                  D 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @suslucoder
                    Hi
                    How big is this file ?

                    You could read it to a StringList and then use a variable to keep track of how far into the list you are
                    so each time , timer triggers, then you take next 20 from list and give to the chart.

                    D Offline
                    D Offline
                    deleted286
                    wrote on last edited by
                    #9

                    @mrjj it is not so big. It has 20 or 40 line at all. Here is my reading file and creating a chart code. I have a timer but it is another project. I cannot create a connection between them :(

                    #include <QtWidgets/QApplication>
                    #include <QtWidgets/QMainWindow>
                    #include <QtCharts/QChartView>
                    #include <QtCharts/QLineSeries>
                    #include <QtCore/QDateTime>
                    #include <QtCharts/QDateTimeAxis>
                    #include <QtCore/QFile>
                    #include <QtCore/QTextStream>
                    #include <QtCore/QDebug>
                    #include <QtCharts/QValueAxis>
                    #include <QQueue>
                    
                    QT_CHARTS_USE_NAMESPACE
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);  //QApplication ana olay döngüsünü içeren bir QT sınıfıdır. Bu kodu çağırdığımızda, yapıcısını argc ve argv olarak çağırıp
                                                     //yeni bir nesne oluşturuyoruz.
                    
                    
                        QLineSeries *series = new QLineSeries(); //
                    
                    
                    
                        QFile sunSpots(":sun");
                        if (!sunSpots.open(QIODevice::ReadOnly | QIODevice::Text)) {
                            return 1;
                        }
                    
                        QTextStream stream(&sunSpots);
                        while (!stream.atEnd()) {
                            QString line = stream.readLine(); //satır satır okuma yap
                            if (line.startsWith("#") || line.startsWith(":")) //# veya : ile başlayıyorsa devam et, hiçbir şey yapma
                                continue;
                            QStringList values = line.split(QLatin1Char(' '), Qt::SkipEmptyParts); //boşlukları ayırıyor ve atıyor
                            QDateTime momentInTime;
                            momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15)); //?
                            qDebug()<< momentInTime;
                            series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());
                        }
                        sunSpots.close(); //açtığımız txt dosyasını kapat
                    
                    
                        QChart *chart = new QChart(); //bir chart oluşturabilmek için QChart sınıfından bir instace oluşturuyoruz.
                        chart->addSeries(series); //dosyadan okuduğumuz verileri chart'a ekliyoruz.
                        chart->legend()->hide();  //show ile de olur, set,
                        chart->setTitle("Sunspots count (by Space Weather Prediction Center)");
                    
                    
                    
                      QDateTimeAxis *axisX = new QDateTimeAxis; //eksenleri ayarlıyoruz. QLineSeries
                      axisX->setTickCount(10); //x ekseninde gösterilecek veri sayısı.
                      axisX->setFormat("MMM yyyy"); //format ay ve yıl olacak.
                      axisX->setTitleText("Date"); //x ekseninin altndaki başlık date olacak.
                      chart->addAxis(axisX, Qt::AlignBottom);
                     series->attachAxis(axisX); //seriye eklediğimiz bilgileri x eksenine koyuyoruz.
                    
                     QValueAxis *axisY = new QValueAxis;
                     axisY->setLabelFormat("%i");
                      axisY->setTitleText("Sunspots count");
                      chart->addAxis(axisY, Qt::AlignLeft);
                     series->attachAxis(axisY);
                    
                       QChartView *chartView = new QChartView(chart);
                      chartView->setRenderHint(QPainter::Antialiasing);
                    
                    
                    
                      QMainWindow window;
                        window.setCentralWidget(chartView);
                       window.resize(820, 600);
                       window.show();
                    
                    
                        return a.exec();
                    }
                    
                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi
                      Well you already read the file and add the data.
                      in
                      series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());

                      so instead of this, you want to do it in steps ?

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        deleted286
                        wrote on last edited by
                        #11

                        yes, exactly

                        mrjjM 1 Reply Last reply
                        0
                        • D deleted286

                          yes, exactly

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

                          @suslucoder
                          Hi
                          Well then instead of appending to the series object,
                          then make a list and add to that list instead.
                          So we read file to a list.
                          do you know std::vector ?

                          Then make a new timer and connect to a lambda slot .
                          in the slot, you call series->append to add the data in steps.
                          make sure you dont run out of list end.

                          D 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @suslucoder
                            Hi
                            Well then instead of appending to the series object,
                            then make a list and add to that list instead.
                            So we read file to a list.
                            do you know std::vector ?

                            Then make a new timer and connect to a lambda slot .
                            in the slot, you call series->append to add the data in steps.
                            make sure you dont run out of list end.

                            D Offline
                            D Offline
                            deleted286
                            wrote on last edited by
                            #13

                            do you know std::vector ?
                            I dont know but im trying to do this.
                            Thank you

                            mrjjM 1 Reply Last reply
                            0
                            • D deleted286

                              do you know std::vector ?
                              I dont know but im trying to do this.
                              Thank you

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

                              @suslucoder
                              Hi
                              well i was thinking something like

                              struct Data {
                              qint64 timestamp;
                              double value;
                              };
                              
                              std::vector<Data> thelist;
                              

                              would allow you to store what you now add directly.
                              i mean this line
                              series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());

                              Im only guessing since you seems to read timestamp from file too. but not sure what you do with momentInTime variable.

                              Once in a list, you can take data from that and add step by step.

                              D 1 Reply Last reply
                              0
                              • mrjjM mrjj

                                @suslucoder
                                Hi
                                well i was thinking something like

                                struct Data {
                                qint64 timestamp;
                                double value;
                                };
                                
                                std::vector<Data> thelist;
                                

                                would allow you to store what you now add directly.
                                i mean this line
                                series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());

                                Im only guessing since you seems to read timestamp from file too. but not sure what you do with momentInTime variable.

                                Once in a list, you can take data from that and add step by step.

                                D Offline
                                D Offline
                                deleted286
                                wrote on last edited by
                                #15

                                @mrjj I will try

                                mrjjM 1 Reply Last reply
                                0
                                • D deleted286

                                  @mrjj I will try

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

                                  @suslucoder
                                  Ok so instead fo this line
                                  series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());
                                  you do
                                  Data tmp;
                                  tmp.timestamp = momentInTime.toMSecsSinceEpoch();
                                  tmp.value = values[2].toDouble();
                                  thelist.push_back(tmp);

                                  To store in list.

                                  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