解决Qt发送信号指定重载

现象

信号发送者找不到正确的信号函数

connect(ui->LSpinBox,&QSpinBox::valueChanged,ui->hSlider,&QSlider::setValue);

QSpinBox的valueChanged函数分为int和QString两种,存在函数重载,需让编译器加以区分。
不区分的话会爆出:

error: C2664: 
“QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const: 
无法将参数 2 从“overloaded-function”转换为“const char *

解决方案

使用QOverload

Qt5官方文档推荐使用的方式

 connect(comboBox, QOverload<int>::of(&QComboBox::activated),
      [=](int index){
    /* ... */ });
 connect(comboBox, QOverload<const QString &>::of(&QComboBox::activated),
      [=](const QString &text){
    /* ... */ });
auto qOverload(T functionPointer)
Returns a pointer to an overloaded function. The template parameter is the list of the argument types of the function. functionPointer is the pointer to the (member) function:
     struct Foo {
   
         void overloadedFunction();
         void overloadedFunction(int, const QString &);
     };
     ... qOverload<>(&Foo::overloadedFunction)
     ... qOverload<int, const QString &>(&Foo::overloadedFunction)
If a member function is also const-overloaded qConstOverload and qNonConstOverload need to be used.
qOverload() requires C++14 enabled. In C++11-only code, the helper classes QOverload, QConstOverload, and QNonConstOverload can be used directly:
     ... QOverload<>::of(&Foo::overloadedFunction)
     ... QOverload<int, const QString &>::of(&Foo::overloadedFunction)
Note: Qt detects the necessary C++14 compiler support by way of the feature test recommendations from C++ Committee's Standing Document 6.
This function was introduced in Qt 5.7.
See also qConstOverload(), qNonConstOverload(), and Differences between String-Based and Functor-Based Connections.

使用指针

创建一个函数指针用于保存指定的函数地址:

void (QComboBox:: * activatedInt)(int) = &QComboBox::activated;
void (QComboBox:: * activatedString)(QString) = &QComboBox::activated;
 connect(comboBox, activatedInt,
      [=](int index){
    /* ... */ });
 connect(comboBox, activatedString,
      [=](const QString &text){
    /* ... */ });

相关推荐

  1. 解决Qt发送信号指定重载

    2023-12-08 19:50:03       59 阅读
  2. Qt使用函数指针处理信号和槽函数重载

    2023-12-08 19:50:03       60 阅读
  3. QT绑定信号重载

    2023-12-08 19:50:03       49 阅读
  4. Qt : 如何解决重载引起的歧义

    2023-12-08 19:50:03       38 阅读
  5. Qt 槽函数重载时通过函数指针绑定

    2023-12-08 19:50:03       38 阅读
  6. Qt信号

    2023-12-08 19:50:03       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-08 19:50:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 19:50:03       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 19:50:03       87 阅读
  4. Python语言-面向对象

    2023-12-08 19:50:03       96 阅读

热门阅读

  1. Glide系列-生命周期的监听

    2023-12-08 19:50:03       52 阅读
  2. macOS sandbox 获取用户路径文件夹

    2023-12-08 19:50:03       49 阅读
  3. final, finally, finalize的区别

    2023-12-08 19:50:03       49 阅读
  4. 算法训练营Day9(字符串,以后补KMP)

    2023-12-08 19:50:03       60 阅读
  5. 数据宝库:深入探讨数据隐私与安全的要义

    2023-12-08 19:50:03       51 阅读
  6. React中的页面跳转方式详解

    2023-12-08 19:50:03       51 阅读