Q_OBJECT causing a 'undefined reference to v_table' issue - c++

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

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();

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.

You need to uncomment //Q_OBJECT in mediaplayerwizard.h, the line should contain only Q_OBJECT
And define void btnActivateClicked(); as slot.

Related

connect QPushButton to a function inside a the same class

I want to connect QPushButton to a function inside the same class. the class is just a QGridLayout that making ui via code. I don't know what to do. actually I don't know what to put in for reciever argument in QObject::connect.
I don't want to use bunch of h and cpp files so that the code be as simple and readable as possible.
here is my main.cpp file:
#include <QApplication>
#include <QtWidgets>
#include <iostream>
using namespace std;
class ConnectPage
{
public:
void login_clicked(){
cout<<"login pressed"<<endl;
}
QGridLayout *main_layout = new QGridLayout;
// user interface Labels
QLabel *username_label = new QLabel("username");
QLabel *password_label = new QLabel("password");
QLabel *host_label = new QLabel("host");
QLabel *port_label = new QLabel("port");
QLabel *status_bar = new QLabel;
// user interface lineedits
QLineEdit *username_lineedit = new QLineEdit;
QLineEdit *password_lineedit = new QLineEdit;
QLineEdit *host_lineedit = new QLineEdit;
QLineEdit *port_lineedit = new QLineEdit;
// user interface buttons
QPushButton *login = new QPushButton("Connect");
QPushButton *reset = new QPushButton("Clear form");
ConnectPage()
{
QObject::connect(login, SIGNAL(clicked()), &app, SLOT(login_clicked()));
main_layout->addWidget(username_label,0,0,1,1);
main_layout->addWidget(username_lineedit,0,1,1,1);
main_layout->addWidget(password_label,1,0,1,1);
main_layout->addWidget(password_lineedit,1,1,1,1);
main_layout->addWidget(host_label,2,0,1,1);
main_layout->addWidget(host_lineedit,2,1,1,1);
main_layout->addWidget(port_label,3,0,1,1);
main_layout->addWidget(port_lineedit,3,1,1,1);
main_layout->addWidget(reset,4,0,1,1);
main_layout->addWidget(login,4,1,1,1);
main_layout->addWidget(status_bar,5,0,1,2);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
ConnectPage start_connecting;
QGridLayout *main_layout = start_connecting.main_layout;
window->setLayout( main_layout );
window->setWindowTitle("Login Page");
window->show();
return app.exec();
}
and here is .pro file:
QT += core gui widgets
SOURCES += \
main.cpp \
HEADERS += \
any help that regards only these two files would be appreciated.
If you are using Qt <= 15.2, i think you should start at reading Qt resources with signals and slots. link
Simple answer is that:
your class should inherit from Qobject,and you should put QObject makro in top and private section of this class.
class example : public QObject
{
Q_OBJECT
public: ...
public slots: ...
private: ...
};
Function that is slot, should be in public slots: section of a class. Like:
public slots:
void login_clicked(){
cout<<"login pressed"<<endl;
}
You could capture this function in class constructor like this:
connect(login, &QPushButton::pressed, this, &ConnectPage::login_clicked);
And you may use a lambda expression in connect.
connect(login, &QPushButton::pressed, [&](){
cout<<"login pressed"<<endl;
});
And for last word. If you want to use syntax that you have now, you need to remember about function signatures. From documentation:
"The rule about whether to include arguments or not in the SIGNAL() and SLOT() macros, if the arguments have default values, is that the signature passed to the SIGNAL() macro must not have fewer arguments than the signature passed to the SLOT() macro."
So this syntax should work:
connect(login, SIGNAL( clicked(bool)), this, SLOT(login_clicked()));
But I don't like this syntax.
Best regards!

Simple app getting "undefined reference to vtable" error? [Qt]

I am trying to make the code found on this page compile. All I want to do is replicate what that page describes. I keep getting an error that says:
main.cpp:13: error: undefined reference to `vtable for myMainWindow'
Here's my code, which is pretty much exactly like the code on that page:
main.cpp
#include <QApplication>
#include <QDialog>
#include <QWidget>
#include <QGridLayout>
#include <QPushButton>
#include <QMainWindow>
#include <QBitmap>
class myMainWindow : public QMainWindow
{
public:
myMainWindow():QMainWindow()
{
setMask((new QPixmap("saturn.png"))->mask());
QPalette* palette = new QPalette();
palette->setBrush(QPalette::Background,QBrush(QPixmap("saturn.png")));
setPalette(*palette);
setWindowFlags(Qt::FramelessWindowHint);
QWidget *centralWidget = new QWidget(this);
QGridLayout *layout = new QGridLayout();
centralWidget->setLayout(layout);
QPushButton* button1 = new QPushButton("Button 1");
button1->setFixedSize(80,50);
layout->addWidget(button1,0,0);
setCentralWidget(centralWidget);
};
~myMainWindow();
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
myMainWindow *window = new myMainWindow();
window->resize(600, 316);
window->show();
return app.exec();
}
I read up on why this might be occurring and saw that it was something about needing to have the class defined in a header file. How would I go about doing that correctly given this code?
Aside from missing the destructor definition, you are also missing the Q_OBJECT macro, which is mandatory for all QObject derived classes. If you had that, you'd get another error about the MOC generated files, when you define QObject derived classes in main.cpp you need to include the MOC file manually. This is not the case if you use a dedicated h and cpp file for QObject derived classes.
As stated in the comments: The missing function body for ~myMainWindow() was the problem.

How to get the member object for SLOT in connect()?

I Started learning Qt (5.5) a couple of days ago, and I recently got stuck on something when working with the connect function, specifically the SLOT parameter. I'm calling a member function from the same class that the connect function is called in, but when the SLOT function is triggered it acts like it's creating a new class object. It worked initially when I kept everything in the same class, but this problem popped up when I tried implementing a hierarchy. I wrote a short program to demonstrate my problem.
Main.cpp
#include <QApplication>
#include "MainWindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow QtWindow;
QtWindow.show();
return app.exec();
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include <QGridLayout>
#include "TopWidget.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QMainWindow *parent = 0);
private:
QWidget *mainWidget;
QGridLayout *mainLayout;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "MainWindow.h"
MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent){
mainWidget = new QWidget(this);
mainLayout = new QGridLayout(mainWidget);
setCentralWidget(mainWidget);
TopWidget tWidget(this);
mainLayout->addWidget(tWidget.topWidget, 0, 0);
}
TopWidget.h
#ifndef TOPWIDGET_H
#define TOPWIDGET_H
#include <stdlib.h>
#include <QWidget>
#include <QPushButton>
#include <QGridLayout>
#include <QDebug>
#include <QErrorMessage>
class TopWidget : public QWidget
{
Q_OBJECT
public:
TopWidget(QWidget *parent);
QWidget *topWidget;
private:
QGridLayout *wLayout;
QPushButton *Button;
int memVar1;
int memVar2;
private slots:
void testConnect();
//void SlotWithParams(int a, int b);
};
#endif // TOPWIDGET_H
TopWidget.cpp
#include "TopWidget.h"
TopWidget::TopWidget(QWidget *parent) : QWidget(parent){
topWidget = new QWidget(parent);
wLayout = new QGridLayout(topWidget);
memVar1 = 123;
memVar2 = 321;
Button = new QPushButton("Click Me", topWidget);
connect(Button, &QPushButton::clicked, [=](){ TopWidget::testConnect(); });
}
void TopWidget::testConnect(){
qDebug("Button worked");
if(memVar1 != 123 || memVar2 != 321){
qDebug("Linking failed");
}else{
qDebug("Linking success");
}
}
Since I just started with Qt, I don't have a good feel for what's "proper" Qt code, and what I should avoid, so tips in that direction are also appreciated. The following is the qmake file, if that's important.
CONFIG += c++11
CONFIG += debug
CONFIG += console
QT += widgets
QT += testlib
SOURCES += main.cpp
SOURCES += MainWindow.cpp
SOURCES += TopWidget.cpp
HEADERS += MainWindow.h
HEADERS += TopWidget.h
Release:DESTDIR = bin/Release
Release:OBJECTS_DIR = obj/Release
Release:MOC_DIR = extra/Release
Release:RCC_DIR = extra/Release
Release:UI_DIR = extra/Release
Debug:DESTDIR = bin/Debug
Debug:OBJECTS_DIR = obj/Debug
Debug:MOC_DIR = extra/Debug
Debug:RCC_DIR = extra/Debug
Debug:UI_DIR = extra/Debug
When I run the program in debug mode and press the button, it outputs "Button worked" indicating the link to the function was successful, but then outputs "Linking failed" indicating that a new object was created rather than taking the old one. My knowledge of C++ is patchy, since I only pick up what I need to, and I spent hours yesterday trying to fix this, so forgive me if the fix is something ridiculously easy, but I've mentally exhausted myself over this.
The problem comes from this line:
TopWidget tWidget(this);
You are allocating tWidget on the stack, and it gets destroyed just at the end of the MainWindow constructor.
Replace by:
TopWidget * tWidget = new TopWidget(this);
Also, you should replace your connect line by this one
connect(Button, &QPushButton::clicked, this, &TopWidget::testConnect);
It appears that your slot is called even after the TopWidget is destroyed. Qt normally disconnects connections when sender or receiver are destructed, but it's not able to do that when you connect to a lambda.
And finally, you are doing something weird. What is the purpose of your TopWidget class besides just creating another widget and receiving signals on its slot? You never add the TopWidget to any layout, but just its child. TopWidget is never shown, so it should rather derive from QObject only.

undefined reference to function send

I'm a very beginnner at C++ /Qt programming. I've made this simple dialog box that check the QLineEdit, if the text entered is "bob" should enable the OK button.
I can't get it to compile successfully, it gives me:
dialog.cpp|31|undefined reference to `Dialogmio::send()'
What am I doing wrong?
This is dialog.h:
//dialog.h
#ifndef DIALOG_H_INCLUDED
#define DIALOG_H_INCLUDED
#include <QDialog>
class QPushButton;
class QLineEdit;
class Dialogmio : public QWidget
{
public:
Dialogmio(QWidget *parent =0);
signals:
void send ();
public slots:
void recip(QString &text);
private:
QLineEdit *linedit;
QPushButton *buttonOK;
};
#endif
This is dialog.cpp:
//dialog.cpp
#include <QtGui>
#include "dialog.h"
Dialogmio::Dialogmio(QWidget *parent)
: QWidget(parent)
{
linedit = new QLineEdit();
buttonOK = new QPushButton("OK");
buttonOK->setEnabled(FALSE);
connect( linedit, SIGNAL( textChanged(const QString &) ), this, SLOT( recip(const QString &) ));
connect (this,SIGNAL( send()), this, SLOT( buttonOK->setEnabled(true)) );
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(linedit);
layout->addWidget(buttonOK);
setLayout(layout);
}
void Dialogmio::recip(QString &text)
{
QString a = linedit->text();
if (a == "bob"){
emit send(); //here it gives me the error
}
}
This is main.cpp:
#include <QApplication>
#include "dialog.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Dialogmio *dialog = new Dialogmio;
dialog->show();
return app.exec();
}
I've inserted the Q_OBJECT macro as suggested, now I get one more errors on line 7:
dialog.cpp|7|undefined reference to `vtable for Dialogmio'|
You start by including the Qt file for QDialog, but then go on to inherit from QWidget. While inheriting from QWidget is not a problem, was your intention to actually inherit from QDialog(?), in which case you should define your class this way: -
class Dialogmio : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget* parent);
private slots:
void aSlotFunction();
}
The signals and slots mechanism are C++ extensions that are unique to Qt and in order for a class to use it, the class must include the Q_OBJECT macro, which adds all the necessary functionality. During the build stages, Qt parses the header and creates the code required for the extensions, including run-time type information, the dynamic property system and of-course the signals and slots.
As you state you're using codeblocks as the IDE, if it doesn't automatically run qmake before building, you'll need to do that whenever you add any signals or slots to a class in order for the moc (meta-object-compiler) to see them.
Another thing is that the call to connect signals and slots is wrong: -
connect (this,SIGNAL( send()), this, SLOT( buttonOK->setEnabled(true)) );
The parameter in the SLOT macro takes a slot function, so you need to create a slot and connect it to the send signal: -
connect(this, SIGNAL(send()), this, SLOT(aSlotFunction());
Inside the aSlotFunction, you can then call the set enabled for the button: -
void Dialogmio::aSlotFunction()
{
buttonOK->setEnabled(true);
}
If you're using Qt 5, there's an easier syntax of handling the connection: -
connect(this, &Dialogmio::send, this, &Dialogmio::aSlotFunction);
As this syntax accepts pointers to the functions that will be called, they don't actually have to be declared as slots to work. In addition, you do not provide the arguments, so if they change, you won't have to update the connect calls too.

Qt connect function [duplicate]

This question already has answers here:
Undefined reference to vtable. Trying to compile a Qt project
(21 answers)
Closed 9 years ago.
I'm trying to program a Qt GUI in C++. Here is the code:
sample.h:
#ifndef SAMPLE_H
#define SAMPLE_H
#include <QtGui/QApplication>
#include <QtGui/QPushButton>
#include <QtGui/QWidget>
#include <QtGui/QGridLayout>
#include <QtGui/QLineEdit>
#include <qobject.h>
class Sample : public QObject
{
Q_OBJECT
public:
Sample();
public slots:
void buttonPressed();
private:
QWidget *widget;
QGridLayout *layout;
QLineEdit *le;
QPushButton *button;
};
#endif // SAMPLE_H
sample.cpp:
#include "sample.h"
Sample::Sample()
{
widget = new QWidget();
widget->setWindowTitle("Sample");
layout = new QGridLayout();
le = new QLineEdit();
button = new QPushButton();
button->setText("Button");
connect(button, SIGNAL(clicked()), this, SLOT(buttonPressed()));
layout->addWidget(le, 0, 0);
layout->addWidget(button, 1 , 0);
widget->resize(300, 300);
widget->setLayout(layout);
widget->show();
}
void Sample::buttonPressed(){
le->setText("pressed");
}
I obtain this error when building:
error: undefined reference to `vtable for Sample'
I'm using QtCreator from official Qt webpage.
Does anybody know what to do to make it work? Thank you very much for your responses :)
This kind of error usually occurs if you add the Q_OBJECT macro after having written and compiling your class. Rerunning qmake will usually fix it.