我正在关注 this tutorial我设计了一个带有单个按钮的简单窗口并尝试触发它。当我编译它时,我得到一个错误,我真的不明白它来自哪里。当我编译一个可以工作的简单程序时,但现在当我尝试使用 QDesginer 创建我自己的 gui 时,它不再工作了。通常 vtable
错误意味着某些虚函数没有实现,但我看不出这应该是什么原因造成的。我在这里看过类似的问题,比如 QT C++ error: undefined reference to `vtable for appprinter'或 Qt with codeblocks - undefined reference to vtable但这并没有真正的帮助。
main.cpp
#include <QtWidgets/qapplication.h>
#include <QtWidgets/qpushbutton.h>
#include "main_frame.h"
class TestMainFrame : public QFrame
{
Q_OBJECT
public:
explicit TestMainFrame(QWidget *parent = 0);
~TestMainFrame();
private slots:
void onTest();
private:
Ui::MainFrameGUI *ui;
};
TestMainFrame::TestMainFrame(QWidget *parent) :
QFrame(parent),
ui(new Ui::MainFrameGUI)
{
ui->setupUi(this);
}
TestMainFrame::~TestMainFrame()
{
delete ui;
}
void TestMainFrame::onTest()
{
printf("Test\n");
}
int main( int argc, char **argv )
{
QApplication a(argc, argv);
TestMainFrame w;
w.show();
return a.exec();
}
main_frame.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainFrameGUI</class>
<widget class="QFrame" name="MainFrameGUI">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>295</width>
<height>77</height>
</rect>
</property>
<property name="windowTitle">
<string>Testbutton</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="mButton">
<property name="text">
<string>Test</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>mButton</sender>
<signal>clicked()</signal>
<receiver>MainFrameGUI</receiver>
<slot>onTest()</slot>
<hints>
<hint type="sourcelabel">
<x>303</x>
<y>38</y>
</hint>
<hint type="destinationlabel">
<x>303</x>
<y>38</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>onTest()</slot>
</slots>
</ui>
build.log
d:\opt\qt-5.2.1\bin\uic.exe -g cpp -o d:\src\c\QTFrameQML\main_frame.h d:\src\c\QTFrameQML\main_frame.ui
g++.exe -std=c++11 -Wall -g -ID:\opt\qt-5.2.1\include -c d:\src\c\QTFrameQML\main.cpp -o Debug\obj\main.o
g++.exe -LD:\opt\qt-5.2.1\lib -o Debug\QTFrameQML.exe Debug\obj\main.o -lgdi32 -luser32 -lkernel32 -lcomctl32 -lQt5Core -lQt5Gui -lGLESv2d -lQt5Widgetsd -mwindows
Debug\obj\main.o: In function `ZN13TestMainFrameC2EP7QWidget':
d:/src/c/QTFrameQML/main.cpp:23: undefined reference to `vtable for TestMainFrame'
d:/src/c/QTFrameQML/main.cpp:23: undefined reference to `vtable for TestMainFrame'
Debug\obj\main.o: In function `ZN13TestMainFrameD2Ev':
d:/src/c/QTFrameQML/main.cpp:28: undefined reference to `vtable for TestMainFrame'
d:/src/c/QTFrameQML/main.cpp:28: undefined reference to `vtable for TestMainFrame'
collect2.exe: error: ld returned 1 exit status
最佳答案
除非您想手动运行 moc,否则不要在 cpp 文件中使用 Q_OBJECT 宏...只需将您的类定义移动到 main.h,将其包含在 SOURCES 中并重新运行 qmake -> works
关于c++ - 编译 Qt hello World 时对 vtable 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23080617/