1

So i have a custom class extending QWidget. I have tried adding a custom slot to it. However it keeps giving me the error:

"QObject::connect: No such slot QWidget::closeSlot() in ..\menuTest\menu.cpp:13".

My code is:

#include "menu.h"

Menu::Menu()
{
    exitButton = new QPushButton;
    exitButton->setText(tr("Exit"));

    connect(exitButton, SIGNAL(clicked()), this, SLOT(closeSlot()));

    QVBoxLayout * layout = new QVBoxLayout;
    layout->addWidget(exitButton);
    this->setLayout(layout);
}

void Menu::closeSlot()
{
    qDebug() << "I'm inside";
}

and the header file:

#ifndef MENU_H
#define MENU_H

#include "allIncludes.h"

class Menu : public QWidget
{
public:
    Menu();

private:
QPushButton * exitButton;

private slots:
    void closeSlot();
};

#endif // MENU_H
Silnik
  • 328
  • 2
  • 4
  • 14

1 Answers1

6

For a class to be recognised by Qt's meta-object system, it must include the Q_OBJECT macro:

class Menu : public QWidget
{
    Q_OBJECT
public:
    Menu();

private:
QPushButton * exitButton;

private slots:
    void closeSlot();
};
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455