使用CMake构建Qt项目并从QMainWindow继承会导致未引用的vtable错误

时间:2015-04-30 16:01:45

标签: c++ qt cmake

这是关于使用CMake的Qt 5.3.2项目构建。

我使用Qt Designer设计了一个QMainWindow,领先 到 main.ui

CMakeLists.txt(几乎完整的东西可能是 在这里找到我已经发布了一个不同的问题: Linking and UIC order in a CMake Qt project) 已经负责调用UIC,所以我亲自动手 ui_main.h

ui_main.h 提供带有简单表单信息的类 Ui :: MainWindow 所有按钮和东西应该在哪里,方法* void setupUi(QMainWindow MainWindow)

现在我的工作流程(它是否可行?)如下所示: 我构建了一个全新的头文件Form_main.h:

// Form_main.h
[..]
class Form_main : public MainWindow, public QMainWindow
{
  Q_OBJECT
  privat slots:
    void on_some_event();
[..]
};

该类使用所述自动生成的 MainWindow :: setupUi(this)来“变形”。并且 QMainWindow 是一个 QMainWindow ,代表所有这一切。

但现在我处于两难境地:我要么删除 Q_OBJECT 宏调用,导致连接(..)不再认识到 Form_main 有信号槽,或者 我让 Q_OBJECT 导致臭名昭着的

undefined reference to `vtable for display::Form_main'
链接项目时出现

错误。

现在,事实上,有类似问题的人。 命名一些链接:

http://michael-stengel.com/blog/?p=103

Qt Linker Error: "undefined reference to vtable"

Undefined reference to vtable... Q_OBJECT macro

Qt vtable error

我从最后一个提示:" MOC必须为ui_main.h生成代码,生成的代码必须编译和链接。"

无论如何,这些答案似乎都归结为再次运行qmake'。好吧,我使用CMake一直想要我的项目完全配置和编译

cmake .
make

所做的尝试删除了构建目录中和下面的所有内容 (包括每个自动生成的文件)然后运行 cmake。 &安培;&安培;使;

可悲的是,这没有帮助。我担心这是我今天的第二个菜鸟问题......你会再次忍受我吗?

===在试试绿道之后我会提供更多细节。 ===

这是自动生成的ui_main.h

/********************************************************************************
** Form generated from reading UI file 'main.ui'
**
** Created by: Qt User Interface Compiler version 5.3.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_MAIN_H
#define UI_MAIN_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
[.. more Widget Includes ..]

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
    public:
        QAction *action_exit;
        [.. more sub widgets like that .. ]

    void setupUi(QMainWindow *MainWindow)
    {
      [ .. Setting up the form. Harmless code. .. ]
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
      [ .. completely harmless .. ]
    } // retranslateUi
};

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MAIN_H

现在阅读所有这些并合并你的帖子我在

// form_main.h

#ifndef MHK_FORM_MAIN_H
#define MHK_FORM_MAIN_H

#include <QMainWindow>
#include "ui_main.h"
[..]

namespace Ui { class MainWindow; }

namespace display
{
class Form_main : public QMainWindow
{
Q_OBJECT

private:
    ostream* stdout;
    ostream* stderr;

    Ui::MainWindow* uiMainWindow;

    /** Called by the constructor. Sets up event connections and other
     * preliminary stuff the qt Designer is overtasked with. */
    void setup_form();

[..]

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

private slots:
    void exit_program();
};
}

#endif

我的cpp

// form_main.cpp

#include "ui_main.h"
#include "form_main.h"
[..]

using namespace Ui;

namespace display
{
void Form_main::setup_form()
{
    QObject::connect(uiMainWindow->action_exit, SIGNAL(triggered()), this, SLOT(exit_program()));
    [..]
}

Form_main::Form_main(QWidget* parent) : QMainWindow(parent)
{
    uiMainWindow = new Ui::MainWindow();
    uiMainWindow->setupUi(this);
    [..]
#if defined(Q_OS_SYMBIAN)
  this->showMaximized();
#else
  this->show();
#endif
}

Form_main::~Form_main()
{
    delete uiMainWindow;
}
[..]
Form_main::exit_program()
{
    this->close();
    (*stdout) << "Thanks for playing " << getProgramName() << endl;
}
}

1 个答案:

答案 0 :(得分:1)

确定。我(部分)看到了这个问题。只需创建一个这样的小部件类:

·H

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

的.cpp

#include "MainWindow.h"
#include "ui_MainWindow.h"

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

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

这就是QtCreator创建ui-Widgets的方式。 “ui_MainWindow.h”是您生成的.h文件。