QT 框架C++我如何从另一个类访问进度栏

QT Framework C++ how i access to progressBar from another class

本文关键字:访问 另一个 C++ 框架 QT      更新时间:2023-10-16

我在从另一个类访问进度栏时遇到问题。这是不起作用的方法。我在UI(mainwindow.ui)中有进度条,名称为进度条。

void QtDownload::downloadProgress(qint64 recieved, qint64 total) {
   ui->progressBar->setMaximum(total);  
   ui->progressBar->setValue(recieved);
}

以下是代码的所有部分:

downloader.pro

#-------------------------------------------------
#
# Project created by QtCreator 2017-01-12T15:22:17
#
#-------------------------------------------------
QT       += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = downloader
TEMPLATE = app

SOURCES += main.cpp
        mainwindow.cpp
HEADERS  += mainwindow.h
FORMS    += mainwindow.ui

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QObject>
#include <QString>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
};

class QtDownload : public QObject {
    Q_OBJECT
public:
    explicit QtDownload();
    QNetworkAccessManager manager;
    QString target;
    void setTarget(const QString& t);
private:

signals:
    void done();
public slots:
    void download();
    void downloadFinished(QNetworkReply* data);
    void downloadProgress(qint64 recieved, qint64 total);
};

主.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QtCore>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

主窗口.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QUrl>
    #include <QtNetwork/QNetworkRequest>
    #include <QFile>
    #include <QDebug>
    #include <QtNetwork/QNetworkReply>
    #include <QtNetwork/QNetworkReply>
    #include <QByteArray>
    #include <QObject>
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {

        ui->setupUi(this);

        //  QCoreApplication app(argc, argv);
        QtDownload *dl =  new QtDownload();
       dl->setTarget( "http://media09.vbox7.com/s/21/21bbc2dca3r3634e3389.mp4");
       dl->download();
          //quit when the download is done.
        //  QObject::connect(&dl, SIGNAL(done()), &app, SLOT(quit()));
    }

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

    QtDownload::QtDownload() : QObject(0) {
        QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(downloadFinished(QNetworkReply*)));
    }


    void QtDownload::setTarget(const QString &t) {
        this->target = t;
    }
    void QtDownload::downloadFinished(QNetworkReply *data) {
        QFile localFile("C:/downloadedfile.mp4");
        if (!localFile.open(QIODevice::WriteOnly))
            return;
        const QByteArray sdata = data->readAll();
        localFile.write(sdata);
        qDebug() << sdata;
        localFile.close();
        emit done();
    }
    void QtDownload::download() {
        QUrl url = QUrl::fromEncoded(this->target.toLocal8Bit());
        QNetworkRequest request(url);
        QObject::connect(manager.get(request), SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
    }
    void QtDownload::downloadProgress(qint64 recieved, qint64 total) {
        ui->progressBar->setMaximum(total);    // DOES NOT WORK
   ui->MainWindow.progressBar->setValue(recieved);  // DOES NOT WORK
    }

那是因为你的slot不应该在QtDownload里面,它应该在MainWindow里面。

您应该在其构造函数中传递MainWindow(就像父小部件一样),并在连接中重用此MainWindow,而不是this

QtDownload 中的编辑:

页眉

QtDownload(QObject* parent = 0); //Don't forget to make it in the source file
signals:
    void done();
    void downloadProgress(qint64 recieved, qint64 total);
public slots:
    void download();
    void downloadFinished(QNetworkReply* data);
private:
    MainWindow* parent; //Set it in the new constructor

QtDownload::QtDownload(QObject *parent) : QObject(parent)
{
    //The code that was initially inside QtDownload::QtDownload()
}
void QtDownload::download() {
    QUrl url = QUrl::fromEncoded(this->target.toLocal8Bit());
    QNetworkRequest request(url);
    QObject::connect(manager.get(request), SIGNAL(downloadProgress(qint64,qint64)), parent, SLOT(downloadProgress(qint64,qint64)));
}

在主窗口中进行编辑

页眉

public slots:
    void downloadProgress(qint64 recieved, qint64 total);

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //  QCoreApplication app(argc, argv); (Remove this line, it should be in the main.cpp
    QtDownload *dl =  new QtDownload(this); //Pass this as parameter
    dl->setTarget( "http://media09.vbox7.com/s/21/21bbc2dca3r3634e3389.mp4");
    dl->download();
    //quit when the download is done.
    //  QObject::connect(&dl, SIGNAL(done()), &app, SLOT(quit()));
}
void MainWindow::downloadProgress(qint64 recieved, qint64 total) {
    ui->progressBar->setMaximum(total);   
    ui->progressBar->setValue(recieved);
}