在项目中动态添加了一个QComboBox,在当准备关联currentIndexChanged信号到某个成员函数中时,发现currentIndexChanged有两个重载:
void currentIndexChanged(int index); void currentIndexChanged(const QString &);
如果直接关联,例:
connect(comboBox, &QComboBox::currentIndexChanged,this, &Dialog::comBoxChangeIndex);
会报:
error: no matching member function for call to 'connect'
需要把对应的函数强转一下,指定使用currentIndexChanged函数的那个重载:
connect(comboBox, (void (QComboBox::*)(int))&QComboBox::currentIndexChanged, this, (void (Dialog::*)(int))&Dialog::comBoxChangeIndex);
connect(comboBox, (void (QComboBox::*)(int))&QComboBox::currentIndexChanged, this, (void (Dialog::*)(int))&Dialog::comBoxChangeIndex);
这样,就指定了要用QComboBox的currentIndexChanged(int)重载,如果想使用currentIndexChanged(const QString &)重载,把(void (QComboBox::*)(int))改成(void (QComboBox::*)(const QString &))
例:
connect(combobox, (void (QComboBox::*)(int))&QComboBox::currentIndexChanged, this, (void (Setting::*)(int))&Setting::typeComBoxCurrentIndexChanged); connect(combobox, (void (QComboBox::*)(const QString &))&QComboBox::currentIndexChanged, this, (void (Setting::*)(QString))&Setting::typeComBoxCurrentIndexChanged);
这里Setting类的typeComBoxCurrentIndexChanged函数也有两个重载,对应QComboBox的currentIndexChanged函数两个重载。
void typeComBoxCurrentIndexChanged(int) void typeComBoxCurrentIndexChanged(QString)