这是关于使用 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 以及所有代表的内容。
但现在我进退两难:要么删除导致 connect(..) 不再识别 Form_main 具有信号槽的 Q_OBJECT 宏调用,要么 我保留导致臭名昭著的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
我从上一个得到的提示:“MOC 必须为 ui_main.h 生成代码并且必须编译和链接生成的代码。”
无论如何,这些答案似乎都归结为“再次运行 qmake”。好吧,我一直在使用 CMake,希望我的项目能够准确地配置和编译
cmake .
make
我所做的尝试删除构建目录中和下面的所有内容 (包括每个自动生成的文件)然后运行 cmake 。 && 制作;。
遗憾的是,这没有帮助。恐怕这是我今天的第二个新手问题...你能再忍受我一次吗?
=== 在尝试使用 GREENWAYS 答案后,我提供了更多详细信息。 ===
这是自动生成的 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;
}
}
最佳答案
好的。我看到了(部分)问题。只需像这样创建一个小部件类:
.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 文件。
关于c++ - 使用CMake构建Qt项目并从QMainWindow继承导致未引用的vtable错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29972542/