当前位置: 首页 > news >正文

qt 重载信号,使用““方式进行connect()调用解决方案

问题

在Qt中,重载的信号默认是无法使用&这种方式调用的。
因为&只能绑定到一个具体的信号,而重载的信号名称相同,编译器无法确定要绑定哪一个信号。

解决方案

如果非要使用&绑定重载的信号,可以使用函数指针进行转换,指定参数类型和个数,例如:

signals:void sendMsg(int n);void sendMsg(QString str);connect(this, static_cast<void (MainWindow::*)(const int)>(&MainWindow::sendMsg), this, [=](int n){qDebug().noquote() << "[" << __FILE__ << __LINE__ << "]" << "n :" << n;});connect(this, static_cast<void (MainWindow::*)(const QString)>(&MainWindow::sendMsg), this, [=](QString str){qDebug().noquote() << "[" << __FILE__ << __LINE__ << "]" << "str :" << str;});

示例

.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = nullptr);~MainWindow();signals:void sendMsg(int n);void sendMsg(QString str);private:Ui::MainWindow *ui;
};#endif // MAINWINDOW_H

.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QDebug>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);connect(this, static_cast<void (MainWindow::*)(const int)>(&MainWindow::sendMsg), this, [=](int n){qDebug().noquote() << "[" << __FILE__ << __LINE__ << "]" << "n :" << n;});connect(this, static_cast<void (MainWindow::*)(const QString)>(&MainWindow::sendMsg), this, [=](QString str){qDebug().noquote() << "[" << __FILE__ << __LINE__ << "]" << "str :" << str;});emit sendMsg(1);emit sendMsg("helloworld");
}MainWindow::~MainWindow()
{delete ui;
}

main.cpp

#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}

结果

在这里插入图片描述

http://www.lryc.cn/news/233730.html

相关文章:

  • 阿里云+宝塔部署项目(Java+React)
  • Linux_系统信息_uname查看内核版本、内核建立时间、处理器类型、顺便得到操作系统位数等
  • screen中conda激活环境后登录jupyter notebook导入包提示找不到,但是在命令行中就可以导入包
  • 基于SSM的中小型企业财务管理设计与实现
  • 工厂模式之简单工厂模式(常用)
  • Kafka入门教程与详解(一)
  • GoFrame学习随便记1
  • 最新自动定位版本付费进群系统源码
  • freeswitch的一个性能问题
  • 各机构如何加强网络渗透、“渗透”防御
  • Docker命令 常用中间件运维部署,方便构建自己服务
  • Android——gradle构建知识片-散装版
  • 3.3 Windows驱动开发:内核MDL读写进程内存
  • 开源与闭源:驾驭大模型未来的关键决断
  • 面向对象成员之属性
  • 第六十二周周报
  • 【机器学习】 特征工程:特征预处理,归一化、标准化、处理缺失值
  • 【深度学习实验】网络优化与正则化(七):超参数优化方法——网格搜索、随机搜索、贝叶斯优化、动态资源分配、神经架构搜索
  • 简单漂亮的首页
  • SSM项目初始化流程与操作概念解释-SpringBoot简化版
  • Angular 路由无缝导航的实现与应用(六)
  • quickapp_快应用_tabBar
  • PCL_点云分割_基于法线微分分割
  • 计算机毕业论文内容参考|基于深度学习的交通标识智能识别系统的设计与维护
  • SELinux零知识学习十六、SELinux策略语言之类型强制(1)
  • 轻量封装WebGPU渲染系统示例<34>-数据驱动之Json构建场景
  • 全局异常拦截和Spring Security认证异常的拦截的顺序
  • Hive Lateral View explode列为空时导致数据异常丢失
  • 音频类型转换工具-可执行文件exe/dmg制作
  • 【Proteus仿真】【51单片机】公交车报站系统