Unsolved How to use QTimer in Qt Creator to get data from an external peripheral device for a specific time interval only?
-
@SHUBHAM-SINGH-RAO the question is: how do you fetch the data? in a loop? then QElapsedTimer could help you.
Or with signals? Then your SLOT() would stop the fetching.
As long as this is unclear, its hard to help.
-
also, how is that question different from: https://forum.qt.io/topic/96190/timer-problem ?
-
@aha_1980 i m giving a detailed explanation of my problem in this post
-
- this is no reason to create a new topic
- important information is still missing
-
Hi,
To add to @aha_1980, you can edit your post and add the missing information there. Click on the icon with the vertical three points.
-
@SGaist I have edited my present status in my Question..kindly look at it!
-
-
@SHUBHAM-SINGH-RAO there is no way outside the blockin method. the timeout has to be implemented within that method.
Sorry, but if you don't show some relevant code, thats all I can tell you.
Regards
-
@aha_1980 I am attaching a similar code to test concept
I have used network access manager but I want to take serial data instead.mainwindow.cpp
void MainWindow::on_pushButton_clicked()
{
ui->label_2->setText("Clear replies ");
if(value==0)
{
ui->label->setText("clicked");
value=1;
}
else if(value==1)
{ui->label->setText("unclicked"); value=0; } somefunction();
}
void MainWindow::somefunction()
{
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QEventLoop loop;
QTimer timer;timer.setSingleShot(true); connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); connect(manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit())); timer.start(10000); manager->get(QNetworkRequest(QUrl("http://www.google.com"))); loop.exec(); if (timer.isActive()) { ui->label_2->setText("Reply recieved"); } else ui->label_2->setText("No reply from client");
}
void MainWindow::on_pushButton_2_clicked()
{
if(value2==0)
{
ui->label_3->setText("clicked");
value2=1;
}
else if(value2==1)
{ui->label_3->setText("unclicked"); value2=0; }
}
mainwindow.ui
-
@SHUBHAM-SINGH-RAO said in How to use QTimer in Qt Creator to get data from an external peripheral device for a specific time interval only?:
timer.start(10000);
manager->get(QNetworkRequest(QUrl("http://www.google.com")));
loop.exec();Don't do this! Local event loops are advanced stuff, and can easily lead to problems.
Use QNetworkRequest as it is designed with signals&slots. In your timeout slot, you can close() or abort() the QNetworkReply, like described here: https://stackoverflow.com/questions/14535502/how-to-stop-qnetworkaccessmanager-from-getting-a-reply-c