当前位置:网站首页>QT batch operation control and set signal slot
QT batch operation control and set signal slot
2022-07-28 06:32:00 【Schoolmate Xiaotu!】
Preface
qt In many cases, you need to perform batch operations on controls , And want to operate the container separately . If you create a control with code , You cannot get the object name of the control alone , If in ui Drag in the file to create a control , You cannot batch operate . I looked up some information on the Internet , It feels very cumbersome , So I tried to write it myself , There are two main ideas
Situation 1 、 Code creation control , Batch operation and separate operation of controls , And set signal slot
Ideas : Use QVector Template class , Add control to container
The code is as follows :
// Create a container
QVector<QPushButton *> btnList;
for (int i=0; i<6; i++) {
// Add a button for code creation
btnList.push_back(new QPushButton(this));
}
// Batch operations on containers
for (int i=0; i<btnList.size(); i++) {
btnList[i]->setText(QString(" Button %1").arg(i+1));
btnList[i]->move(100,(i+1)*50);
// Set signal slot
connect(btnList[i],&QPushButton::clicked,this,[=](){
QMessageBox::about(this," Tips ",QString(" This is the button %1").arg(i+1));
});
}
You can use QVector Containers , You can also use QList list , The use method is roughly the same . When setting the signal slot , We can also easily get the serial number of the button , Then let it jump to a specific slot .
The effect is as follows :

but ui The controls in the file can also be manipulated in this way , But if ui When there are too many buttons in the file , Adding one by one is also troublesome , Let's look at situation 2
Situation two 、ui Create control in file , Batch operation and separate operation of controls , And set signal slot
Ideas : Get the internal button control through the parent control , Store these spaces in QList perhaps QVector in , Control again
// obtain widget All buttons in
QList<QPushButton *> btnsList = ui->widget->findChildren<QPushButton *>();
// Batch operations on containers
for (int i = 0; i<btnsList.size(); i++)
{
btnsList[i]->setText(QString(" This is the button %1").arg(i+1));
// Set signal slot
connect(btnsList[i],&QPushButton::clicked,this,[=](){
QMessageBox::about(this," Tips ",btnsList[i]->text());
});
}
Be careful : The order of buttons in the list is recursive , The current order of speculation is , From left to right , From top to bottom , The name of the button on the left is the order I created . This needs special attention

It's not easy to code words , If this blog is helpful to you , Please praise the collection , Thank you very much ! There is something wrong , Can comment on the area of communication .
边栏推荐
猜你喜欢

使用wampserver3.2.6时切换中文时造成启动失败

小程序自定义组件-数据,方法和属性

OpenGL quick configuration method

Beta distribution (probability of probability)
![[yolov5] environment construction: win11 + mx450](/img/04/4da9b3748d5f45ef73cffcc48f8609.png)
[yolov5] environment construction: win11 + mx450

机器学习笔记 5 —— Logistic Regression

How many columns are the most suitable for Clickhouse to build a width table?

IMS-FACNN(Improved Multi-Scale Convolution Neural Network integrated with a Feature Attention Mecha

vi和vim命令

The startup fails when switching Chinese when using wampserver3.2.6
随机推荐
A NOVEL DEEP PARALLEL TIME-SERIES RELATION NETWORK FOR FAULT DIAGNOSIS
Word邮件合并功能详解:合并后生成多个word文档,删除空白页
OpenGL的开发环境配置【VS2017】+常见问题
Listener
Vscode中,无法打开源文件 “Adafruit_GFX.h“
Esxi on arm 10/22 update
2021-11-10
保研面试中常见的英语问题有哪些?
自定义组件--插槽
【学习笔记】编码能力
小程序navigator无法跳转(debug)
Matlab simulation of radar imaging 1 - LFM signal and its spectrum
转义字符笔记
Relative path and absolute path
Several methods of QT setting loading interface
OpenGL quick configuration method
EMC experiment practical case ESD electrostatic experiment
Servlet
scrapy 定时执行
clickhouse聚合之探索聚合内部机制