c++ - 如何在单击各种编号的按钮时更新 QLabel
问题描述
我有一个带有数字 1-9 和 0 的拨号盘,上面有一个 QLabel,用于在单击时显示数字(与任何电话上的键盘相同)。都是按钮。什么是让 QLabel 在单击按钮时显示数字的最简单方法?
例如,如果单击 2 然后 0 然后 7,则标签将实时更新为 207。Qlabel 的格式应遵循标准电话号码,000-000-0000。我了解如何一次为一个数字设置文本,但它们一直相互覆盖。任何帮助表示赞赏。先感谢您
解决方案
您正在寻找的是一个QSignalMapper
. 它通过单个接口映射多个输入,并为您进行发送者调度。
QSignalMapper *mapper(new QSignalMapper(parent));
for (int i=0; i<10; ++i){
QPushButton *button = some_new_button_function();
connect(button, &QPushButton::clicked, mapper, &QSignalMapper::map);
mapper->setMapping(button, i);
}
connect(mapper, QOverload<int>::of(&QSignalMapper::mapped),
[this](int i){/*here your append code*/});
推荐阅读
- arrays - 这个数组元素访问语法是如何工作的?
- sql - 如何使用sql查询选择excel表中的数据插入到mysql数据库中?
- node.js - 使用数组为大数据设计 DynamoDB。超过 400k
- asp.net-core - 错误:- 值不能为空参数名称:.Net Core 中的ConnectionString 用于两个数据库连接
- python - 出现错误:没有找到带有标签的句柄放在图例中。使用 kdeplot 和 searborn 进行绘图时,将图例移出框外
- c# - 实体框架选择多表一对多
- sql - Oracle 将时间戳转换为日期
- python - 这里的 return -1 是什么意思?
- python - 当我返回的变量有一个与之关联的字符串时,我的函数返回 None
- sql - SQL JOIN 两个表并返回对应的like