我已经成功地创建了一个在 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