1

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 slot QWidget::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 vtable for Window_General

My question is, how I can use button event in this code set?

3
  • 1
    You're not showing how you build the files. The link error is probably caused by not running moc on the file. Commented Mar 20, 2017 at 16:12
  • Sometimes when a vtable error appears, a make distclean is enough to solve the issue. Qt tends to produce support files which doesn't update with some changes Commented Mar 20, 2017 at 16:13
  • The first error is definetely because you are missing Q_OBJECT at the beginning of Windows_General, you should put Q_OBJECT in both classes. Finally clean project and run qmake is a good suggestion. Commented Mar 20, 2017 at 16:18

2 Answers 2

3

It seems that you need to call qmake utility to regenerate moc. Q_OBJECT puts some QObject's overriding member functions declaration to your class, and qmake generates their definitions to yourclass.moc.cpp. Every time you place new Q_OBJECT, you need to call qmake.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please add a code how to call qmake to make your answer more clear?
I have found Run qmake in Qt creator menu and that did the trick, thank you !
2

You can avoid the Q_OBJECT macro altogether by using the pointer-to-member-function syntax instead of the SIGNAL and SLOT macros. For example:

connect(m_button, &QPushButton::released, this, &Window_General::MyhandleButton);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.