Reputation: 67
I have successfully created a GUI in C++
using Qt
in Eclipse, but then when it came to assigning my own button click event, I was told the macro Q_OBJECT
would need to be included in the headerfile of my QWidget
class.
Upon running, the window no longer displays and I am met with an error on both the constructor and the destructor of my QWidget
class.
The header file is as follows:
#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_ */
And the constructor and destructors are as follows:
MediaPlayerWizard::MediaPlayerWizard(QWidget *parent):QWidget(parent)
{
Initialize(); //Instantiates the buttons and labels etc..
}
MediaPlayerWizard::~MediaPlayerWizard(){
}
All of my header files are listed in the HEADERS
list of my .pro file, and I have ran QMake
after adding the Q_OBJECT
macro to my header file.
Initialize code:
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();
}
Here is the error message displayed:
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
Upvotes: 0
Views: 549
Reputation: 2102
You need to uncomment //Q_OBJECT
in mediaplayerwizard.h, the line should contain only Q_OBJECT
And define void btnActivateClicked();
as slot.
Upvotes: 0
Reputation: 3174
You need to remove your build directory and build the solution again. Rebuild could not work so please remove the build directory. This is an old Qt issue.
Upvotes: 0
Reputation: 1906
You need to mark member function as a slot to use it as a slot. So, try to change declaration
void btnActivateClicked();
with
private slots:
void btnActivateClicked();
or
Q_SLOT void btnActivateClicked();
Upvotes: 2