I was trying to convert a QString to char* type by the following methods, but they don't seem to work.
//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?
QString s("some"); printf(reinterpret_cast<char *>(s.data()));
Well, the Qt FAQ says:
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?
Maybe
my_qstring.toStdString().c_str();
or safer, as Federico points out:
std::string str = my_qstring.toStdString();
const char* p = str.c_str();
It's far from optimal, but will do the work.
toStdString()
return a new std::string
object and then the pointer to internal data const char *
is obtained. However, the string object is immediately destroyed after this statement, so the result pointer probably does not have a valid address if you use it in a subsequent statement.
toStdString()
that's dangerous; it's the use of raw pointers. Or, more specifically, the use of raw pointers from objects whose scopes aren't well-understood.
f(qstr.toStdString().c_str())
is ok from the point of view of temporary lifetime (extended until the end of f
). However, the evaluation order of a.b
is only guaranteed to be a
before b
from C++17 onwards. I guess most implementations would already work that way in practice, but strictly speaking I believe it would be UB before C++17
The easiest way to convert a QString to char* is qPrintable(const QString& str), which is a macro expanding to str.toLocal8Bit().constData()
.
qPrintable
returns const char*
not char*
, str.toLocal8Bit().data()
returns char*
. 2) The pointer to const char*
becomes invalid as soon as you hit a semicolon in the statement where qPrintable
was used. So const char* c_ptr = s.toLocal8Bit().constData();
does not make any sense.
qPrintable
output guaranteed to be zero terminated?
qPrintable()
description: "The char pointer will be invalid after the statement in which qPrintable() is used."
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:
// 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);
EDITED
this way also works
QString str ("Something");
char* ch = str.toStdString().C_str();
std::string
→ QString
), not what's asked for.
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".
If your string contains non-ASCII characters - it's better to do it this way: s.toUtf8().data()
(or s->toUtf8().data()
)
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<<"<---";
Qt provides the simplest API
const char *qPrintable(const QString &str)
const char *qUtf8Printable(const QString &str)
If you want non-const data pointer use
str.toLocal8Bit().data()
str.toUtf8().data()
It is a viable way to use std::vector as an intermediate container:
QString dataSrc("FooBar");
QString databa = dataSrc.toUtf8();
std::vector<char> data(databa.begin(), databa.end());
char* pDataChar = data.data();
Success story sharing
const char*
andchar*
are not the same type.const char*
is what can really be obtained. The user is free to copy the data to a writable buffer.char*
, notchar const*
, and your answer simply ignores that fact without mention.toLocal8Bit()
?