QT Linux - QObject::connect & QHttp issue



>我有一个问题。我正在编写一个小应用程序,它将从网站获取图像并将其显示在 QT GUI 应用程序中。

我使用 QHttp 来做到这一点。如果我在 main 中执行它(在显示 GUI 之前),代码可以工作,但是当我尝试实现它时,当我单击按钮时代码将运行时,它不起作用。下面是一些代码:

downloader.h - 负责创建连接和保存图像的类

#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QHttp>
#include <QFile>
#include <QDebug>
#include <QDir>
class Downloader : public QObject
{
    Q_OBJECT
public:
    explicit Downloader(QObject *parent = 0);
    void getImageFromWeb(QString host, QString append);
signals:
public slots:
    void stateChanged(int state);
    void responseHeaderReceived(const QHttpResponseHeader &resp);
    void requestFinished(int id, bool error);
private:
    QHttp *http;
};
#endif // DOWNLOADER_H

下载器.cpp - 实现

添加机箱开关用于调试

#include "downloader.h"
#include <QApplication>
Downloader::Downloader(QObject *parent) :
    QObject(parent)
{
}
void Downloader::getImageFromWeb(QString host, QString append)
{
    http = new QHttp(this);
    connect(http, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
    qDebug() << "Connect 1";
    connect(http, SIGNAL(responseHeaderReceived(QHttpResponseHeader)), this, SLOT(responseHeaderReceived(QHttpResponseHeader)));
    qDebug() << "Connect 2";
    connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(requestFinished(int,bool)));
    qDebug() << "Connect 3";
    http->setHost(host);
    http->get(append);
}
void Downloader::stateChanged(int state)
{
    switch(state)
    {
    case 0:
        qDebug() << "Unconnected";
        break;
    case 1:
        qDebug() << "Hhost Lookup";
        break;
    case 2:
        qDebug() << "Connection";
        break;
    case 3:
        qDebug() << "Sending";
        break;
    case 4:
        qDebug() << "Reading";
        break;
    case 5:
        qDebug() << "Connect";
        break;
    case 6:
        qDebug() << "Closing";
        break;
    }
}
void Downloader::responseHeaderReceived(const QHttpResponseHeader &resp)
{
    qDebug() << "Size" << resp.contentLength();
    qDebug() << "Type" << resp.contentType();
    qDebug() << "Status" << resp.statusCode();
}
void Downloader::requestFinished(int id, bool error)
{
    if(error)
    {
        qDebug() << "ERROR!";
    }
    else
    {
        qDebug() << "OK";
        QFile *file = new QFile(QDir::currentPath() + "/image.png");
        if(file->open(QFile::Append))
        {
            file->write(http->readAll());
            file->flush();
            file->close();
        }
        delete file;
    }
}

main.cpp - 如果像这样实现,上面的代码可以正常工作

#include "mainwindow.h"
#include <QApplication>
#include <downloader.h>
int main(int argc, char *argv[])
{
    Downloader getImage;
    getImage.getImageFromWeb("servlet.dmi.dk", "/byvejr/servlet/byvejr?by=8000&tabel=dag3_9");
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

取而代之的是,我希望在按下程序中的按钮时获取图像,所以我尝试了这个:

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "downloader.h"
#include <QApplication>
#include <QDir>
void MainWindow::on_pushButton_clicked()
{
    Downloader getImage;
    getImage.getImageFromWeb("www.dmi.dk/", "/uploads/tx_dmidatastore/webservice/k/d/_/n/g/femdgn_dk.png");
}

这行不通。从调试器中,我得到:

Connect 1
Connect 2
Connect 3

它工作时(当它在main.cpp中实现时),调试器给我的东西如下:

Connect 1 
Connect 2 
Connect 3 
OK 
Connection 
Sending 
Reading 
Size 16282 
Type "image/png" 
Status 200 
Connect 
OK

所以我想这告诉我连接已经建立,但没有执行任何内容。

任何答案/建议不胜感激。

提前感谢!

看起来您的 getImage 对象在执行任何操作之前就被取消了作用域/破坏。尝试将下载器对象创建为 MainWindow 的成员,而不是在 on_pushButton_clicked() 函数中。

最新更新