当前位置:网站首页>QT implements the delete method of the container
QT implements the delete method of the container
2022-07-07 11:17:00 【InfoQ】
Summary of premises
When writing interface , You cannot avoid using the same type repeatedly , And you only need to use it once , No further application . When we finish using a container , Or when you need to empty a container, you need to delete the contents one by one . If you write directly in the interface QCheckBox *box=new QCheckBox(this); Instead of delete, It is easy to cause memory leakage . So we store it in vector in , We can delete it all by traversing in the destructor , This can not only simplify our memory management work , Reduce the risk of memory leaks .
vector advantage
- He can store at will , It has unlimited advantages .
- After deleting a certain data in the middle , Subsequent data will be automatically supplemented , No waste .
delete Method 1 of container :
for (std::vector<QCheckBox*>::iterator itvet = m_vetAddCheckBoxControls.begin(); itvet != m_vetAddCheckBoxControls.end(); itvet++)
{
if(*itvet)
{
delete *itvet;
*itvet = nullptr;
}
}
m_vetAddCheckBoxControls.clear();
The most traditional way , You can do this in the destructor , It traverses , Delete every item of data in the container , Finally, just in case , The container clear().
delete Method 2 of container :
vector<QCheckBox*>().swap(m_vetAddCheckBoxControls);
This method is not only applicable to vector, The same applies to QMAp, Only need to vector<QCheckBox*>() Replace with QMap<QCheckBox*,bool>() That's all right. .
This method can be interspersed in the code , It does not affect the later use .
summary
These two methods cannot be used at the same time , Only one of the two , When I first used the second method, I still kept the first method in the destructor . As a result, errors are always reported when closing the interface and no solution can be found . This is also a big hole I stepped into . Both methods have their own advantages , You can choose to use it according to your own situation . The first one is applicable to emptying processing that does not need to be used during program operation , The second is more appropriate .
This is the end of today's update , I hope you guys will criticize and correct !
边栏推荐
- Ping tool ICMP message learning
- Galaxy Kirin desktop operating system installation postgresql13 (source code installation)
- uniapp 在onLaunch中跳转页面后,点击事件失效解决方法
- MIF file format record
- 科普达人丨一文弄懂什么是云计算?
- 创意信息获2家机构调研:GreatDB 数据库已在9地部署
- Ffmpeg record a video command from RTSP
- JSON format query of MySQL
- Deeply understand the characteristics of database transaction isolation
- Qtcreator sets multiple qmake
猜你喜欢
随机推荐
测试开发基础,教你做一个完整功能的Web平台之环境准备
MIF file format record
互联网协议
verilog设计抢答器【附源码】
[untitled]
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
简单介绍一下闭包及它的一些应用场景
[untitled]
Transaction rolled back because it has been marked as rollback only
VIM命令模式与输入模式切换
Compile QT project script with qmake
Still cannot find RPC dispatcher table failed to connect in virtual KD
Case study of Jinshan API translation function based on retrofit framework
常用sql语句整理:mysql
Eth trunk link switching delay is too high
PostgreSQL中的表复制
Debezium同步之Debezium架构详解
Unity script generates configurable files and loads
Mysql的json格式查询
Vuthink proper installation process