久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將 QString 轉換為 char*

Converting QString to char*(將 QString 轉換為 char*)
本文介紹了將 QString 轉換為 char*的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

可能的重復:
QString 到 char 的轉換

我有一個函數(STL 中的 fopen),它提供一個 char* 參數作為我計算機中的路徑,但我必須在那個地方使用 QString,所以它不起作用.

I have a function (fopen in STL) that gives a char* argument as a path in my computer, but I must use QString in that place so it doesn't work.

如何將QString轉為char*來解決這個問題?

How can I convert QString to char* to solve this problem?

推薦答案

參見 這里是 How我可以將 QString 轉換為 char*,反之亦然嗎?

為了將 QString 轉換為char*,那么你首先需要得到一個字符串的 latin1 表示形式調用 toLatin1() 它將返回一個 QByteArray.然后調用data()在 QByteArray 上獲取指向的指針存儲在字節數組中的數據.看文檔:

In order to convert a QString to a char*, then you first need to get a latin1 representation of the string by calling toLatin1() on it which will return a QByteArray. Then call data() on the QByteArray to get a pointer to the data stored in the byte array. See the documentation:

https://doc.qt.io/qt-5/qstring.html#toLatin1https://doc.qt.io/qt-5/qbytearray.html#data

請看下面的例子演示:

int main(int argc, char **argv)
{
 QApplication app(argc, argv);
  QString str1 = "Test";
  QByteArray ba = str1.toLatin1();
  const char *c_str2 = ba.data();
  printf("str2: %s", c_str2);
  return app.exec();
}

注意,需要存儲在調用 data() 之前的 bytearray它,像下面這樣的調用

Note that it is necessary to store the bytearray before you call data() on it, a call like the following

const char *c_str2 = str2.toLatin1().data();

會使應用程序崩潰,因為QByteArray 沒有被存儲并且因此不再存在

will make the application crash as the QByteArray has not been stored and hence no longer exists

要將 char* 轉換為 QString可以使用 QString 構造函數接受 QLatin1String,例如:

To convert a char* to a QString you can use the QString constructor that takes a QLatin1String, e.g:

QString string = QString(QLatin1String(c_str2)) ;

查看文檔:

https://doc.qt.io/qt-5/qlatin1string.html

當然,我發現從這個以前的SO還有另一種方式答案:

QString qs;

// Either this if you use UTF-8 anywhere
std::string utf8_text = qs.toUtf8().constData();

// or this if you on Windows :-)
std::string current_locale_text = qs.toLocal8Bit().constData();

這篇關于將 QString 轉換為 char*的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 国产九九精品 | 久久精品日产第一区二区三区 | 国产精品日韩一区二区 | 欧美视频 亚洲视频 | 我我色综合 | 亚洲图片视频一区 | 精品免费国产视频 | 99精品视频免费在线观看 | 9porny九色视频自拍 | 在线观看精品 | 日韩欧美专区 | av在线播放不卡 | 天堂综合网 | 成人影院在线 | 欧美日韩国产精品一区 | 久久久99国产精品免费 | 久久久久国产一区二区三区 | 一区二区三区视频在线观看 | 中文字幕日韩一区二区 | 偷牌自拍 | 九九热国产精品视频 | 日韩不卡一区二区三区 | 久操国产| 99久久精品免费视频 | 中文字幕日韩欧美一区二区三区 | 成人在线网 | 亚洲欧美精品国产一级在线 | 一区二区三区亚洲 | 日韩午夜影院 | 99久久电影| 日韩影院在线观看 | a视频在线观看 | 97中文视频 | 综合久久99 | 久操国产 | 国产一区二区三区四区五区3d | 91在线精品视频 | 欧美激情一区二区 | 国产精品视频一区二区三 | 国产精品色综合 | 久草精品在线 |