Search code examples
c++qtqt4signals-slots

Slot seemingly not recognized in Qt app


I have been working on learning C++ and Qt4 recently, but I have hit a stumbling block.

I have the following class and implementation:

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

public slots:
    void run();

private:
    //...
};

and

Window::Window()
{
    //...

    connect(runBtn,SIGNAL(clicked()),this,SLOT(run()));

    //...
}
Window::run()
{
    //...
}

However, when I attempt to build and run it, although it builds just fine, it immediately exits out with the message

Object::connect: No such slot QWidget::run()

Unless I did something wrong, Qt does not seem to be recognizing the slot run()

Could anyone please help?


Update:

The code is now:

class Window : public QWidget
{
    Q_OBJECT
public:
    Window(QWidget *parent = 0);

public slots:
    void run();

private:
    //...
};

and

Window::Window(QWidget *parent) : QWidget(parent)
{
    //...

    connect(runBtn,SIGNAL(clicked()),this,SLOT(run()));

    //...
}
Window::run()
{
    //...
}

The program still "unexpectedly finished", but no longer tell me that there is no such thing as QWidget::run()


Solution

  • Possibly you have forgotten a Q_OBJECT macro in your Window class?

    class Window : public QWidget
    {
    Q_OBJECT
    public:
        Window()
    ...