缺少带有 QObject 的派生类
Posted
技术标签:
【中文标题】缺少带有 QObject 的派生类【英文标题】:missing derived class with QObject 【发布时间】:2014-06-21 20:45:27 【问题描述】:我收到错误 ~mainwindow.cpp:9: error: redefinition of 'MainWindow::MainWindow(QWidget*)' MainWindow::MainWindow(QWidget *parent) :我很确定它在抱怨错误派生类名。基类是 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*)));
//主窗口.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】:您已经定义了两次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);
【讨论】:
以上是关于缺少带有 QObject 的派生类的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 QObject 派生类实例遵循 OOP 基本概念?