This question is pretty sure will seem as duplicate, but after doing a research and looking at same questions I found that it is needed to use macro Q_OBJECT for SLOT using but when I use it I got a compilation error. Here are my code snippets.
main.cpp
#include <QApplication>
#include "window_general.h"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
Window_General window_general;
window_general.show();
return app.exec();
}
windows_general.h
#ifndef WINDOW_GENERAL_H
#define WINDOW_GENERAL_H
#include <QWidget>
#include <QApplication>
class QPushButton;
class Window_General : public QWidget
{
public:
explicit Window_General(QWidget *parent = 0);
private slots:
void MyhandleButton();
private:
QPushButton *m_button;
};
#endif // WINDOW_GENERAL_H
windows_general.cpp
#include "window_general.h"
#include <QPushButton>
Window_General::Window_General(QWidget *parent) :
QWidget(parent)
{
// Set size of the window
setFixedSize(800, 500);
// Create and position the button
m_button = new QPushButton("Hello World", this);
m_button->setGeometry(10, 10, 80, 30);
connect(m_button, SIGNAL (released()), this, SLOT (MyhandleButton()));
}
void Window_General::MyhandleButton()
{
m_button->setText("Example");
m_button->resize(100,100);
}
In this code I have a runtime error:
QObject::connect: No such slotQWidget::MyhandleButton()in ../prj/window_general.cpp:14
If I put a Q_OBJECT macro at here,
class Window_General : public QWidget
{
Q_OBJECT
public:
explicit Window_General(QWidget *parent = 0);
private slots:
void MyhandleButton();
private:
QPushButton *m_button;
};
I then have this error:
D:\work\my_qt\prj\window_general.h:8: error: undefined reference to
vtableforWindow_General
My question is, how I can use button event in this code set?