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开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓
上面的写法是非常繁琐的,Qt提供了一个类QSignalMapper,用于信号分发,类似于信号中转站:
按钮被点击触发信号激活 QSignalMapper的槽函数map(),map()会统一释放一个带有标记的信号,该信号触发定义的按钮槽函数,根据标记(ID、QWidget、QString)去判断到底是哪个按钮被点击了。
2、效果展示
3、具体代码
#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_OBJECT
public:
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);
}
}
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓