1

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&)
KernelPanic
  • 2,328
  • 7
  • 47
  • 90
sutradhar
  • 93
  • 1
  • 8
  • Can you show us implementation (`.cpp`) file, did you declare `void enableFindButton(const QString &text);` in `.cpp` file as `void mywidgets::enableFindButton(const QString &text) {}` for start. Once you get rid of error, you can put code inside `slot`'s body. – KernelPanic May 16 '16 at 20:15
  • @KernelPanic the slot function is used in connect() function. `connect(lineLayout, SIGNAL(textChanged(const QString &)), this, SLOT(enableFindButton(const QString &)));` – sutradhar May 16 '16 at 20:31
  • Please add additional information back into the question, rather than in comments. – jonspaceharper May 16 '16 at 20:33
  • I'm unfamiliar with automoc, but private (as opposed to protected) slots are rare due to Qt's extensive use of the PIMPL idiom. Try making the slot protected and see if automoc still chokes. – jonspaceharper May 16 '16 at 20:37
  • To me it looks like your cpp file is missing the definition of the slot. You must define it like any other function, moc isn't involved in this. – Frank Osterfeld May 16 '16 at 21:01
  • @KernelPanic @Frank Osterfeld thank you. It works. I did not know that without defining the `void enableFindButton(.)` it will show automoc error. I searched automoc error help from other posts in stackoverflow but most of them are related to cmake. So I was confused. Thank you all! now it works. – sutradhar May 16 '16 at 23:31

2 Answers2

3

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()); }

sutradhar
  • 93
  • 1
  • 8
0

Can you show us implementation (.cpp) file, did you declare void enableFindButton(const QString &text); in .cpp file as
void mywidgets::enableFindButton(const QString &text) { }
for start. Once you get rid of error, you can put code inside slot's body.

KernelPanic
  • 2,328
  • 7
  • 47
  • 90