QString与CString互转

#include 
#include 
#include 

inline QString  CS2QS(CString cs) { return  QString::fromWCharArray((LPCTSTR)cs, cs.GetLength()); }
inline CString  QS2CS(QString qs) { return  qs.toStdString().c_str(); }

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
	
	CString cs = "你好Hello World啊   !";
	QString qs = "好啊hehe.........";
	CString cs2 = QS2CS(qs);
	qDebug() << CS2QS(cs);
	qDebug() << CS2QS(cs2);

	return a.exec();
}


【输出】

"你好Hello World啊   !"
"好啊hehe........."


【另外一个CString到QString的转换方法(借助std::wstring)】

CString cs = "Hello测 试world";
std::wstring ss = (LPCTSTR)cs;
QString qsTest = QString::fromStdWString(ss);

你可能感兴趣的:(Qt)