Q_OBJECT导致对v_table'的未定义引用问题

时间:2014-09-29 08:05:26

标签: c++ qt qwidget qobject moc

我已经在Eclipse中使用C++Qt中成功创建了一个GUI,但是在分配我自己的按钮点击事件时,我被告知宏Q_OBJECT需要被包含在我的QWidget类的头文件中。

运行后,窗口不再显示,我的QWidget类的构造函数和析构函数都遇到错误。

头文件如下:

#ifndef MEDIAPLAYERWIZARD_H_
#define MEDIAPLAYERWIZARD_H_

#include "../MediaPlayer.Helpers/SystemConfiguration.h"
#include "../MediaPlayer.Helpers/StringHelpers.h"
#include "../MediaPlayer.DataAccess/DataRepository.h"
#include "../MediaPlayer.Helpers/Globals.h"
#include <QtGui/QApplication>
#include <QtGui/QLabel>
#include <QtGui>
#include <QtCore>
#include <sstream>
#include <iostream>

class MediaPlayerWizard: public QWidget {
    Q_OBJECT

public:
    MediaPlayerWizard(QWidget *parent = 0);
    void Initialize();
    virtual ~MediaPlayerWizard();

private:
    QLabel *lblWelcomeMessage;

    //Input
    QLineEdit *txtName;
    QLabel *lblName;

    QLineEdit *txtActivationCode;
    QLabel *lblActivationCode;

    //Buttons
    QPushButton *btnActivate;
    QPushButton *btnCancel;

    //Layouts
    QVBoxLayout *fldWizardLayout;

    QHBoxLayout *fldWelcomeMessage;
    QHBoxLayout *fldName;
    QHBoxLayout *fldActivationCode;
    QHBoxLayout *fldButtons;

private slots:
    void btnActivateClicked();
};

#endif /* MEDIAPLAYERWIZARD_H_ */

构造函数和析构函数如下:

MediaPlayerWizard::MediaPlayerWizard(QWidget *parent):QWidget(parent)
{
    Initialize(); //Instantiates the buttons and labels etc..
}

MediaPlayerWizard::~MediaPlayerWizard(){

}

我的.pro文件的HEADERS列表中列出了所有头文件,并且在将QMake宏添加到头文件后运行了Q_OBJECT

初始化代码:

void MediaPlayerWizard::Initialize()
{
    //Widget Configuration

    this->setWindowTitle("Media Player: First Run Wizard");

    int labelWidth = 150;

    //Welcome Message
    lblWelcomeMessage = new QLabel;
    lblWelcomeMessage->setText("Welcome to the first run wizard that will assist you in\n connecting and registering this advertising player to your account.");
    lblWelcomeMessage->setAlignment(Qt::AlignCenter);

    //Input Labels
    lblName = new QLabel;
    lblName->setText("Name: ");
    lblName->setFixedWidth(labelWidth);

    lblActivationCode = new QLabel;
    lblActivationCode->setText("Application Code: ");
    lblActivationCode->setFixedWidth(labelWidth);

    //Input Fields
    txtName = new QLineEdit();
    txtActivationCode = new QLineEdit();

    //Buttons
    btnActivate = new QPushButton;
    btnActivate->setText("Activate");
    btnCancel = new QPushButton;
    btnCancel->setText("Cancel");

    //Button Events
    QObject::connect(btnActivate, SIGNAL(clicked()), this, SLOT(btnActivateClicked()));
    QObject::connect(btnCancel, SIGNAL(clicked()), qApp, SLOT(quit()));

    //Layouts
    fldWelcomeMessage = new QHBoxLayout;
    fldWelcomeMessage->addWidget(lblWelcomeMessage);

    fldName = new QHBoxLayout;
    fldName->addWidget(lblName);
    fldName->addWidget(txtName);

    fldActivationCode = new QHBoxLayout;
    fldActivationCode->addWidget(lblActivationCode);
    fldActivationCode->addWidget(txtActivationCode);

    fldButtons = new QHBoxLayout;
    fldButtons->addWidget(btnActivate);
    fldButtons->addWidget(btnCancel);

    fldWizardLayout = new QVBoxLayout;
    fldWizardLayout->addLayout(fldWelcomeMessage);
    fldWizardLayout->addLayout(fldName);
    fldWizardLayout->addLayout(fldActivationCode);
    fldWizardLayout->addLayout(fldButtons);

    setLayout(fldWizardLayout);
    show();
}

以下是显示的错误消息:

Building target: MediaPlayerCPP
Invoking: Cross G++ Linker
g++ -L/usr/lib -o "MediaPlayerCPP"  ./src/MediaPlayer.o ./src/MediaPlayerWizard.o ./src/mysqlapidemo.o  ./MediaPlayer.Services/MediaPlayerClient.o  ./MediaPlayer.Helpers/DeviceManagement.o ./MediaPlayer.Helpers/Globals.o ./MediaPlayer.Helpers/MD5.o ./MediaPlayer.Helpers/StringHelpers.o ./MediaPlayer.Helpers/SystemConfiguration.o ./MediaPlayer.DataAccess/DataObject.o ./MediaPlayer.DataAccess/Database.o ./MediaPlayer.DataAccess/Media.o ./MediaPlayer.DataAccess/MediaLog.o ./MediaPlayer.DataAccess/MediaLogProvider.o ./MediaPlayer.DataAccess/MediaProvider.o ./MediaPlayer.DataAccess/MediaSchedule.o ./MediaPlayer.DataAccess/MediaScheduleProvider.o ./MediaPlayer.DataAccess/SystemConfig.o ./MediaPlayer.DataAccess/SystemConfigProvider.o   -lQtCore -lmysqlclient -lz -lQtGui
./src/MediaPlayerWizard.o: In function `MediaPlayerWizard::MediaPlayerWizard(QWidget*)':
/home/gtteam/Projects/MediaPlayerCPP/Debug/../src/MediaPlayerWizard.cpp:10: undefined reference to `vtable for MediaPlayerWizard'
/home/gtteam/Projects/MediaPlayerCPP/Debug/../src/MediaPlayerWizard.cpp:10: undefined reference to `vtable for MediaPlayerWizard'
./src/MediaPlayerWizard.o: In function `MediaPlayerWizard::~MediaPlayerWizard()':
/home/gtteam/Projects/MediaPlayerCPP/Debug/../src/MediaPlayerWizard.cpp:77: undefined reference to `vtable for MediaPlayerWizard'
/home/gtteam/Projects/MediaPlayerCPP/Debug/../src/MediaPlayerWizard.cpp:77: undefined reference to `vtable for MediaPlayerWizard'
collect2: error: ld returned 1 exit status
make: *** [MediaPlayerCPP] Error 1

3 个答案:

答案 0 :(得分:2)

您需要将成员函数标记为插槽才能将其用作插槽。 所以,尝试更改声明

void btnActivateClicked();

private slots: void btnActivateClicked();

Q_SLOT void btnActivateClicked();

答案 1 :(得分:0)

您需要在mediaplayerwizard.h中取消注释//Q_OBJECT,该行应仅包含Q_OBJECT

将void btnActivateClicked();定义为插槽。

答案 2 :(得分:0)

您需要删除构建目录并再次构建解决方案。重建无法工作,请删除构建目录。这是an old Qt issue