0

我已经成功地创建了一个在 Eclipse 中C++使用的 GUI Qt,但是在分配我自己的按钮单击事件时,我被告知宏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(){

}

我的所有头文件都列在HEADERS我的 .pro 文件列表中,并且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
4

3 回答 3

2

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

void btnActivateClicked();

private slots: void btnActivateClicked();

或者

Q_SLOT void btnActivateClicked();

于 2014-09-29T08:29:19.610 回答
0

You need to uncomment //Q_OBJECT in mediaplayerwizard.h, the line should contain only Q_OBJECT

And define void btnActivateClicked(); as slot.

于 2014-09-29T08:19:37.997 回答
0

您需要删除构建目录并再次构建解决方案。重建无法工作,因此请删除构建目录。这是一个旧的 Qt 问题

于 2014-09-29T08:26:48.287 回答