久久久久久久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 ()?環形?)
主站蜘蛛池模板: 超碰免费在线观看 | 三级黄色在线观看 | 亚洲综合激情网 | 欧美精品在线播放 | 欧洲一区二区 | 黄色片视频 | 国产日韩欧美在线 | 成人午夜网站 | 国产激情一区二区三区 | 亚洲精品www久久久久久广东 | 五月婷婷 | 国产一区二区三区在线看 | 欧美在线性爱视频 | 色噜噜狠狠一区二区三区果冻 | 中文字幕日韩在线观看 | 欧美日韩在线视频观看 | 成人动漫免费观看 | 无遮挡一级毛片 | 久久久久免费 | 成人看片网 | 黄色大毛片 | 久久九九免费视频 | 一区二区在线视频 | 亚洲性猛交 | 欧美成人猛片aaaaaaa | 不卡在线视频 | 激情五月综合 | 久久五月婷| 日日爱影视 | 亚洲天天 | 夜夜操网站 | 一级二级片 | 精品久久久久久久久久久久久久久久 | 神马福利视频 | 懂色av一区二区三区 | 欧美国产日韩一区二区 | 欧美日本在线观看 | 亚洲欧美日韩在线 | 久久免费视频网站 | 精品视频久久 | 4438成人网 |