编译Qt hello World时未定义的对vtable的引用。

时间:2023-01-15 04:54:01

I was following this tutorial where I designed a simple window with a single button and try to trigger it. When I compile it I get an error and I don't really understand where it comes from. When I compile a simple program that works, but now when I try to create my own gui using QDesginer it doesn't work anymore. Usually the vtable error means that some virtual function is not implemented, but I don't see wheer this should result from. I have looked at similar questions here like QT C++ error: undefined reference to `vtable for appprinter' or Qt with codeblocks - undefined reference to vtable but this doesn't really help.

我在学习本教程的时候,我设计了一个简单的单按钮窗口,并试图触发它。当我编译它时,我得到一个错误,我不知道它是从哪里来的。当我编写一个简单的程序时,但是现在当我尝试用QDesginer创建我自己的gui时,它不再工作了。通常,vtable错误意味着一些虚函数没有实现,但我不认为这应该是由什么导致的。我在这里看到了类似的问题,比如QT c++错误:未定义的对“appprinter”的“vtable”的引用,或者带有codeblocks的QT(未定义的对vtable的引用),但这并没有真正的帮助。

main.cpp

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

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

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

2 个解决方案

#1


0  

this is often an dependency issue where the moc-compiler where not invoked due to an already existing object file. try to distclean your environment and start qmake again. hope that helps

这通常是一个依赖关系问题,在这里,由于已经存在的对象文件而没有调用的moc编译器。试着让你的环境变得更糟,重新开始。希望这有助于

#2


1  

Do NOT use Q_OBJECT macros in cpp files except you want to manually run the moc... Just move your class definition to main.h, include this in SOURCES and rerun qmake -> works

不要在cpp文件中使用Q_OBJECT宏,除非您想手动运行moc…只需将类定义移动到main。h,将其包含在源代码中,重新运行qmake ->。

#1


0  

this is often an dependency issue where the moc-compiler where not invoked due to an already existing object file. try to distclean your environment and start qmake again. hope that helps

这通常是一个依赖关系问题,在这里,由于已经存在的对象文件而没有调用的moc编译器。试着让你的环境变得更糟,重新开始。希望这有助于

#2


1  

Do NOT use Q_OBJECT macros in cpp files except you want to manually run the moc... Just move your class definition to main.h, include this in SOURCES and rerun qmake -> works

不要在cpp文件中使用Q_OBJECT宏,除非您想手动运行moc…只需将类定义移动到main。h,将其包含在源代码中,重新运行qmake ->。