QT 中文 和 QTableWidget::setItem 每次new是否内存泄漏

 

 

 1.Code

复制代码
 1 void SolveChineseImproper::setAndPrintTestCharacter()
 2 {
 3     //如果源文件是UTF-8+BOM的编码方式,还需要在头文件加入
 4 #if 0
 5 #if defined(_MSC_VER) && (_MSC_VER >= 1600)    
 6 # pragma execution_character_set("utf-8")    
 7 #endif
 8 #endif
 9 
10     qDebug() << QString("Version:0x%1").arg(QT_VERSION, 0, 16);
11 
12     //设置中文编码
13 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
14 #if _MSC_VER
15     QTextCodec* codec = QTextCodec::codecForName("gbk");
16 #else
17     QTextCodec* codec = QTextCodec::codecForName("utf-8");
18 #endif
19     QTextCodec::setCodecForLocale(codec);
20     //QTextCodec::setCodecForCStrings(codec);
21     //QTextCodec::setCodecForTr(codec);
22 #else
23     QTextCodec* codec = QTextCodec::codecForName("utf-8");
24     QTextCodec::setCodecForLocale(codec);
25 #endif
26 
27     QString str(QObject::tr("1中文"));
28     qDebug() << str;
29     qDebug() << QStringLiteral("2中文");
30     qDebug() << QString::fromLatin1("3中文");
31     qDebug() << QString::fromLocal8Bit("4中文");
32     qDebug() << QString::fromUtf8("5中文");
33     qDebug() << QString::fromWCharArray(L"6中文");
34 
35 }
View Code
复制代码
复制代码
 1 #include "QT_GetProcessMemoryInfo.h"
 2 
 3 #pragma comment(lib, "Psapi.lib")
 4 
 5 int col = 5;
 6 int row = 3;
 7 int count = 1000;
 8 QT_GetProcessMemoryInfo::QT_GetProcessMemoryInfo(QWidget *parent)
 9     : QMainWindow(parent)
10 {
11     ui.setupUi(this);  
12 
13     tableWidget = new QTableWidget(0);
14     tableWidget->setRowCount(row);
15     tableWidget->setColumnCount(col);
16  
17     tableWidget->show();
18 
19     QTimer* timer = new QTimer(this);
20     connect(timer, &QTimer::timeout, this, &QT_GetProcessMemoryInfo::update);
21     timer->start(1000);
22 
23     HANDLE handle = GetCurrentProcess();
24     PROCESS_MEMORY_COUNTERS pmc;
25     GetProcessMemoryInfo(handle, &pmc, sizeof(pmc));
26     initUse = pmc.WorkingSetSize;
27 }
28 
29 void QT_GetProcessMemoryInfo::update()
30 {
31   
32         count += 10001;
33         // auto p = new char[count];
34         
35         for (int i = 0; i < row; i++)
36         {
37             for (int j = 0; j < col; j++)
38             {
39                 QTableWidgetItem* cubesHeaderItem = new QTableWidgetItem(QString("%1").arg(count));
40                 (count % 2) ?
41                     [=]() { cubesHeaderItem->setIcon(QIcon(QPixmap("./Images/1.png"))); }()
42                     :
43                     [=]() { cubesHeaderItem->setIcon(QIcon(QPixmap("./Images/2.png"))); }();
44                
45                 cubesHeaderItem->setTextAlignment(Qt::AlignVCenter);
46                 tableWidget->setItem(i, j, cubesHeaderItem);
47             }
48         }
49         
50         HANDLE handle = GetCurrentProcess();
51         PROCESS_MEMORY_COUNTERS pmc;
52         GetProcessMemoryInfo(handle, &pmc, sizeof(pmc));
53         qDebug() << CH("INIT:")
54                  << initUse<<CH("  内存使用量:") 
55                  << pmc.WorkingSetSize <<"new:"
56                  << (pmc.WorkingSetSize - initUse);
57         initUse = pmc.WorkingSetSize;
58 }
View Code
复制代码
复制代码
 1 #pragma once
 2 
 3 #include <QtWidgets/QMainWindow>
 4 #include "ui_QT_GetProcessMemoryInfo.h"
 5 
 6 #include <QDebug>
 7 #include <windows.h>
 8 #include <Psapi.h>
 9 #include <QTimer> 
10 #include <QTableWidget>
11 #include <QTableWidgetItem>
12 
13 
14 #define CH(T) QStringLiteral(T)
15 
16 class QT_GetProcessMemoryInfo : public QMainWindow
17 {
18     Q_OBJECT
19 
20 public:
21    QT_GetProcessMemoryInfo(QWidget *parent = Q_NULLPTR);
22  
23  public slots:
24    void update();
25 
26 private:
27     Ui::QT_GetProcessMemoryInfoClass ui;
28     QTableWidget* tableWidget;
29     SIZE_T initUse;
30 };
View Code
复制代码

 

2. 结论会释放 ;//INIT:" 37961728 "  内存使用量:" 37961728 new: 0

 

posted @   三岁玩童  阅读(231)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示