1、适用场景

Qt在编程过程中,不免有时候会批量的创建不异的控件,例如创建10个按钮,常规写法如下:

QPushButton* Btn;QVector<QPushButton*> Btn_Vec;for(int i=0; i<10; i++){ Btn = new QPushButton(QString("%1").arg(i)); Btn_Vec.push_back(Btn);}connect(Btn_Vec[0],SIGNAL(clicked()),this,SLOT(slot_Btn_0()));connect(Btn_Vec[1],SIGNAL(clicked()),this,SLOT(slot_Btn_1()));connect(Btn_Vec[2],SIGNAL(clicked()),this,SLOT(slot_Btn_2()));connect(Btn_Vec[3],SIGNAL(clicked()),this,SLOT(slot_Btn_3()));connect(Btn_Vec[4],SIGNAL(clicked()),this,SLOT(slot_Btn_4()));connect(Btn_Vec[5],SIGNAL(clicked()),this,SLOT(slot_Btn_5()));connect(Btn_Vec[6],SIGNAL(clicked()),this,SLOT(slot_Btn_6()));connect(Btn_Vec[7],SIGNAL(clicked()),this,SLOT(slot_Btn_7()));connect(Btn_Vec[8],SIGNAL(clicked()),this,SLOT(slot_Btn_8()));connect(Btn_Vec[9],SIGNAL(clicked()),this,SLOT(slot_Btn_9()));

上面的写法长短常繁琐的,Qt供给了一个类QSignalMapPEr,用于信号分发,类似于信号直达站:

Qt信号分发(QSignalMapper)  第1张

按钮被点击触发信号激活 QSignalMapper的槽函数map(),map()会同一释放一个带有标识表记标帜的信号,该信号触发定义的按钮槽函数,按照标识表记标帜(ID、QWidget、QString)去判断到底是哪个按钮被点击了。

2、效果展现Qt信号分发(QSignalMapper)  第2张

3、详细代码

QT开发交换君羊:714620761

#ifndef#define#include <QMainWindow>#include <QWidget>#include <QDebug>#include <QTime>#include <QPushButton>#include <QSignalMapper>#include <QGridLaYOUt>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); void sleep(int sectime);public slots: void Btn_handle(int);PRivate: Ui::MainWindow *ui; QWidget* widget; QSignalMapper* signalMapper; QPushButton* Btn; QVector<QPushButton*> Btn_Vec;};#endif// MAINWINDOW_H#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); widget = new QWidget; this->setCentralWidget(widget); QGridLayout* Grid_Layout = new QGridLayout(widget); signalMapper = new QSignalMapper(this); for(int i=0; i<10; i++) { Btn = new QPushButton(QString("%1").arg(i)); Btn_Vec.push_back(Btn); signalMapper->setMapping(Btn,i); connect(Btn,SIGNAL(clicked()),signalMapper,SLOT(map())); Grid_Layout->addWidget(Btn,i,0); } connect(signalMapper,SIGNAL(mapped(int)),this,SLOT(Btn_handle(int)));}MainWindow::~MainWindow(){ delete ui;}void MainWindow::Btn_handle(int index){ switch(index) { case 0: { Btn_Vec[0]->setText("按下"); sleep(500); Btn_Vec[0]->setText("0"); break; } case 1: { Btn_Vec[1]->setText("按下"); sleep(500); Btn_Vec[1]->setText("1"); break; } case 2: { Btn_Vec[2]->setText("按下"); sleep(500); Btn_Vec[2]->setText("2"); break; } case 3: { Btn_Vec[3]->setText("按下"); sleep(500); Btn_Vec[3]->setText("3"); break; } case 4: { Btn_Vec[4]->setText("按下"); sleep(500); Btn_Vec[4]->setText("4"); break; } case 5: { Btn_Vec[5]->setText("按下"); sleep(500); Btn_Vec[5]->setText("5"); break; } case 6: { Btn_Vec[6]->setText("按下"); sleep(500); Btn_Vec[6]->setText("6"); break; } case 7: { Btn_Vec[7]->setText("按下"); sleep(500); Btn_Vec[7]->setText("7"); break; } case 8: { Btn_Vec[8]->setText("按下"); sleep(500); Btn_Vec[8]->setText("8"); break; } case 9: { Btn_Vec[9]->setText("按下"); sleep(500); Btn_Vec[9]->setText("9"); break; } }}void MainWindow::sleep(int sectime){ QTime dieTime = QTime::currentTime().addMSecs(sectime); while (QTime::currentTime() < dieTime) { QCoreApplication::processEvents(QEventLoop::AllEvents, 100); }}