PDA

View Full Version : QNetworkAccessManager and QNetworkRequest not doing anything



AusSkiller
1st October 2013, 06:04
I'm having trouble using QNetworkAccessManager to download anything, I create the QNetworkRequest and pass it to QNetworkAccessManager::get, but nothing happens, the finished signal never calls the function I connected (onFinished), and I even created a while loop after calling QNetworkAccessManager::get that checks QNetworkReply::isFinished every 500ms, but it always returns false and the progress is always 0 of 0. I called QNetworkReply::error which returns NoError but nothing seems to be working :(. I also tried disabling my firewall and antivirus in case they were blocking traffic but that didn't change anything, and the QWebView I'm also using in the app works fine so it shouldn't be a problem with connecting to the internet.

Can anyone help me? I'm using Qt 5.1.1 and the url I'm using is just "http://google.com" which I figure shouldn't cause any problems.

Below is the code for the class I created to test out how this stuff works and which I'm having problems with, please ignore the poor design of the code, it's just testing code to see how things work so I have a better idea how to fit it into the application.


Class declaration:

class FileDownloader : public QObject
{
Q_OBJECT

public:
FileDownloader(QObject* parent = 0);
~FileDownloader();

void DownloadToFile(const QString& url, const QString& file);

public slots:
void onFinished(QNetworkReply* reply);

private:
QNetworkRequest* m_networkRequest;
QNetworkAccessManager* m_networkManager;
QNetworkReply* m_reply;
QString m_file;
};

Implementation:

FileDownloader::FileDownloader(QObject* parent)
: QObject(parent)
{
m_reply = NULL;
m_networkManager = new QNetworkAccessManager(this);
QObject::connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
}

FileDownloader::~FileDownloader()
{
delete m_networkManager;
}

void FileDownloader::DownloadToFile(const QString& url, const QString& file)
{
m_networkRequest = new QNetworkRequest(QUrl(url));
m_reply = m_networkManager->get(*m_networkRequest);
m_file = file;

while (m_reply->isFinished() == false)
{
Sleep(500);

char buffer[256];
qint64 done = 0;
qint64 total = 0;
m_reply->downloadProgress(done, total);
QNetworkReply::NetworkError error = m_reply->error();
sprintf_s(buffer, 256, "Data: %d/%d\n%s\n", done, total, m_reply->errorString().toStdString().c_str());
OutputDebugStringA(buffer);
}
}

void FileDownloader::onFinished(QNetworkReply* reply)
{
QFile outputFile = m_file;
outputFile.open(QFile::WriteOnly);
outputFile.write(reply->readAll());
outputFile.close();
}

wysota
1st October 2013, 07:15
and I even created a while loop after calling QNetworkAccessManager::get that checks QNetworkReply::isFinished every 500ms

That's exactly why it doesn't work. You have to return to the event loop for any Qt networking classes to start their operations.

AusSkiller
1st October 2013, 07:45
That's exactly why it doesn't work. You have to return to the event loop for any Qt networking classes to start their operations.
Originally it didn't have the while loop but I encountered the problem so I added it to try to figure out why it wasn't working. Commenting out the while loop doesn't change anything, it still doesn't work. Do I need to explicitly create an event loop though? I don't remember ever creating one but most of the examples I read didn't seem to create one either.

Added after 22 minutes:

Argh! Me and my silliness. I'm porting my app from using windows forms and managed C++/CLR to using straight C++ and Qt, and one of the differences is that the HttpWebRequest and HttpWebResponse stuff I was using before uses blocking calls, which was fine since I managed all the threads myself, but of course QNetworkAccessManager works asynchronously so I'd created the FileDownloader to make it easier to handle the async calls knowing that would be a problem. However in my rush to get it working I'd forgot to ensure the instance of the FileDownloader I was using would be persistent and instead just had it as a local variable which naturally fell out of scope and crapped up everything, changing it to static (just for testing purposes) makes it work properly.

Except now I've encountered a different problem with it, it doesn't seem to follow redirects automatically :(, is there a simple way to enable that or will I need to handle it all myself?

wysota
1st October 2013, 08:07
As far as I know you need to handle that yourself.

AusSkiller
1st October 2013, 08:33
As far as I know you need to handle that yourself.
Cool thanks.