Qt connect function [duplicate] - c++

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.

Related

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.

Building Qt project with CMake and inheriting from QMainWindow leads to unreferenced vtable error

This is about a Qt 5.3.2 project buildt using CMake.
I have designed a QMainWindow using the Qt Designer, leading
to main.ui.
CMakeLists.txt (the almost complete thing may be
found here where I already posted it for a different question:
Linking and UIC order in a CMake Qt project )
already takes care of calling UIC so I have my hands on ui_main.h.
ui_main.h offers the class Ui::MainWindow with the plain form information
where all the buttons and stuff should be and the method *void setupUi(QMainWindow MainWindow).
Now my workflow (is it even a feasible one?) goes like this:
I build a totally new header file Form_main.h:
// Form_main.h
[..]
class Form_main : public MainWindow, public QMainWindow
{
Q_OBJECT
privat slots:
void on_some_event();
[..]
};
The class uses said auto-generated MainWindow::setupUi(this) to 'get in shape' and QMainWindow to be, well, a QMainWindow with all that stands for.
But now I am in a dilemma: Either I remove the Q_OBJECT macro call leading to connect(..) no longer recognizing that Form_main has signal slots, or
I keep the Q_OBJECT leading to the infamous
undefined reference to `vtable for display::Form_main'
error while linking the project.
Now, there have been, in fact, people with similar issues.
Naming some links:
http://michael-stengel.com/blog/?p=103
Qt Linker Error: "undefined reference to vtable"
Undefined reference to vtable... Q_OBJECT macro
Qt vtable error
A hint I got from the last one: "MOC must generate code for ui_main.h and the generated code must be compiled and linked."
In any case, these answers all seem to boil down to 'running qmake again'. Well, I use CMake all the way wanting my project to configure and compile after exactly
cmake .
make
What I did try was deleting everything in and below the build directory
(including every auto-generated file) and then running cmake . && make;.
Sadly that did not help. I am afraid this is my second noob question today... would you bear with me once more?
=== AFTER TRYING GREENWAYS ANSWER I PROVIDE MORE DETAILS. ===
Here is the autogenerated ui_main.h
/********************************************************************************
** Form generated from reading UI file 'main.ui'
**
** Created by: Qt User Interface Compiler version 5.3.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_MAIN_H
#define UI_MAIN_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
[.. more Widget Includes ..]
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QAction *action_exit;
[.. more sub widgets like that .. ]
void setupUi(QMainWindow *MainWindow)
{
[ .. Setting up the form. Harmless code. .. ]
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
[ .. completely harmless .. ]
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MAIN_H
Reading all this and incorporating your post right now I am at
// form_main.h
#ifndef MHK_FORM_MAIN_H
#define MHK_FORM_MAIN_H
#include <QMainWindow>
#include "ui_main.h"
[..]
namespace Ui { class MainWindow; }
namespace display
{
class Form_main : public QMainWindow
{
Q_OBJECT
private:
ostream* stdout;
ostream* stderr;
Ui::MainWindow* uiMainWindow;
/** Called by the constructor. Sets up event connections and other
* preliminary stuff the qt Designer is overtasked with. */
void setup_form();
[..]
public:
explicit Form_main(QWidget* parent = 0);
~Form_main();
private slots:
void exit_program();
};
}
#endif
And my cpp
// form_main.cpp
#include "ui_main.h"
#include "form_main.h"
[..]
using namespace Ui;
namespace display
{
void Form_main::setup_form()
{
QObject::connect(uiMainWindow->action_exit, SIGNAL(triggered()), this, SLOT(exit_program()));
[..]
}
Form_main::Form_main(QWidget* parent) : QMainWindow(parent)
{
uiMainWindow = new Ui::MainWindow();
uiMainWindow->setupUi(this);
[..]
#if defined(Q_OS_SYMBIAN)
this->showMaximized();
#else
this->show();
#endif
}
Form_main::~Form_main()
{
delete uiMainWindow;
}
[..]
Form_main::exit_program()
{
this->close();
(*stdout) << "Thanks for playing " << getProgramName() << endl;
}
}
Ok. I see (partly) the problem. Just create a widget class like this:
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
.cpp
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
This is how the QtCreator creates ui-Widgets. "ui_MainWindow.h" is your generated .h file.
Thanks for all your help! However, the problem was in CMakeLists.txt after all. The comment of Chris Morlier on Undefined reference to vtable pointed me to the solution.
The pertinent passage goes:
For Qt users: you can get this same error if you forget to moc a header
I simply had to add the header form_main.h into this line:
QT5_WRAP_CPP(qt_H_MOC ${qt_H} "${DIR_SRC}/include/form_main.h")

Q_OBJECT causing a 'undefined reference to v_table' issue

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.

Custom Slot from qt4 design generated h file

Hi Im try to make a custom slot with the h file that qt4 designer generated
I see how the standard slot is made but i would like to, "once past learning this step" have run terminal commands and have it output in the text box so im tryin to replace the past() slot with one i can right commands in
Here is the h file file that works great but.... the above problem'' trying to understand the flow.. 2nd day so any pointers are awsome. so here it is i will comment where i think i should be working.. but have had no success all day lol.
#ifndef SWEETGUIFORMQ19488_H
#define SWEETGUIFORMQ19488_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QHeaderView>
#include <QtGui/QTextEdit>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_SweetGuiForm
{
public:
QVBoxLayout *verticalLayout_2;
QVBoxLayout *verticalLayout;
QTextEdit *textEdit;
QDialogButtonBox *buttonBox;
void setupUi(QWidget *SweetGuiForm)
{
if (SweetGuiForm->objectName().isEmpty())
SweetGuiForm->setObjectName(QString::fromUtf8("SweetGuiForm"));
SweetGuiForm->resize(511, 275);
verticalLayout_2 = new QVBoxLayout(SweetGuiForm);
verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
verticalLayout = new QVBoxLayout();
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
textEdit = new QTextEdit(SweetGuiForm);
textEdit->setObjectName(QString::fromUtf8("textEdit"));
verticalLayout->addWidget(textEdit);
buttonBox = new QDialogButtonBox(SweetGuiForm);
buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
verticalLayout->addWidget(buttonBox);
verticalLayout_2->addLayout(verticalLayout);
QWidget::setTabOrder(textEdit, buttonBox);
retranslateUi(SweetGuiForm);
QObject::connect(buttonBox, SIGNAL(rejected()), SweetGuiForm, SLOT(close()));
ok here i guess i could replace the paste() with on_buttonBox_accepted(), please correct me i really am still quessing at this point but where do i right the function? do i do the in a file called sweetguiform.cpp ? this is where im lost.. help..
QObject::connect(buttonBox, SIGNAL(accepted()), textEdit, SLOT(paste()));
QMetaObject::connectSlotsByName(SweetGuiForm);
} // setupUi
void retranslateUi(QWidget *SweetGuiForm)
{
SweetGuiForm->setWindowTitle(QApplication::translate("SweetGuiForm", "Form", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class SweetGuiForm: public Ui_SweetGuiForm {};
} // namespace Ui
QT_END_NAMESPACE
#endif // SWEETGUIFORMQ19488_H
Is this the way to do things when brand new?
I think you shouldn't be editing the header file that Qt generates for you. Instead, you should write your own header/implementation file that includes this auto-generated header and sets up the signal-slot connections there. For example:
#include "ui_SweetGuiForm.h"
class SweetGuiForm : public QWidget
{
public:
SweetGuiForm( ): ui( new Ui::SweetGuiForm )
{
ui->setupUi( this );
}
public slots:
void on_buttonBox_accepted( )
{
// your code here
}
protected:
Ui::SweetGuiForm* ui;
};
Qt will find the on_buttonBox_accepted method and set up the signal-slot connection for you, so you don't have to call QObject::connect.