Unsolved QT c++
-
Hi All,
using below code for downloading.string recBuff = Download(wpathURL.c_str()); Download(std::wstring szUrl){ connect(&manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(downloadFinished(QNetworkReply*))); QString url("http://guest:p2guest@192.168.55.3/contents/2/clip/08615L.xml"); QUrl urls(url); QNetworkRequest request(urls); manager.get(request); } downloadFinished(QNetworkReply *data) { QFile localFile("/home/roi/Desktop/downloadedfile.txt"); if (!localFile.open(QIODevice::WriteOnly)) return; const QByteArray sdata = data->readAll(); localFile.write(sdata); qDebug() << sdata; localFile.close(); }
issue is control does not goes to sloats downloadFinished.
file which i want to download is small and network is connected.
so please suggest me why it is not going to sloats.
Thanks
shashi[edit: added coding tags SGaist]
-
Hi,
Your code as is, won't even compile so it can't be accurately analysed for bugs.
You should also connect the error signal related signals to see if there's anything going wrong.
And please use the coding tags to enclose your code so it makes it more easily readable. It's three back ticks or use the last button that is just above the text edit area.
-
Hi
This code compiles properly.string recBuff = Download(wpathURL.c_str());
Download(std::wstring szUrl){
char data;
connect(&manager, SIGNAL(finished(QNetworkReply)),this, SLOT(downloadFinished(QNetworkReply*)));
QString url("http://guest:p2guest@192.168.55.3/contents/2/clip/08615L.xml");
QUrl urls(url);
QNetworkRequest request(urls);
manager.get(request);
return data;
}downloadFinished(QNetworkReply *data) {
QFile localFile("/home/roi/Desktop/downloadedfile.txt");
if (!localFile.open(QIODevice::WriteOnly))
return;
const QByteArray sdata = data->readAll();
localFile.write(sdata);
qDebug() << sdata;
localFile.close();}
please suggest now.
-
Maybe for you, but I don't see any class declaration nor any Q_OBJECT macro nor any return type for your functions and you haven't even added the error detection I suggested.
Your SIGNAL macro content is wrong thus your connection will fail at runtime. If using Qt 5, change for the new connection syntax and you'll have a build time error. -
Seems , This code is from the Qt examples. You have copied as it is in the example and did not make changes needed .
I am expecting all the code in Qt class with Q_OBJECT as it is in the sample.- you have copied the main() code simply in to your own function directly. Which is not preferable. You need some code like inside your function.
void MainWindow::startQtDownload(QString stg ) { QtDownload *dl = new QtDownload; dl->setTarget(stg); dl->download(); qDebug() << "Got clicked _2"; qDebug() << "MainWindow" << QObject::connect(dl, SIGNAL(done()), this, SLOT(closeit())); //This is Qt 4.8 way, better use Qt5 way connect() }
-
Also as Mr. @SGaist sugguested , the connect is not placed / coded properly. It not should be placed inside this function. Download(std::wstring szUrl) . Please be as it is in constructor of the QtDownlaod class.
-
Currently in that there is nothing to give error messages. Please make some code to look for errors, so that it would be easy to catch the errors.
Here I have attached small sample of your code and called the QtDownlaod class in MainWindow class. I made a zip , download it and use it as you like.
https://www.dropbox.com/s/k9cc3fcaadph8br/forumNetworkExample.zip?dl=0
Hopefully, this will solve your problem.