c++qtcmakeautomoc

qt private slot moc undefined reference error


I have a simple C++ Qt program and I get undefined reference when I use private slots. Can anyone help me?

I am learning GUI design using Qt5 from the book C++ GUI Programming with Qt4. I am using cmake with MinGW compiler in Windows.

However, if I comment out Q_OBJECT from the header file, then it compiles without error.

This is the header file.

#ifndef MYWIDGETS_H
#define MYWIDGETS_H

#include <QDialog>


class mywidgets : public QDialog
{
    Q_OBJECT
public:

    mywidgets();
    void myDemoWidgets();
    void mySecondDemoWidgets();
private:
    void findLayout();

private slots:
    //void findClicked();
    void enableFindButton(const QString &text);

signals:
    void findNext(const QString &str, Qt::CaseSensitivity cs);
    void findPrevious(const QString &str, Qt::CaseSensitivity cs);
};

#endif // MYWIDGETS_H

This is the error:

myClass_automoc.cpp:-1: error: undefined reference to mywidgets::enableFindButton(QString const&)

Solution

  • The undefined reference error was because I did not define the void mywidgets::enableFindButton(const QString &text) { } function in the (.cpp) file. The implementation is given here. void mywidgets::enableFindButton(const QString &text){ findButton->setEnabled(!text.isEmpty()); }