如何在另一个班级中显示下载进度?

时间:2014-03-02 15:06:46

标签: qt qt5

如何在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)));


}

(...)

DownloadProgress.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;
};

DownloadProgress.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很新。请原谅我的愚蠢:(

1 个答案:

答案 0 :(得分: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)));