开发者

Not able to find quit slot, and use Q_OBJECT macro

开发者 https://www.devze.com 2023-04-03 09:21 出处:网络
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

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[])
{
    QAppli开发者_C百科cation 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.


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?


Move the declaration of Notepad to a header (say, notepad.h), reenable the Q_OBJECT, then add to your .pro file:

HEADERS += notepad.h

Rerun qmake, then it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号