C++ 如何将 QString 转换为 std::string?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4214369/
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 14:51:57  来源:igfitidea点击:

How to convert QString to std::string?

c++qttype-conversionqstring

提问by augustin

I am trying to do something like this:

我正在尝试做这样的事情:

QString string;
// do things...
std::cout << string << std::endl;

but the code doesn't compile. How to output the content of qstring into the console (e.g. for debugging purposes or other reasons)? How to convert QStringto std::string?

但代码无法编译。如何将 qstring 的内容输出到控制台(例如出于调试目的或其他原因)?如何转换QStringstd::string

采纳答案by Artyom

One of the things you should remember when converting QStringto std::stringis the fact that QStringis UTF-16 encoded while std::string... May have any encodings.

其中一个转换时,你应该记住的事情QStringstd::string的是,事实QString是UTF-16编码,同时std::string可以得到...任何编码。

So the best would be either:

所以最好的是:

QString qs;

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

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

The suggested (accepted) method may work if you specify codec.

如果您指定编解码器,建议的(接受的)方法可能会起作用。

See: http://doc.qt.io/qt-5/qstring.html#toLatin1

见:http: //doc.qt.io/qt-5/qstring.html#toLatin1

回答by Pablo Santa Cruz

You can use:

您可以使用:

QString qs;
// do things
std::cout << qs.toStdString() << std::endl;

It internally uses QString::toUtf8() function to create std::string, so it's Unicode safe as well. Here'sreference documentation for QString.

它内部使用 QString::toUtf8() 函数来创建 std::string,因此它也是 Unicode 安全的。这里的参考文档QString

回答by liaK

If your ultimate aim is to get debugging messages to the console, you can use qDebug().

如果您的最终目标是将调试消息发送到控制台,则可以使用qDebug()

You can use like,

你可以使用像,

qDebug()<<string;which will print the contents to the console.

qDebug()<<string;这会将内容打印到控制台

This way is better than converting it into std::stringjust for the sake of debugging messages.

这种方式比std::string仅仅为了调试消息而将其转换为更好。

回答by chris

QString qstr;
std::string str = qstr.toStdString();

However, if you're using Qt:

但是,如果您使用的是 Qt:

QTextStream out(stdout);
out << qstr;

回答by Puppy

Best thing to do would be to overload operator<< yourself, so that QString can be passed as a type to any library expecting an output-able type.

最好的办法是自己重载 operator<<,这样 QString 就可以作为类型传递给任何需要可输出类型的库。

std::ostream& operator<<(std::ostream& str, const QString& string) {
    return str << string.toStdString();
}

回答by flokk

An alternative to the proposed:

提议的替代方案:

QString qs;
std::string current_locale_text = qs.toLocal8Bit().constData();

could be:

可能:

QString qs;
std::string current_locale_text = qPrintable(qs);

See qPrintable documentation, a macro delivering a const char * from QtGlobal.

请参阅qPrintable 文档,这是一个从 QtGlobal 提供 const char * 的宏。

回答by shaveenk

The simplest way would be QString::toStdString().

最简单的方法是QString::toStdString()

回答by Hafsa Elif ?z?iftci

You can use this;

你可以用这个;

QString data;
data.toStdString().c_str();

回答by JPM

 QString data;
   data.toStdString().c_str();

could even throw exception on VS2017 compiler in xstring

甚至可以在 xstring 中的 VS2017 编译器上抛出异常

 ~basic_string() _NOEXCEPT
        {   // destroy the string
        _Tidy_deallocate();
        }

the right way ( secure - no exception) is how is explained above from Artyom

正确的方法(安全 - 也不例外)是上面 Artyom 的解释

 QString qs;

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

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

回答by wudongliang

Try this:

尝试这个:

#include <QDebug>
QString string;
// do things...
qDebug() << "right" << string << std::endl;