C++ 将 QString 转换为 char*

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5505221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:18:52  来源:igfitidea点击:

Converting QString to char*

c++stringqtqt4

提问by amiref

Possible Duplicate:
QString to char conversion

可能的重复:
QString 到 char 的转换

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.

我有一个函数(STL 中的 fopen),它在我的计算机中提供一个 char* 参数作为路径,但我必须在那个地方使用 QString 所以它不起作用。

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

如何将 QString 转换为 char* 来解决此问题?

回答by

See here at How can I convert a QString to char* and vice versa?

请参阅此处如何将 QString 转换为 char*,反之亦然?

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

See the following example for a demonstration:

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

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

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

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

See the documentation:

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

为了将 QString 转换为 char*,您首先需要通过调用 toLatin1() 来获取字符串的 latin1 表示,这将返回 QByteArray。然后在 QByteArray 上调用 data() 以获取指向存储在字节数组中的数据的指针。请参阅文档:

https://doc.qt.io/qt-5/qstring.html#toLatin1 https://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() 之前需要存储字节数组,调用如下

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

将使应用程序崩溃,因为 QByteArray 尚未存储,因此不再存在

要将 char* 转换为 QString,您可以使用采用 QLatin1String 的 QString 构造函数,例如:

QString string = QString(QLatin1String(c_str2)) ;

请参阅文档:

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

Of course, I discovered there is another way from this previous SO answer:

当然,我发现以前的 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();

回答by Martin York

You could use QFilerather than std::fstream.

您可以使用QFile而不是 std::fstream。

QFile           file(qString);

Alternatively convert the QString into a char* as follows:

或者将 QString 转换为 char*,如下所示:

std::ifstream   file(qString.toLatin1().data());

The QStringis in UTF-16 so it is converted toLatin1() here but QString has a couple of different conversions including toUtf8() (check your file-system it may use UTF-8).

QString的是UTF-16,因此被转换toLatin1()在这里,但QString的有几个()不同的转换,包括toUtf8(检查你的文件系统,它可以使用UTF-8)。

As noted by @0A0D above: don'tstore the char* in a variable without also getting a local copy of the QByteArray.

正如上面@0A0D 所指出的:不要在没有获得 QByteArray 的本地副本的情况下将 char* 存储在变量中。

char const*      fileName = qString.toLatin1().data();
std::ifstream    file(fileName);  // fileName not valid here.

This is because toLatin1() returns an object of QByteArray. As it is not actually bound to a variable it is a temporary that is destroyed at the end of the expression. Thus the call to data() here returns a pointer to an internal structure that no longer exists after the ';'.

这是因为 toLatin1() 返回 QByteArray 的对象。由于它实际上并未绑定到变量,因此它是在表达式结束时销毁的临时变量。因此,这里对 data() 的调用返回一个指向内部结构的指针,该结构在“;”之后不再存在。