我关注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
答案 0 :(得分:1)
不要在cpp文件中使用Q_OBJECT宏,除非你想手动运行moc ...只需将你的类定义移动到main.h,在SOURCES中包含它并重新运行qmake - &gt;作品
答案 1 :(得分:0)
这通常是一个依赖性问题,其中moc-compiler由于已经存在的目标文件而未被调用。尝试扰乱你的环境并重新开始qmake。希望有所帮助