#include “toolbarwidget.h”
#include “ui_toolbarwidget.h”
#include
#include
#include
ToolBarWidget::ToolBarWidget(QWidget parent) :
QWidget(parent),
ui(new Ui::ToolBarWidget)
{
ui->setupUi(this);
// QSignalMapper mapper = new QSignalMapper(this);
// QList<QPushButton*> buttonList = findChildren<QPushButton*>();
// for(auto btn: buttonList){
//(尤其是Qt6)中,QSignalMapper 类已被弃用并移除。
//这种写法在QT6上面已经被废弃了
// connect(btn, SIGNAL(clicked()), mapper, SLOT(map()));
// mapper->setMapping(btn, btn->text());
// }
// connect(mapper, SIGNAL(mapped(QString)), this, SIGNAL(buttonClicked(QString)));
QList<QPushButton*> buttonList = findChildren<QPushButton*>();
for(auto btn : buttonList) {
// 使用Lambda直接捕获按钮文本
connect(btn, &QPushButton::clicked, this, [this, btn]() {
emit buttonClicked(btn->text());
});
}
}
ToolBarWidget::~ToolBarWidget()
{
delete ui;
}