Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved How can i add timer to my code?

    General and Desktop
    timer read chart
    2
    2
    165
    Loading More Posts
    • 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.
    • D
      deleted286 last edited by

      Hi everyone. There is a code script that read data from txt and create a chart. I want to add timer to it. In every 20 sec read the data and add it to chart. I also want to see timer on screen. Thanks for any suggestion

      #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 Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        Already asked and answered here: https://forum.qt.io/topic/122197

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 4
        • First post
          Last post