0

我正在编写一个 Qt 程序来从站点获取图像并插入 QLabel。当我发送我的请求时,我的屏幕冻结了,没有任何事情发生。请注意,我是 Qt 的新手。

根据我对 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());
}
4

1 回答 1

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() 定义为头文件中的插槽

于 2012-09-26T21:43:39.310 回答