无法链接到自定义Qt小部件

时间:2015-10-12 07:20:02

标签: c++ qt

我是Qt的新手,正在尝试弄清楚如何将我创建的自定义小部件添加到主窗口。

小部件非常简单 - 我只是使用Qt Designer创建默认小部件(我称之为编辑器),然后添加了一个按钮。接下来,我在mainwindow.h中包含了我的自定义小部件的标题,并在我的MainWindow类中添加了指向它的指针。到现在为止还挺好。但是当我尝试创建一个新的Editor实例时,我收到错误:

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Editor::Editor(class QWidget *)" (??0Editor@@QEAA@PEAVQWidget@@@Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)

我需要做些什么才能链接我的代码?

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "editor.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    Editor *editor;
};

#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

    editor = new Editor(0);
}

MainWindow::~MainWindow()
{
    delete ui;

    delete editor;
}

editor.h:

#ifndef EDITOR_H
#define EDITOR_H

#include <QWidget>

namespace Ui {
class Editor;
}

class Editor : public QWidget
{
    Q_OBJECT

public:
    explicit Editor(QWidget *parent = 0);
    ~Editor();

private:
    Ui::Editor *ui;
};

#endif // EDITOR_H

editor.cpp:

#include "editor.h"
#include "ui_editor.h"

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

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

CustomWidgetTest.pro:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = CustomWidgetTest
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    editor.cpp

HEADERS  += mainwindow.h \
    editor.h

FORMS    += mainwindow.ui \
    editor.ui

0 个答案:

没有答案