Qt:no matching function for call to (类名)::connect()的错误原因总结

时间:2022-06-30 18:45:52


          最近初学Qt,在做项目的过程中遇到  E:\QT Project\WinCalculater\mainwindow.cpp:132: 错误:no matching function for call to 'MainWindow::connect(MyButton*&, void (MyButton::*)(), MainWindow* const, const char*)' 的问题,然后我就上网查错误的原因,但是查到的答案都没有解决我的问题,后来自己发现了问题所在,故在此与大家分享下。

错误代码:

connect(but0,&MyButton::clicked,this,SLOT(button1()));
错误原因:

       这样不满足connect()五个重载函数的参数要求,也就是参数列表出了问题。

 在 Qt 5 中,QObject::connect()有五个重载:

QMetaObject::Connection connect(const QObject *, const char *,
const QObject *, const char *,
Qt::ConnectionType);

QMetaObject::Connection connect(const QObject *, const QMetaMethod &,
const QObject *, const QMetaMethod &,
Qt::ConnectionType);

QMetaObject::Connection connect(const QObject *, const char *,
const char *,
Qt::ConnectionType) const;

QMetaObject::Connection connect(const QObject *, PointerToMemberFunction,
const QObject *, PointerToMemberFunction,
Qt::ConnectionType)

QMetaObject::Connection connect(const QObject *, PointerToMemberFunction,
Functor);

我的错误是混用了第一个和第四个重载函数: 信号&MyButton::clicked是PointerToMemberFunction类型的,槽函数SLOT(button1())是const char * 类型的,所以会报错。

         因为我的button1()是自定义的private slots:的函数,所以我把SLOT(button1())换成&MainWindow::button1就可以了。