C++ QString 到 char* 的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2523765/
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
QString to char* conversion
提问by mawia
I was trying to convert a QString to char* type by the following methods, but they don't seem to work.
我试图通过以下方法将 QString 转换为 char* 类型,但它们似乎不起作用。
//QLineEdit *line=new QLineEdit();{just to describe what is line here}
QString temp=line->text();
char *str=(char *)malloc(10);
QByteArray ba=temp.toLatin1();
strcpy(str,ba.data());
Can you elaborate the possible flaw with this method, or give an alternative method?
您能否详细说明这种方法可能存在的缺陷,或者提供替代方法?
回答by Eli Bendersky
Well, the Qt FAQsays:
好吧,Qt 常见问题解答说:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QString str1 = "Test";
QByteArray ba = str1.toLocal8Bit();
const char *c_str2 = ba.data();
printf("str2: %s", c_str2);
return app.exec();
}
So perhaps you're having other problems. How exactly doesn't this work?
所以也许你有其他问题。这到底怎么行不通?
回答by davidnr
Maybe
也许
my_qstring.toStdString().c_str();
my_qstring.toStdString().c_str();
or safer, as Federico points out:
或者更安全,正如 Federico 指出的那样:
std::string str = my_qstring.toStdString();
const char* p = str.c_str();
It's far from optimal, but will do the work.
它远非最佳,但可以完成工作。
回答by Robert
The easiest way to convert a QStringto char*is qPrintable(const QString& str),
which is a macro expanding to str.toLocal8Bit().constData()
.
将QString转换为char*的最简单方法是qPrintable(const QString& str),它是一个扩展为str.toLocal8Bit().constData()
.
回答by Alex
David's answer works fine if you're only using it for outputting to a file or displaying on the screen, but if a function or library requires a char* for parsing, then this method works best:
如果您仅将 David 的答案用于输出到文件或在屏幕上显示,则它可以正常工作,但如果函数或库需要 char* 进行解析,则此方法效果最佳:
// copy QString to char*
QString filename = "C:\dev\file.xml";
char* cstr;
string fname = filename.toStdString();
cstr = new char [fname.size()+1];
strcpy( cstr, fname.c_str() );
// function that requires a char* parameter
parseXML(cstr);
回答by Shanks
EDITED
已编辑
this way also works
这种方式也有效
QString str ("Something");
char* ch = str.toStdString().C_str();
回答by gregseth
Your string may contain non Latin1 characters, which leads to undefined data. It depends of what you mean by "it deosn't seem to work".
您的字符串可能包含非 Latin1 字符,这会导致数据未定义。这取决于您所说的“它似乎不起作用”是什么意思。
回答by AlexDarkVoid
If your string contains non-ASCII characters - it's better to do it this way:
s.toUtf8().data()
(or s->toUtf8().data()
)
如果您的字符串包含非 ASCII 字符 - 最好这样做:(
s.toUtf8().data()
或s->toUtf8().data()
)
回答by sam
the Correct Solution Would be like this
正确的解决方案是这样的
QString k;
k = "CRAZYYYQT";
char ab[16];
sprintf(ab,"%s",(const char *)((QByteArray)(k.toLatin1()).data()) );
sprintf(ab,"%s",(const char *)((QByteArray)(k.toStdString()).data()));
sprintf(ab,"%s",(const char *)k.toStdString().c_str() );
qDebug()<<"--->"<<ab<<"<---";