2012-09-26 95 views
0

我正在写一个Qt程序从网站获取图像并插入到QLabel中。当我发送我的请求时,我的屏幕冻结,没有更多事情发生。 注意我是Qt中的新成员。Qt HTTP GET冻结屏幕

基于我对Qt的初步知识,它足以在下载完成时发送信号。

... 

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())); 
    QObject::connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedDownload(QNetworkReply*))); 
} 

void MapReader::setImage(QByteArray imageBytes) 
{ 
    QImage map; 
    ... 
} 

void MapReader::triggerDownload() 
{ 
    QUrl url("http://images.tsn.ca/images/stories/2012/09/26/terrydunfield_2035-430x298.jpg"); 
    QNetworkReply* reply = networkManager->get(QNetworkRequest(url)); 
    QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit())); 
} 

void MapReader::finishedDownload(QNetworkReply* reply) 
{ 
    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()); 
} 

回答

0

我认为有一些代码缺失可能会给我们一些线索。你有

QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit())); 

但我没有看到哪里定义了循环?听起来你正在运行一个额外的事件循环?

无论如何,你不需要那样。这应该是简单:

void MapReader::triggerDownload() 
{ 
    QUrl url("http://images.tsn.ca/images/stories/2012/09/26/terrydunfield_2035-430x298.jpg"); 
    QNetworkReply* reply = networkManager->get(QNetworkRequest(url)); 
    QObject::connect(reply, SIGNAL(finished()), this, SLOT(finishedDownload())); 
} 

void MapReader::finishedDownload() 
{ 
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender()); // sender() allows us to see who triggered this slot - in this case the QNetworkReply 

    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()); 
    reply->deleteLater(); 
} 

确保您已经定义finishedDownload()作为中的槽头文件

+0

我已经试过您的解决方案,但问题仍然存在。它恰好在我调用networkManager-> get(QNetworkRequest(url))时冻结。我不认为这个信息是否有用。我在当地的大学网络,我正在使用他们的互联网接入来做到这一点。我们没有任何内容被封锁。因为我在本地网络上,所以我无法做到这一点? – learner

+0

我认为它必须与您的网络相关,因为我只是在这里尝试了这个确切的代码,它工作正常。我会建议运行Qt提供的HTTP示例[http://qt-project.org/doc/qt-4.8/network-http.html](http://qt-project.org/doc/qt-4.8/网络-http.html),看看是否可行。另请参阅qDebug()输出以获取线索。 – docsteer

+0

docster感谢您的帮助。问题仍然在VS2010上。我的版本是终极版。我的假设是Qt Addin中的一个bug。我已经运行这个代码,没有任何修改QtCreator和工作很好。我不知道,因为这种行为发生。所以,我会使用QtCreator。 – learner