QString與CString互轉

#include <QtCore/QCoreApplication>
#include <qDebug>
#include <afx.h>

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);

發佈了44 篇原創文章 · 獲贊 15 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章