Visual Studio C++ Qt slot connect error error C2665 on QComboBox::currentIndexChanged

5jdjgkvh  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(146)

Visual Studio说这里是错误C2665。但是为什么呢?我100%确定我的代码是正确的。QComboBox有一个只接受一个int参数的信号currentIndexChanged。我有一个只接受一个int参数的插槽。那么连接它们有什么问题呢?

//Defined in ui_MyDialog.h
QComboBox *messageType;

...

//Constructor of MyDialog
connect(ui.messageType, &QComboBox::currentIndexChanged, this, &MyDialog::IndexChanged, Qt::QueuedConnection);

...

//Prototype of signal in MyDialog class definition
public slots:
    void IndexChanged(int index);

...

//Definition of IndexChanged signal
void MyDialog::IndexChanged(int index)
{

}

错误文本:

1>MyDialog.cpp(61,119): error C2665: QObject::connect: none of the 3 overloads can convert all argument types
1>F:\Qt\5.15.2\msvc2019_64\include\QtCore\qobject.h(225,36): message : can be "QMetaObject::Connection QObject::connect(const QObject *,const QMetaMethod &,const QObject *,const QMetaMethod &,Qt::ConnectionType)" (source file MyDialog.cpp is compiled)
1>F:\Qt\5.15.2\msvc2019_64\include\QtCore\qobject.h(222,36): message : or "QMetaObject::Connection QObject::connect(const QObject *,const char *,const QObject * ,const char *,Qt::ConnectionType)" (compiles source file MyDialog.cpp)
1>MyDialog.cpp(61,5): message : "QMetaObject::Connection QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)": cannot convert argument 2 from "overloaded-function" to "const char *"
1>MyDialog.cpp(61,41): message : Context does not allow overloaded function disambiguation
1>F:\Qt\5.15.2\msvc2019_64\include\QtCore\qobject.h(222,36): message : see "QObject::connect" declaration (source file MyDialog.cpp is compiled)
1>MyDialog.cpp(61,119): message : when trying to match argument list "(QComboBox *, overloaded-function, MyDialog *, void (__cdecl MyDialog::* )(int), Qt::ConnectionType)"
osh3o9ms

osh3o9ms1#

编译器对插槽和信号一无所知,所以它不会比较插槽签名和信号签名。如果某个插槽或信号有重载,指向这个插槽的链接或指针将是不明确的。为了避免这种情况,我们需要显式指定我们在这里使用的重载方法,使用QOverload
连接此信号的正确方法:

connect(ui.messageType, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MyDialog::IndexChanged, Qt::QueuedConnection);

相关问题