使用QObject缺少派生类

时间:2014-06-21 20:45:28

标签: c++ qt class inheritance

我收到错误~mainwindow.cpp:9:错误:重新定义'MainWindow :: MainWindow(QWidget *)'MainWindow :: MainWindow(QWidget * parent):我很确定它抱怨错误派生类名。基类是MainWindow,但我看不到派生类是什么。

// mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QImageReader>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void coreEng::requestPage(){
    QUrl url("http://www.nyctelecomm.com");
    QNetworkReply* reply = nam->get(QNetworkRequest(url));
}

void coreEng::finishedSlot(QNetworkReply* reply){

    QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);

    if (reply->error() == QNetworkReply::NoError)
    {

        QImageReader imageReader(reply);
        QImage pic = imageReader.read();

        QByteArray bytes = reply->readAll();  // bytes
        QString string(bytes); // string
    }

    else
    {

    }

//    delete reply();

}

void MainWindow::on_lcdNumber_overflow()
{

    QByteArray replyData = netReply->readAll();
}

void MainWindow::on_pushButton_clicked()
{
    QObject::on_pushButton_clicked(nam, SIGNAL(finished(QNetworkReply*)),
    this, SLOT(finishedSlot(QNetworkReply*)));
}

// mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QObject>
#include <QNetworkAccessManager>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0):
    QMainWindow(parent)
        {
            nam = new QNetworkAccessManager();
        }
    ~MainWindow();

private:
    Ui::MainWindow *ui;

public slots:
    //void connect();
    void on_pushButton_clicked();
    void requestPage();
    void finishedSlot(QNetworkReply* reply);
private slots:
    void on_lcdNumber_overflow();

private:
    QNetworkAccessManager* nam;

};

#endif // MAINWINDOW_H

// main.cpp中

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow mConnect;
    mConnect.on_pushButton_clicked();
    mConnect.requestPage();

    MainWindow w;
    w.show();

    return a.exec();
}

1 个答案:

答案 0 :(得分:0)

您已定义MainWindow::MainWindow两次。首先在头文件中,第二个在源文件中。这是在错误消息中说的。

explicit MainWindow(QWidget *parent = 0):
QMainWindow(parent)
    {
        nam = new QNetworkAccessManager();
    }

//

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}