Preliminary understanding of Qt signal mapping mechanism

Qt
's understanding of signal mapping, signal mapping is a method used to process signals in Qt. It will be troublesome when multiple similar signals need to use a signal method. At this time, we will use the signal mapping mechanism of Qt. Its principle is to use something equivalent to a bridge to connect the slot functions to be executed by each command. This is called signal mapping. Next, let's look at a classic signal mapping example.
// we expect to see the effect of a 12-button, and normally use 12 clicked method as well as bath method to deal with, but we use the signal mapping method only needs his original response plus signal mapping method of the bridge
this This is our test interface.
Insert picture description here
We clicked 2 to respond to a corresponding slot method, and
Insert picture description here
we clicked 0 to respond to a corresponding slot method.Insert picture description here

head File

widget.h

#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
Q_OBJECT

public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void DoClicked(const QString &str);

private:
Ui::Widget *ui;
QGridLayout *m_layout;
QPushButton *m_btn;
QSignalMapper *m_smapper;
QLineEdit *line;
};
#endif // WIDGET_H

widget.cpp
#include “widget.h”
#include “ui_widget.h”
#define QSTR QString::fromLocal8Bit
#include
#include
#include

//The goal is
/*
**edit column
1 2 3

4 5 6

7 8 9

. 0 C
*/

//1, use signal mapping
//2, use 12 buttons

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);

line = new QLineEdit(this);
m_smapper = new QSignalMapper(this);
QString text;
m_layout = new QGridLayout;
m_btn = new QPushButton[12];
text = "1,2,3,4,5,6,7,8,9,.,0,C";
//采用QStringList 来初始化我们按钮上面的文本## 标题
QStringList store = text.split(",");//用split方法将以","为分隔符,分隔里面的字符
QLabel *label = new QLabel;
label->setText(QSTR("测试"));

//Button layout, using m_layout to initialize the button layout according to the ranks
for (int i = 0; i <store.size(); i++) { (m_btn + i)->setText(store.at(i));

    m_layout->addWidget((m_btn + i), 1 + (i / 3), i % 3);//通过指针的方式找到每一个按钮


    connect((m_btn + i), SIGNAL(clicked()), m_smapper, SLOT(map()));//这里是信号与槽,点击信号按钮发
    //发射点击信号,到m_samapper相当于一个桥梁,SLOTmap(()),相应响应到下面的SLOT(mapped)方法
    //mappper做信号,
    m_smapper->setMapping(m_btn + i, store[i]);//给信号映射设置一个法则,既然是映射每个特定的 槽函数
    //然需需要有特定的法则对应,这里我们采用的是btn+i,然后后面是以store[i]里内容作为区别点
    //不同的btn上面按的有不同的字符,然后我们把这12个按钮都添加上去
}


m_layout->addWidget(label, 5, 0);
m_layout->addWidget(line, 0, 0, 1, 4);//设置标签布局,四个参数意思,在哪一行哪一列,跨越几行跨越几列
//为什么要用后面两个参数呢,因为我们第一行是编辑栏,而且我想要它一整行,所以加上后面的参数,
//4代表它跨越4列就是一整行了
connect(m_smapper, SIGNAL(mapped(const QString &)), this, SLOT(DoClicked(const QString &)));
//mapper做信号响应不同的槽方法,这样就避免了12个按钮用12个槽函数,这里用2个槽函数解决问题,

setLayout(m_layout);

}

Widget::~Widget()
{
delete[] m_btn;
delete ui;
}

void Widget::DoClicked(const QString &str)
{
QMessageBox::information(this, " Cliked", str + " isPushed");
}

main.cpp
#include “widget.h”

#include

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(450, 250);
w.show();
return a.exec();
}

Guess you like

Origin blog.csdn.net/weixin_45825875/article/details/109428778