【问题标题】:How to show download progress in another class?如何在另一个班级显示下载进度?
【发布时间】:2014-03-02 15:06:45
【问题描述】:

如何在DownloadProgress 窗口的“选项”窗口中显示下载文件的下载进度?

Options.h:

class Options : public QDialog
{
    Q_OBJECT
    QScopedPointer<QNetworkAccessManager> nam;
public:
    explicit Options(QWidget *parent = 0);
    ~Options();

QNetworkReply *red;
    private slots:
    void writeData();
    void downloadFinished();


private:
    Ui::Options *ui;

public:

    QString getResolution(int width, int height);
};

Options.cpp

(...)

void Options::on_toggleGDownload_clicked()
{

QString rep;

// shows download progress
DownloadProgress n;
n.exec();

QFile dwnl("somepath");
dwnl.open(QIODevice::WriteOnly);

QNetworkAccessManager *nam = new QNetworkAccessManager(this);
red = nam->get(QNetworkRequest(QUrl("someurl")));
connect(red, SIGNAL(readyRead()), this, SLOT(writeData()));
connect(red, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDownloadProgress(qint64, qint64)));


}

(...)

下载进度.h:

class DownloadProgress;
}

class DownloadProgress : public QDialog
{
Q_OBJECT

public:
   explicit DownloadProgress(QWidget *parent = 0);
   ~DownloadProgress();

public slots:
    void updateDownloadProgress(qint64, qint64);

private:
    Ui::DownloadProgress *ui;
};

下载进度.cpp:

(...)

DownloadProgress::DownloadProgress(QWidget *parent) :
QDialog(parent),
ui(new Ui::DownloadProgress)
{
 ui->setupUi(this);

}

DownloadProgress::~DownloadProgress()
{
delete ui;
}

void DownloadProgress::updateDownloadProgress(qint64 readBytes, qint64 totalBytes)
{
ui->progressBar->setMaximum(totalBytes);
ui->progressBar->setValue(readBytes);

}

我对 Qt5 很陌生。请原谅我的愚蠢:(

【问题讨论】:

    标签: qt qt5


    【解决方案1】:

    变化:

    DownloadProgress n;
    

    DownloadProgress* n(new DownloadProgress(this));
    

    connect(red, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDownloadProgress(qint64, qint64)));
    

    连接

    connect(red, SIGNAL(downloadProgress(qint64,qint64)), n, SLOT(updateDownloadProgress(qint64, qint64)));
    

    【讨论】:

    • 好吧,在我问这个问题之前我尝试了你的解决方案,它没有奏效。 (没有匹配函数调用'Options::connect(QNetworkReply*&, const char [33], DownloadProgress&, const char [40])' connect(red, SIGNAL(downloadProgress(qint64,qint64)), n, SLOT( updateDownloadProgress(qint64, qint64)))
    • @user3343366:您似乎没有为 DownloadProgress 提供连接指针。您需要在堆上创建它,否则它会过早地消失,因为它将异步运行。
    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    相关资源
    最近更新 更多