注: 该博文为扩展型;
1) QString转换为LPCTSTR
QString szStr;
LPCTSTR str = (LPWSTR)(szStr.utf16());
2) 中文乱码解决;
QTextCodec *pCodec = QTextCode::codecForName("gb2312");
if(!pCodec) return "";
std::string szStr;
QString str = pCodec->toUnicode(szStr.c_str(), szStr.length());
3) 国际化, 检测系统语言;
QLocale locate;
if( locate.language() == QLocale::English)
language = "English";
else if( locate.language() == QLocale::Chinese)
language = "Chinese";
4)ini配置读取乱码;
QSettings setInitCodec("UTF-8")
5)pro CONFIG配置
在pro文件中分别对debug, release进行处理, 这里有一个问题;
CONFIG(debug,debug | release){
...
}else{
...
}
注意: CONFIG()后面要紧跟{,如果{在下一行,将会报错,提示“Unexpected 'else‘”
6) Qt 获取屏幕分辨率大小;
QRect deskRect = QApplication::desktop()->availableGeometry();
7) keys使用;
keys可以绑定快捷键;
Item{
focus:true; //必须有;
keys.onEscaped:{
console.log("esc pressed");
}
keys.onPressed:{
....
}
}
8) 遍历文件夹;
#include <QDir>
#include <QFileInfo>
...
QDir dir(szFilePath);
foreach(QFileInfo mfi ,dir.entryInfoList())
{
if(mfi.isFile())
{
...
}
}
....