Search code examples
visual-studio-2008qtsignals-slotsqobject

Not able to find quit slot, and use Q_OBJECT macro


I am using VS2008 with QT 4.7.1 and add-ins. I am new to this environment.
I managed to do necessary setting and run simple "hello world". But when I try to use simple quit() slot on click of a button, I failed. Also it results in build failed when trying to use Q_OBJECT.
After commenting Q_OBJECT code is built and debugged. Now it shows

QObject::connect : no such slot QWidget::quit() in .\main.cpp found.

below is my code

#include <QtGui>
#include "QtGui\QApplication"
#include "QObject"


class Notepad : public QWidget
{
    //Q_OBJECT 
public:
    Notepad();
    private slots:
        void quit();

private:
    QTextEdit *textEdit;
    QPushButton *quitButton;
}; 

Notepad::Notepad()
{
    textEdit = new QTextEdit;
    quitButton = new QPushButton(tr("Quit"));
    connect(quitButton, SIGNAL(clicked()), this, SLOT(quit() ));
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(textEdit);
    layout->addWidget(quitButton);

    setLayout(layout);

    setWindowTitle(tr("Notepad"));
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Notepad nt;// = new Notepad();
    nt.show();

    return a.exec();
}

I have searched through net but failed to get reasonable solution. Most of the solution are for working with qmake on command line. Also I am able to find .pro file for the project.

Any help is appreciated.


Solution

  • Nitesh: You need Q_OBJECT macro for the slots to work properly, the MOC compiles every header that contains Q_OBJECT into moc_*.cpp file. Add that moc*.cpp to your project and everything should work fine. The unresolved external means that you are missing the definition of the function, did you define it anywhere?