How to use QTimer in Qt Creator to get data from an external peripheral device for a specific time interval only?
-
Aim : I m working on Qt Creator and want to interface Modem with my main programme For that, I need fetch the data from modem for a specific time interval (say 3 seconds) only and after that I have to stop receiving data.
Present Status 05/11/18 :
I want to execute a blocking method through which I can wait (say 3 seconds) for data . If I receive data in 3 seconds, I will move ahead for some another task else I will quit. Is there any method except threading?
I am trying to do it with QEventLoop but no success achieved so far.
Kindly suggest any helpful method or any relevant example?My Work: I tried to implement it using
QTimer::singleShot(3000,this,SLOT(SLOT1()))
But the limitation with it: It calls the SLOT1 after 3000 msec, but this is not what I want.
My requirement is to use timer in order to fetch data for 3 seconds only and then stop.
@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 ?
-
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
-
@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!
-
- this is no reason to create a new topic
- important information is still missing
-
@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
-
@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
-
@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