QT框架错误:原型为QtDownload :: QtDownload(MainWindow)'与课堂上的任何内容都不匹配' QtDownload'

时间:2017-01-20 14:06:51

标签: c++ qt class prototype

它在编译程序时给出了错误,问题出在哪里?

  

C:\ Users \ Admin \ Desktop \ downloader \ mainwindow.cpp:34:错误:原型   for QtDownload :: QtDownload()'在课堂上没有任何匹配   ' QtDownload' QtDownload :: QtDownload():QObject(0){^

// 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

// mainwindow.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();

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

private:
    Ui::MainWindow *ui;
};


class QtDownload : public QObject {
    Q_OBJECT
public:
    explicit QtDownload(MainWindow* parent);
    QNetworkAccessManager manager;
    QString target;

    void setTarget(const QString& t);

private:
    MainWindow* parent;

signals:
    void done();
    void downloadProgress(qint64 recieved, qint64 total);

public slots:
    void download();
    void downloadFinished(QNetworkReply* data);
};

#endif // MAINWINDOW_H

// main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

// mainwindow.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); (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()));

}


    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)), parent, SLOT(downloadProgress(qint64,qint64)));

    }

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

输出:

15:52:34: Running steps for project downloader...
15:52:34: Configuration unchanged, skipping qmake step.
15:52:34: Starting: "C:\Qt\Tools\mingw530_32\bin\mingw32-make.exe" 
C:/Qt/Tools/mingw530_32/bin/mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'C:/Users/Admin/Desktop/build-downloader-Qt_5_2_1_5_2_1_Static-Release'
g++ -c -pipe -fno-keep-inline-dllexport -O2 -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_STATIC_BUILD -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_NETWORK_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I..\downloader -I"C:\Qt\Static\5.2.1\include" -I"C:\Qt\Static\5.2.1\include\QtWidgets" -I"C:\Qt\Static\5.2.1\include\QtNetwork" -I"C:\Qt\Static\5.2.1\include\QtGui" -I"C:\Qt\Static\5.2.1\include\QtCore" -I"release" -I"." -I"." -I"C:\Qt\Static\5.2.1\mkspecs\win32-g++" -o release\mainwindow.o ..\downloader\mainwindow.cpp
..\downloader\mainwindow.cpp:34:4: error: prototype for 'QtDownload::QtDownload(MainWindow)' does not match any in class 'QtDownload'
    QtDownload::QtDownload(MainWindow) : QObject(0) {
    ^
In file included from ..\downloader\mainwindow.cpp:1:0:
..\downloader\mainwindow.h:29:7: error: candidates are: QtDownload::QtDownload(const QtDownload&)
 class QtDownload : public QObject {
       ^
..\downloader\mainwindow.h:32:14: error:                 QtDownload::QtDownload(MainWindow*)
     explicit QtDownload(MainWindow* parent);
              ^
Makefile.Release:551: recipe for target 'release/mainwindow.o' failed
mingw32-make[1]: *** [release/mainwindow.o] Error 1
mingw32-make[1]: Leaving directory 'C:/Users/Admin/Desktop/build-downloader-Qt_5_2_1_5_2_1_Static-Release'
Makefile:34: recipe for target 'release' failed
mingw32-make: *** [release] Error 2
15:52:37: The process "C:\Qt\Tools\mingw530_32\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project downloader (kit: Qt 5.2.1 (5.2.1) Static)
The kit Qt 5.2.1 (5.2.1) Static has configuration issues which might be the root cause for this problem.
When executing step "Make"
15:52:37: Elapsed time: 00:04.

error

1 个答案:

答案 0 :(得分:1)

你错过了参数部分

explicit QtDownload(MainWindow* parent);
                    ^^^^^^^^^^^^^^^^^^

在构造函数定义中

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