I want to block my program until finishing the download !
-
Hi . I am new in QT and I wrote a code for download file using QNetworkAccessManager , but I want to block my code until the download finishs.
this is my main :
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); MyClass* obj = new MyClass(); obj->downloadToQString(QUrl( "http://codeforces.com" )); if ( obj->error == false ) qDebug() << obj->result; else qDebug() << "Error : " <<obj->errorString; app.exec(); }
my request for download :
void downloadToQString(QUrl url) { error = false; errorString = ""; result = ""; reply = nam.get(QNetworkRequest(url)); QObject::connect(reply, SIGNAL(finished()), this, SLOT(onFinishedQString())); }
because download don't make block the program , it continues it's running and main prints empty string in output.
I know Threading is good but it will be hard for me to handle it.
-
Hello,
In your method downloadToQString, you connect your QNetworkReply* to a slot of your class MyClass. Why don't you print the result in this method ?
-
Hello,
In your method downloadToQString, you connect your QNetworkReply* to a slot of your class MyClass. Why don't you print the result in this method ?
@mistralegna Hi . I wrote this code just for testing downloading does work correctly or not.
I will use this class in GUI project not in console. first I must prevent user from doing anything before download completes and after knowing the result of download , decide what i have to do.
-
@mistralegna Hi . I wrote this code just for testing downloading does work correctly or not.
I will use this class in GUI project not in console. first I must prevent user from doing anything before download completes and after knowing the result of download , decide what i have to do.
-
@mistralegna Hi . I wrote this code just for testing downloading does work correctly or not.
I will use this class in GUI project not in console. first I must prevent user from doing anything before download completes and after knowing the result of download , decide what i have to do.
@bluemmb
Basically this is possible (e.g. using a "local" QEventLoop), but since you mentioned that you want to use it in GUI it would be way better usability/UX design if you show for example a modal dialog as long as the download isn't finished.You could use a QProgressDialog or even a custom QDialog subclass and set them modal. So the user knows that something is going on instead of a simply unresponding application.
-
@bluemmb
Basically this is possible (e.g. using a "local" QEventLoop), but since you mentioned that you want to use it in GUI it would be way better usability/UX design if you show for example a modal dialog as long as the download isn't finished.You could use a QProgressDialog or even a custom QDialog subclass and set them modal. So the user knows that something is going on instead of a simply unresponding application.
@raven-worx thanks , i have been doing what you have told.
But i really want to know how we can do it , is there any way at all ? -
@raven-worx thanks , i have been doing what you have told.
But i really want to know how we can do it , is there any way at all ?QNetworkReply* reply = nam->get( request ) ; QEventLoop loop; connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec();
-
QNetworkReply* reply = nam->get( request ) ; QEventLoop loop; connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec();