How can i use 2 QTimer? One is for counting and the other one is displaying
-
You would need to publish all files.
Probably your push button is not connected to the slot function.
-
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
-
@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. -
@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. -
@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(); }
-
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 ?
-
yes, exactly
-
@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. -
do you know std::vector ?
I dont know but im trying to do this.
Thank you -
@suslucoder
Hi
well i was thinking something likestruct 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.
-
@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.