PDA

View Full Version : Help me qtime in qt?



tamnv110
15th June 2011, 09:56
I'm sorry, my level language not good, i'm vietnamese. And i used google translate.
Current time is me try to write a program, it will allow connection to a website, but I want to time limit connection to that website. If that is not in 5s received data return the program will show message box "not conncect website".
not know not start from where. Help me.

Tamnv110. from Viet Nam.

wysota
15th June 2011, 12:09
QNetworkAccessManager, QTimer, signals and slots.

tamnv110
15th June 2011, 12:56
Thank you!
Can I use signal timeout in Qtimer and slot finished in QNetworkAccessManager ?

wysota
15th June 2011, 13:16
Yes, you can.

tamnv110
16th June 2011, 11:02
hmmm! Dear wysota!
I not found signals and slots appropriate.
can you specify more for me?
Current, I have a function connect to URL:

QString KiemTraKetNoi::KTTruyenDuLieu(QString &str)
{
QNetworkAccessManager manager;
QNetworkReply *reply=manager.get(QNetworkRequest(QUrl(str)));
QEventLoop evLoop;
connect(reply,SIGNAL(readyRead()),&evLoop,SLOT(quit()));
connect(reply,SIGNAL(error(QNetworkReply::NetworkE rror)),&evLoop,SLOT(quit()));
evLoop.exec();
QString strDuLieu="";
qDebug()<<"kiemtraketnoi.cpp KTTruyenDuLieu Network Error"<<reply->error();
if(reply->error()==0)
{
strDuLieu=QString::fromUtf8(reply->readAll());
}
else
{
qDebug()<<"kiemtraketnoi.cpp KTTruyenDuLieu Network Error"<<reply->error();
strDuLieu="";
QMessageBox msgBox;
QPushButton *okButton = msgBox.addButton(QString::fromUtf8("OK"), QMessageBox::ActionRole);
QPushButton *noButton = msgBox.addButton(QString::fromUtf8("No"),QMessageBox::RejectRole);
QString msg = reply->errorString() + " Retry?";
msgBox.setText(msg);
msgBox.exec();
if(msgBox.clickedButton()== (QAbstractButton*)okButton)
{
KTTruyenDuLieu(str);
}
else if(msgBox.clickedButton() == (QAbstractButton*)noButton)
{
msgBox.close();
}
}
reply->deleteLater();
return strDuLieu;
}

I want to if time connect to website >5s, but application not connect to this web, application return messagebox "Not connect to webstie".

wysota
16th June 2011, 12:04
The two signals you mentioned earlier were the correct ones to use. timeout() signal of the timer will tell you 5 seconds have passed and the finished() signal of QNetworkAccessManager or QNetworkReply will tell you the file has been downloaded (or not).

tamnv110
16th June 2011, 12:55
I don't understand!
I have an idea:

QObject::Connect(time, SIGNAL(timeout()), this,SLOT(StopConnect()));

In StopConnect(), I try stop connecting to website, but I don't know use any func of qnetwork to stop connection.:(

wysota
16th June 2011, 12:59
There is QNetworkReply::abort().