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

將 QString 轉(zhuǎn)換為 char*

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

問題描述

可能的重復(fù):
QString 到 char 的轉(zhuǎn)換

我有一個函數(shù)(STL 中的 fopen),它提供一個 char* 參數(shù)作為我計算機中的路徑,但我必須在那個地方使用 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轉(zhuǎn)為char*來解決這個問題?

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

推薦答案

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

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

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

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

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

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

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

要將 char* 轉(zhuǎn)換為 QString可以使用 QString 構(gòu)造函數(shù)接受 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

當(dāng)然,我發(fā)現(xiàn)從這個以前的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();

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

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 欧美男人的天堂 | 欧美精品一区二区免费 | 精品国产青草久久久久96 | 久久一| 91日韩| 亚洲成av人片在线观看 | 久久久久久一区 | 高清免费av | 午夜视频在线免费观看 | 久久精品网 | 精品欧美一区二区三区久久久 | 欧美九九| 高清人人天天夜夜曰狠狠狠狠 | 犬夜叉在线观看 | 久久久久久亚洲 | 国产91精品久久久久久久网曝门 | 国产福利久久 | 国产伦精品 | 一级毛片免费完整视频 | 亚洲成人日韩 | 日韩欧美中文在线 | 国产综合在线视频 | 91久久精品一区二区三区 | 国产成人精品免费视频大全最热 | 综合网伊人 | 成人精品视频在线观看 | 色综合色综合色综合 | 亚洲一区二区三区视频在线 | 亚洲国产一区二区视频 | 精品福利一区二区三区 | 国产精品久久久亚洲 | 日本免费黄色一级片 | 亚洲精品久久久久久一区二区 | 一区二区三区欧美在线 | 亚洲综合视频一区 | 国产高清在线精品 | av在线播放网 | 欧美亚洲国语精品一区二区 | 亚洲精品www | 国产精品视频在线免费观看 | 日本久久网 |