首页 > 解决方案 > 如何在单击各种编号的按钮时更新 QLabel

问题描述

我有一个带有数字 1-9 和 0 的拨号盘,上面有一个 QLabel,用于在单击时显示数字(与任何电话上的键盘相同)。都是按钮。什么是让 QLabel 在单击按钮时显示数字的最简单方法?

例如,如果单击 2 然后 0 然后 7,则标签将实时更新为 207。Qlabel 的格式应遵循标准电话号码,000-000-0000。我了解如何一次为一个数字设置文本,但它们一直相互覆盖。任何帮助表示赞赏。先感谢您

标签: c++qt

解决方案


您正在寻找的是一个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*/});

推荐阅读