简体   繁体   中英

undefined reference to vtable when compiling a Qt hello World

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.

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

请勿在cpp文件中使用Q_OBJECT宏,除非您要手动运行moc ...只需将类定义移至main.h,将其包含在SOURCES中并重新运行qmake-> works

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM