0

サイトから画像を取得して QLabel に挿入する Qt プログラムを作成しています。リクエストを送信すると、画面がフリーズし、それ以上何も起こりません。私は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 に答える