在Qt之QTextCodec乱谈一文中我们提到这个一样例子
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextCodec * codec = new DbzhangCodec;
QTextCodec::setCodecForCStrings(codec);
qDebug()<<QString("hello world!");
return 0;
}
其中,DbzhangCodec是QTextCodec的一个派生类。这儿使用了 new ,但是却没有相应地使用 delete,可是却没有造成内存泄漏,原因何在?
构造与析构函数
从构造函数开始看起:当对象被创建时,它会将指向自己的指针添加到一个static的列表中。
/*!
Constructs a QTextCodec, and gives it the highest precedence. The
QTextCodec should always be constructed on the heap (i.e. with \c
new). Qt takes ownership and will delete it when the application
terminates.
*/
QTextCodec::QTextCodec()
{
...
all->prepend(this);
}
其中:all是一个static的列表
static QList<QTextCodec*> *all = 0;
看一下析构函数
/*!
Destroys the QTextCodec. Note that you should not delete codecs
yourself: once created they become Qt's responsibility.
*/
QTextCodec::~QTextCodec()
{
if (all) {
all->removeAll(this);
...
}
}
上面的两段注释明确告诉我们:对象必须分配在heap中,而且由Qt负责在程序退出之前删除它。
可是,删除操作是怎么实现的呢?
QTextCodecCleanup
class QTextCodecCleanup
{
public:
~QTextCodecCleanup();
};
/*
Deletes all the created codecs. This destructor is called just
before exiting to delete any QTextCodec objects that may be lying
around.
*/
QTextCodecCleanup::~QTextCodecCleanup()
{
...
for (QList<QTextCodec *>::const_iterator it = all->constBegin()
; it != all->constEnd(); ++it) {
delete *it;
}
delete all;
all = 0;
...
}
这是一个很简单的类,只定义了一个析构函数:负责删除列表 all 中的所有QTextCodec对象。
要使得这个析构函数被调用,显然需要构造一个QTextCodecCleanup对象,这是通过:
Q_GLOBAL_STATIC(QTextCodecCleanup, createQTextCodecCleanup)
实现的
Q_GLOBAL_STATIC
这个宏定义在 qglobal.h 这个头文件内(根据你所用的Qt的版本不同,你的源码初看起来与此可能有很大不同)。
#define Q_GLOBAL_STATIC(TYPE, NAME) \
static TYPE *NAME() \
{ \
static QGlobalStatic<TYPE > thisGlobalStatic \
= { Q_BASIC_ATOMIC_INITIALIZER(0), false }; \
if (!thisGlobalStatic.pointer && !thisGlobalStatic.destroyed) { \
TYPE *x = new TYPE; \
if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x)) \
delete x; \
else \
static QGlobalStaticDeleter<TYPE > cleanup(thisGlobalStatic); \
} \
return thisGlobalStatic.pointer; \
}
由于考虑了线程安全性,这个宏看起来还真乱,其实呢,也就是一个静态的函数
static QTextCodecCleanup * createQTextCodecCleanup()
{
}
该函数内,创建了两个静态的对象
static QGlobalStatic<QTextCodecCleanup> thisGlobalStatic;
static QGlobalStaticDeleter<QTextCodecCleanup> cleanup(thisGlobalStatic);
thisGlobalStatic 中保存有 QTextCodecCleanup 对象的指针(并作为函数的返回值返回);cleanup 析构时将析构掉该指针所指向的对象。
分享到:
相关推荐
这样虽然解决了上面主窗口一闪而过的问题,但是,如果在my1 对话框出现的时 候不点enterBtn,而是直接关闭对话框,那么此时整个程序应该结束执行,但 是事实是这样的吗?如果你此时对程序进行了改动,再次按下run ...
QTextCodec::setCodecForCStrings QTextCodec::codecForName "UTF 8" ; QTextCodec::setCodecForLocale QTextCodec::codecForName "UTF 8" ; QTextCodec::setCodecForTr QTextCodec::...
为了防止直接读取文件里的内容太大而发生卡顿,于是多线程读取将更高效的解决这个问题。 效果图如下: 其中pro文件无需改动,默认就好,头文件h里面的内容为 #ifndef MAINWINDOW_H #define MAINWINDOW_H #...
使用qt软件完成程序设计#include "widget.h" ... QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());//添加中文支持2-2 Widget w; w.show(); client w1; w1.show(); return a.exec(); }
QTextCodec::setCodecForTr(QTextCodec::codecForLocale( )); 显示中文前加这句 中文加tr
syszuxpinyin是一款开源的嵌入式qt输入法,原版一直存在一个乱码的问题,现在qt一般设置为UTF-8编码,可是该输入法只支持GBK编码,所以对其进行稍微修改了下,支持UTF-8编码,亲测不再乱码,代码中需要指定编码方式...
如果是在X86平台上时候,使用QTextCodec::setCodecForTr(QTextCodec::codecForLocale());来设置显示中文
#include <QTextCodec> #include #include #include #include #include #include #include #include #include #ifndef QT_NO_PRINTER #include #include #include #endif #include "textedit.h" #...
QSettings* settings=new QSettings("./setting.ini",QSettings::IniFormat); if(!QFile("./setting.ini").exists()) { settings->setIniCodec(QTextCodec::codecForName("utf-8")); settings->setValue("host...
QTextCodec 字符编码与16进制数据转换 QTextCodec *codec = QTextCodec::codecForName("KOI8-R"); QString string = codec->toUnicode(encodedString); QByteArray encodedString = codec->fromUnicode(string);
在QT中可以直接QTextCodec来转换字符串的编码,这为在QT下开发中文软件带来了便利条件,不过这种方法不符合国际化/本地化的标准,最直接的方法是把整个应用程序的编码设置为GBK编码
为初学mqtt或qt的小伙伴提供一个参考方向,本人也是学习者,有写的不好的地方批评指正。 mqtt的原理和应用程序开发过程通过本程序就能体现,看懂mqtt的主题订阅发布之后,再去看官方的编程手册开发上手就会很简单啦...
作者:曹忠明, 华清远见嵌入式学院讲师。 中文国际化是在QT程序设计中很重要的一个环节,... #include <QTextCodec> #include #include int main(int argc, char **argv) { QApplication app(a
本文实例为大家分享了Python PyQt4实现QQ抽屉效果展示的具体代码,供大家参考,具体内容如下 ...QTextCodec.setCodecForTr(QTextCodec.codecForName(utf8)) class MyQQ(QTabWidget): def __init__(self,parent=None)
2、ICU(International Components for Unicode)是一个国际化的字符编码和转化的库。 3、Qt的QTextCodec的toUnicode方法也采用了这个库来封装 3、压缩包里有icu4c-49_1_2-src.zip源码包以及用vs2010编译好的dll lib
#include <QTextCodec> #include #include #include #include #include enum SMTPCMD{SMTP_EHLO = 0 , SMTP_AUTHLOGIN , SMTP_MAIL , SMTP_RCPT , SMTP_DATA , SMTP_QUIT}; struct ...
目前在使用国内的数据库和实时库接口。 说句实话,国内的东西与国外的东西比,在用户体验和接口调用上比还是存在很大的距离。 个人喜欢用QString去存储数据。...QTextCode::setCodecForLocale(QTextCodec::codecForNam
必需:QT WebKit和QTextCodec :: setCodecForTr()和QTextCodec :: setCodecForCStrings() SP Flash Tool-用于为基于MTK的智能手机刷新固件的程序。 快捷键:SP_Flash_Tool_src SP_Flash_Tool源代码
qtcsv:用于在Qt中读取和写入csv文件的库
KCodecs 字符串编码库 介绍 KCodecs提供了使用各种编码来操作字符... 它可以自动确定字符串的字符集,转换XML实体,验证电子邮件地址,并以比QTextCodec更宽容的方式按名称查找编码(例如,用于来自Internet的数据)。