Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • 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
Qt 6.12 LTS is out! See what's new in the release blog

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 6.3k 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.
  • ? A Former User

    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.

    ? 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.

      ? Offline
      ? Offline
      A Former User
      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
      • ? A Former User

        @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.

        ? 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.

          ? Offline
          ? Offline
          A Former User
          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
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #11

              yes, exactly

              mrjjM 1 Reply Last reply
              0
              • ? A Former User

                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.

                ? 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.

                  ? Offline
                  ? Offline
                  A Former User
                  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
                  • ? A Former User

                    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.

                    ? 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.

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #15

                      @mrjj I will try

                      mrjjM 1 Reply Last reply
                      0
                      • ? A Former User

                        @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
                        • Unsolved