Qt HTTP获取冻结屏幕
我正在编写一个Qt程序,从站点获取一个图像并插入QLabel。当我发送请求时,我的屏幕冻结,不再发生任何事情。 注意,我是新来的 根据我对Qt的初步了解,下载完成后发送信号就足够了Qt HTTP获取冻结屏幕,qt,http,networking,request,Qt,Http,Networking,Request,我正在编写一个Qt程序,从站点获取一个图像并插入QLabel。当我发送请求时,我的屏幕冻结,不再发生任何事情。 注意,我是新来的 根据我对Qt的初步了解,下载完成后发送信号就足够了 ... MapReader::MapReader(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { ui.setupUi(this); imageLabelMap = ui.imageMap; getI
...
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());
}
。。。
MapReader::MapReader(QWidget*父,Qt::WFlags标志)
:QMainWindow(父项、标志)
{
ui.setupUi(这个);
imageLabelMap=ui.imageMap;
getImageButton=ui.getImageButton;
networkManager=新的QNetworkAccessManager(此);
设置();
}
MapReader::~MapReader()
{
}
void MapReader::setup()
{
连接(getImageButton,信号(clicked()),这个,插槽(triggerDownload());
QObject::connect(网络管理器,信号(完成(QNetworkReply*)),此,插槽(完成下载(QNetworkReply*));
}
void MapReader::setImage(QByteArray imageBytes)
{
齐玛格地图;
...
}
void MapReader::triggerDownload()
{
QUrl url(“http://images.tsn.ca/images/stories/2012/09/26/terrydunfield_2035-430x298.jpg");
QNetworkReply*reply=networkManager->get(QNetworkRequest(url));
连接(应答、信号(readyRead())、循环、插槽(quit());
}
void MapReader::finishedDownload(QNetworkReply*reply)
{
回复->删除稍后();
QVariant statusCodeV=回复->属性(QNetworkRequest::HttpStatusCodeAttribute);
QVariant redirectionTargetUrl=回复->属性(QNetworkRequest::RedirectionTargetAttribute);
如果(回复->错误()!=QNetworkReply::无错误)
{
QMessageBox-msgBox;
msgBox.setWindowTitle(“错误”);
msgBox.setInformativeText(“下载文件时出错:\n”+reply->errorString());
msgBox.exec();
返回;
}
QVariant属性=回复->属性(QNetworkRequest::RedirectionTargetAttribute);
if(attribute.isValid())
{
QUrl url=attribute.toUrl();
qDebug()我认为缺少一些代码,可能会给我们提供线索。您已经
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();
}
void MapReader::triggerDownload()
{
QUrl url(“http://images.tsn.ca/images/stories/2012/09/26/terrydunfield_2035-430x298.jpg");
QNetworkReply*reply=networkManager->get(QNetworkRequest(url));
连接(应答,信号(finished()),此,插槽(finishedDownload());
}
void MapReader::finishedDownload()
{
QNetworkReply*reply=qobject_cast(sender());//sender()允许我们查看是谁触发了此插槽-在本例中是QNetworkReply
QVariant statusCodeV=回复->属性(QNetworkRequest::HttpStatusCodeAttribute);
QVariant redirectionTargetUrl=回复->属性(QNetworkRequest::RedirectionTargetAttribute);
如果(回复->错误()!=QNetworkReply::无错误)
{
QMessageBox-msgBox;
msgBox.setWindowTitle(“错误”);
msgBox.setInformativeText(“下载文件时出错:\n”+reply->errorString());
msgBox.exec();
返回;
}
QVariant属性=回复->属性(QNetworkRequest::RedirectionTargetAttribute);
if(attribute.isValid())
{
QUrl url=attribute.toUrl();
qDebug()deleteLater();
}
请确保已将finishedDownload()定义为头文件中的插槽我已尝试了您的解决方案,但问题仍然存在。当我调用networkManager->get(QNetworkRequest(url))时,它将完全冻结。我不认为这些信息是否有用。我在本地大学网络上,我正在使用他们的internet访问来执行此操作。我们没有任何内容被阻止。因为我在本地网络上,所以我无法执行此操作?我认为它一定与您的网络相关,因为我刚刚在这里尝试了这段代码,效果很好。我建议运行HTTP示例用Qt测试d,看看这是否有效输出线索。docster感谢您的帮助。问题仍然存在于VS2010上。我的版本是最终版本。我的假设是Qt Addin中存在错误。我在QtCreator上运行了此代码,没有任何修改,工作非常正常。我不知道,因为会发生这种行为。因此,我将使用QtCreator。