QT GUI freezes when download starts
-
I'm trying to download an image and shows it in a QLabel. Of course my program has a GUI. After I pressed my Button to start the download my GUI freezes completely and I can't do anything. This behaviour occurs with any link.
The problem is occuring at this line:
@
QNetworkReply* reply = networkManager->get(QNetworkRequest(url));
@My work environment is VS2010 Ultimate. Qt version is 4.8.1.
Is there a problem to call this in UI thread?
Below all my code. Thanks for replies.@
#include "mapreader.h"
#include <QByteArray>
#include <QPixMap.h>
#include <QPainter>
#include <QImage>
#include <QDebug>MapReader::MapReader(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
imageLabelMap = ui.imageMap;
getImageButton = ui.getImageButton;
networkManager = new QNetworkAccessManager(this);
setup();
}MapReader::~MapReader()
{
}void MapReader::setup()
{
QObject::connect(getImageButton, SIGNAL(clicked()), this, SLOT(triggerDownload()));
}void MapReader::setImage(QByteArray imageBytes)
{
QPixmap pixelMap;
pixelMap.loadFromData(imageBytes);
imageLabelMap->setPixmap(pixelMap);
}void MapReader::triggerDownload()
{
QUrl url("http://f.i.uol.com.br/folha/mundo/images/12270854.jpeg");
QNetworkReply* reply = networkManager->get(QNetworkRequest(url));QObject::connect(reply, SIGNAL(finished()), this, SLOT(finishedDownload()));
}
void MapReader::finishedDownload()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
reply->deleteLater();QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); if(reply->error() != QNetworkReply::NoError) { QMessageBox msgBox; msgBox.setWindowTitle("Error"); msgBox.setInformativeText("Error on downloading file: \n"+reply->errorString()); msgBox.exec(); return; } QVariant attribute = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); if (attribute.isValid()) { QUrl url = attribute.toUrl(); qDebug() << "must go to:" << url; return; } setImage(reply->readAll());
}
@ -
I had a quick look at your code, but didn't find any obvious errors.
How did you find out that the error was occurring at the get() line? Also, is it freezing when constructing the QNetworkRequest, or when calling QNetworkAccessManager::get()?
-
In Qt4, standard constructors of QUrl have some caveats. Try using one of the static methods, like ::fromUserInput(). Also, you can try connecting the QNAM::finished() slot, instead of the reply itself. Apart from that, as JKSH said - no obvious errors visible at first sight.
-
Tried ::fromUIserInput(), the effect is the same. I've made a separate call from QNetwork request and commented QNetworkAccessManager::get(). No freeze.
When I use QNetworkAccessManager::get() the problem rises again.
I have no idea, what's going on -
Take a look at "QNAM documentation":http://qt-project.org/doc/qt-4.8/qnetworkaccessmanager.html, especially provided snippets. Try doing it how it's done there (connect with QNAM::finished or QNetworkReply::readyRead()). Might also be a good idea to check with Wireshark for actual network traffic.
-
Well, problem is really strange. I take your code, add missing header file and run it. It works correctly. At least I did not observe any freeze. I just use qDebug() to check steps of program execution. The difference is only in system. I used Linux.
-
When I did this call to get my parameters:
QNetworkConfiguration config = networkManager->activeConfiguration();
Values like bearerName, identifier are empty. Do I need to provide some values to my network configuration?
Guziemic, it's very strange. I agree. -
No, you shouldn't need to provide those values. Some websites will refuse to serve you if you don't provide a User-Agent value, but that doesn't seem to be the case for your site.
This is a strange problem; I don't think it's a coding error.
Some more questions:
- Can you double-check that you are indeed using Qt 4.8.1 for MSVC2010? The MSVC2008 or MinGW variants won't work with your compiler.
- Have you tried running your program in a debugger?
- Are you compiling a 32-bit or 64-bit executable?
-
Ok
- running on a 64bit machine;
- Qt 4.8.1 for MSVC 2010;
- I tried to debug in MSVC. It can't reach QNAM::get() method.
I'm now running the very same project on QtCreator. The screen doesn't freeze but the problem remains with the get() method.
Running wireshark I can see requests from QNetworkAccessManager. But any signal is sent on my program.
Very, very strange. -
[quote author="qtBeginner" date="1348846736"]- running on a 64bit machine;
[/quote]Running a 64bit machine isn't an issue. The question is, are you COMPILING a 64bit program?[quote author="qtBeginner" date="1348846736"]- I tried to debug in MSVC. It can't reach QNAM::get() method.[/quote]What do you mean "can't reach QNAM::get()"? What happens? What's the last thing it reaches?
[quote author="qtBeginner" date="1348846736"]But any signal is sent on my program.[/quote] Sorry, I don't understand this line. Could you please explain again?
-
Sorry, I'll clarify myself.
- I'm compilng a 32bit program;
- Debugging program, inserting a breakpoint in the line where QNAM::get() is, the program freezes;
- When I say signal, I mean the signal function inserted on QObject::connect.
Is this ok? Thanks for help
-
Fellows it works. I think this is a bug related to qt plugin for VS2010. Running the project with QtCreator the image was downloaded fine.
On Qt, screen was not freezing because I wasn't calling QObject::connect to my button.
Really, the very same code works on QtCreator, but not in VS2010.
Can someone explains how this occurs? -
You're probably right; it sounds like a bug with the VS2010 plugin. I've never encountered this before, so I don't know what's happening, sorry. It would be good to search if anyone else has had this problem, and if not, file a bug report at https://bugreports.qt-project.org
Check if the build type affects the freeze -- does it freeze with both Debug and Release builds?
Also, what happens if you launch your program by itself? Copy your .exe, and the required DLLs to a separate folder, and open the .exe from Windows Explorer
If you can't get it to work on VS2010, I can think of 2 options:
Develop this program on QtCreator
Upgrade to Qt 4.8.3