当前位置:网站首页>qt批量操作控件,并设置信号槽
qt批量操作控件,并设置信号槽
2022-07-28 05:25:00 【小土同学!】
前言
qt中很多情况都需要对控件进行批量操作,又想对容器进行单独操作。如果用代码创建控件,则无法单独拿到控件的对象名,如果在ui文件中拖动创建控件,则无法批量操作。网上查了一些资料,感觉都写的非常繁琐,于是自己尝试写了一下,主要有两种思路
情况一、代码创建控件,对控件批量操作和单独操作,并设置信号槽
思路:使用QVector模板类,将控件添加到容器
代码如下:
//创建容器
QVector<QPushButton *> btnList;
for (int i=0; i<6; i++) {
//添加代码创建的按钮
btnList.push_back(new QPushButton(this));
}
//对容器批量操作
for (int i=0; i<btnList.size(); i++) {
btnList[i]->setText(QString("按钮 %1").arg(i+1));
btnList[i]->move(100,(i+1)*50);
//设置信号槽
connect(btnList[i],&QPushButton::clicked,this,[=](){
QMessageBox::about(this,"提示",QString("这是按钮 %1").arg(i+1));
});
}
你可以使用QVector容器,也可以使用QList列表,使用方法大致一样。在设置信号槽的时候,我们也可以轻易获取该按钮的序号,然后让其跳转到特定的槽。
效果如下:

但ui文件里的控件也可以这样操控,但如果在ui文件中有太多的按钮时,一个个添加也很麻烦,下面看情况二
情况二、ui文件中创建控件,对控件批量操作和单独操作,并设置信号槽
思路:通过父控件来获取内部的按钮控件,将这些空间存入QList或者QVector中,再进行操控
//获取widget中所有的按钮
QList<QPushButton *> btnsList = ui->widget->findChildren<QPushButton *>();
//对容器批量操作
for (int i = 0; i<btnsList.size(); i++)
{
btnsList[i]->setText(QString("这是按钮 %1").arg(i+1));
//设置信号槽
connect(btnsList[i],&QPushButton::clicked,this,[=](){
QMessageBox::about(this,"提示",btnsList[i]->text());
});
}
注意:按钮在列表中的顺序是以递归方式进行的,目前猜测的顺序是,从左到右,从上到下,左边按钮的名称是我创建的顺序。这一点需要格外注意

码字不易,如果这篇博客对你有帮助,麻烦点赞收藏,非常感谢!有不对的地方,可以评论区交流。
边栏推荐
- MATLAB signal processing
- 简述EMD分解、希尔伯特变换、谱方法
- Arduino reads the analog voltage_ How mq2 gas / smoke sensor works and its interface with Arduino
- Selection of PLC
- 天线效应解决办法
- Low power design isolation cell
- Talk about the "hybrid mode" of esxi virtual switch and port group
- Bag of Tricks训练卷积网络的技巧
- A NOVEL DEEP PARALLEL TIME-SERIES RELATION NETWORK FOR FAULT DIAGNOSIS
- EfficientNET_ V1
猜你喜欢

Detailed explanation of word mail merge function: after merging, multiple word documents are generated and blank pages are deleted

听说你也在实习|当我采访了几个大三实习生之后。

Triode design, understanding saturation, linear region and cut-off region

ICC2分析时序的神器 analyze_design_violations

Fluke dtx-1800 and its accessories dtx-cha002 channel adapter channel replacement RJ45 socket notes

论文神器 VS Code + LaTex + LaTex Workshop

CLIP Learning Transferable Visual Models From Natural Language Supervision

简述EMD分解、希尔伯特变换、谱方法

Trouble encountered in cable testing -- a case study of a manufacturer?

Detailed explanation of creepage distance and electrical clearance
随机推荐
Selection of PLC
BERT基于transformer的双向编码器
What are the common English questions in the postgraduate interview?
Efficient Net_V2
The short jumper dsx-8000 test is normal, but the dsx-5000 test has no length display?
For a large amount of data, matlab generates Excel files and typesetting processing source code
Perl入门学习(九)引用
AEM testpro K50 and south Guangdong survey
【YOLOv5】环境搭建:Win11 + mx450
Learning notes on hardware circuit design 2 -- step-down power circuit
mixup_ratio
Precautions for EMI design of switching power supply circuit in layout process
CString to char[] function
低功耗设计-Power Switch
USB network native driver for esxi updated to support esxi7.0.1
Low power design -power switch
ConNeXt
Synopsys Multivoltage Flow
In asp Usage of cookies in. Net
Example of frameset usage