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(); 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]
-
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 ;)
-
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.