未定义的引用vtable ... Q_OBJECT宏

时间:2011-10-26 15:06:42

标签: c++ qt macros undefined vtable

当我取消注释信号槽所需的Q_OBJECT宏时,我得到了一个未定义的参考vtable for MyApp错误,但没有宏它完全编译,但没有它我就不能使用信号和插槽。我想我可能做了一些愚蠢的错误,但请尝试帮助,因为我真的找不到问题。我和我知道我的代码是不整洁的,我正在努力。

myapp.h:

#ifndef MYAPP_H
#define MYAPP_H

#include <QApplication>
#include <QEvent>
#include <QObject>
#include <QDebug>

class MyApp : public QApplication
{

public:
    MyApp( int argc, char** argv );

protected:
    bool eventFilter(QObject *object, QEvent *event);

signals:
    void focusG();
    void focusL();
};

#endif // MYAPP_H

MyApp.cpp中:

#include "myapp.h"

MyApp::MyApp(int argc, char **argv): QApplication(argc, argv)
{
    installEventFilter(this);
}

bool MyApp::eventFilter(QObject *object, QEvent *event)
{
    if (event->type() == QEvent::ApplicationDeactivate)
    {
        qDebug() << "Focus lost";
        //focusL();
    }
    if (event->type() == QEvent::ApplicationActivate)
    {
        qDebug() << "Focus gained";
        //focusG();
    }

    return false;
}

main.cpp中:

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QObject>
#include <QGraphicsObject>
#include <QTimer>
#include <QVariant>
#include "timecontrol.h"
#include "scorecontrol.h"
#include "Retry.h"
#include <QEvent>
#include "myapp.h"

int main(int argc, char *argv[])
{
    //QApplication app(argc, argv);
    MyApp app(argc, argv);

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape);
    viewer.setMainQmlFile(QLatin1String("qml/Raker/main.qml"));
    viewer.showExpanded();

    QObject *rootObject = viewer.rootObject();

    QTimer *timmer = new QTimer;
    timmer->setInterval(1000);

    TimeControl *timcon = new TimeControl;

    scorecontrol *scorer = new scorecontrol;

    Retry *probeer = new Retry;

    QObject::connect(timmer, SIGNAL(timeout()), timcon, SLOT(updateTime()));
    QObject::connect(timcon, SIGNAL(setTime(QVariant)), rootObject, SLOT(setTime(QVariant)));
    QObject::connect(rootObject, SIGNAL(blockClicked(int, int)), scorer, SLOT(checkRight(int, int)));
    QObject::connect(scorer, SIGNAL(setScore(QVariant)), rootObject, SLOT(setScore(QVariant)));
    QObject::connect(scorer, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant)));

    QObject::connect(rootObject, SIGNAL(start()), probeer, SLOT(Reetry()));
    QObject::connect(probeer, SIGNAL(start()), timmer, SLOT(start()));
    QObject::connect(probeer, SIGNAL(stop()), timmer, SLOT(stop()));
    QObject::connect(probeer, SIGNAL(start(int)), scorer, SLOT(randomNum(int)));
    QObject::connect(probeer, SIGNAL(sReset()), timcon, SLOT(reset()));
    QObject::connect(probeer, SIGNAL(tReset()), scorer, SLOT(reset()));
    QObject::connect(timcon, SIGNAL(timeOut()), scorer, SLOT(reset()));

    QObject::connect(timcon, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant)));
    QObject::connect(timcon, SIGNAL(changeFinal()), scorer, SLOT(changeFinal()));
    QObject::connect(scorer, SIGNAL(setFinal(QVariant)), rootObject, SLOT(setFinal(QVariant)));

    QObject::connect(&app, SIGNAL(focusL()), probeer, SLOT(focusL()));
    QObject::connect(&app, SIGNAL(focusG()), probeer, SLOT(focusG()));

    return app.exec();
}

BlockToucher.pro:

# Add more folders to ship with the application, here
folder_01.source = qml/Raker
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01

# Additional import path used to resolve QML modules in Creator's code model
QML_IMPORT_PATH =

symbian:TARGET.UID3 = 0x2004b49f

# Smart Installer package's UID
# This UID is from the protected range and therefore the package will
# fail to install if self-signed. By default qmake uses the unprotected
# range value if unprotected UID is defined for the application and
# 0x2002CCCF value if protected UID is given to the application
symbian:DEPLOYMENT.installer_header = 0x2002CCCF

# Allow network access on Symbian
symbian {
    #TARGET.CAPABILITY += NetworkServices

    vendorinfo = "%{\"Gerhard de Clercq\"}" ":\"Gerhard de Clercq\""
    ICON = BlockToucher.svg
}

# If your application uses the Qt Mobility libraries, uncomment the following
# lines and add the respective components to the MOBILITY variable.
# CONFIG += mobility
# MOBILITY +=

# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp \
    timecontrol.cpp \
    scorecontrol.cpp \
    Retry.cpp \
    myapp.cpp \
    myapplication.cpp

# Please do not modify the following two lines. Required for deployment.
include(qmlapplicationviewer/qmlapplicationviewer.pri)
qtcAddDeployment()

HEADERS += \
    timecontrol.h \
    scorecontrol.h \
    Retry.h \
    myapp.h \
    myapplication.h

OTHER_FILES += \
    qtc_packaging/debian_fremantle/rules \
    qtc_packaging/debian_fremantle/README \
    qtc_packaging/debian_fremantle/copyright \
    qtc_packaging/debian_fremantle/control \
    qtc_packaging/debian_fremantle/compat \
    qtc_packaging/debian_fremantle/changelog
compat \
    qtc_packaging/debian_fremantle/changelog

2 个答案:

答案 0 :(得分:4)

MOC系统使用Q_OBJECT生成信号所需的代码。我最好的猜测是你的项目中没有包含MOC生成的文件(它生成的CPP文件)。

你如何做到这一点在很大程度上取决于你的构建系统(CMake,QMake,AUtomake,MSVC),但你应该首先参考QMake教程。

也就是说,对vtable的未定义引用是我忘记更新我的Cmake文件并拥有Q_OBJECT时有时会遇到的错误。

此外,您的MyApp ctor错误,签名必须是:

MyApp( int & argc, char** argv );

如果您使用命令行参数,&很重要。

答案 1 :(得分:1)

当我决定将Q_OBJECT添加到我的标题文件中时,我遇到了同样的问题 超级简单的解决方案就是:

  1. 添加一个以QObject为基类的新临时类
  2. 使用新的临时类
  3. 仔细检查标题和源文件是否有任何错误
  4. 构建您的项目。它应该成功构建
  5. 完全删除临时类(.h和.cpp文件以及.pro文件中的相对行)
  6. 享受:)