Qt class MyButton when use Q_PRIVATE_SLOT, the .h file compile error: undefined MyButtonPrivate

168 Views Asked by At

https://www.dvratil.cz/2019/11/q-private-slot-with-new-connect-syntax/

#include <QPushButton>
#include <memory>
class MyButtonPrivate;
class MyButton : public QPushButton {
    Q_OBJECT
public:
    explicit MyButton(QWidget* parent);
    ~MyButton() noexcept override;

private:
    std::unique_ptr<MyButtonPrivate> const d_ptr;
    Q_DECLARE_PRIVATE(MyButton);

    Q_PRIVATE_SLOT(d_func(), void onClicked(bool));
};

this is the .h file, compile error: undefined MyButtonPrivate. truely, the moc_MyButton.cpp(auto generated by compiler) doesnot include the MyButtonPrivate. then what's wrong, and how to solve?

2

There are 2 best solutions below

1
Catcow On

I'm not sure why it's an error, but according to https://forum.qt.io/topic/61295/solved-what-is-the-usage-of-q_private_slot/3, with Qt > 5 and C++11 it is a useless macro.

0
xiaosan On

the moc file generated:

#include "../../MyButton.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
...
case 0: _t->d_func()->onClicked((*reinterpret_cast< bool(*)>(_a[1]))); break;
...

notice: _t->d_func()->onClicked invoke the private class's func(but no include)

so absolutely result in compile error. the question is moc file doesnt include MyButton_p.h, and manually add the include:

#include "../../MyButton.h"
#include "../../MyButton_p.h"
...

then its ok(but the moc file shouldnt modify manually)