ChatGPT解决这个技术问题 Extra ChatGPT

QString to char* conversion

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?

Your example works fine for me, where is the problem?
Sorry for my English but why it isn't right to use such approach? QString s("some"); printf(reinterpret_cast<char *>(s.data()));

b
bur

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?


const char* and char* are not the same type.
@LightnessRacesinOrbit: writing to QString's contents without it knowing is a horrible idea, hence of course const char* is what can really be obtained. The user is free to copy the data to a writable buffer.
I completely agree. However, the question asked about char*, not char const*, and your answer simply ignores that fact without mention.
@LightnessRacesinOrbit: sometimes the best answer is to unask the question. In other words, to point out that it's not asking the right thing. This answer was accepted by the question poster, so I suppose it hit its target
seems like the FAQ has been updated to use toLocal8Bit()?
d
davidnr

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.


This will mess up Unicode characters. A Unicode-friendly solution: stackoverflow.com/a/4644922/238387
This method is very dangerous and should not be used: 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.
@RicoRico It's not the method 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.
Specifically C++ temporaries normally live until the end of the statement that creates them. So the first form in the answer is ok if it's used in-line in a function call (assuming the function doesn't store the pointer for future use) but it's not ok if it's assigned to a variable.
As @plugwash says, 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
K
KernelPanic

The easiest way to convert a QString to char* is qPrintable(const QString& str), which is a macro expanding to str.toLocal8Bit().constData().


Why isn't this a more popular answer? I just learned about this by accident while poking around the Qt source, and that's exactly what they do.
@Phlucious, because: 1) 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.
@Phlucious thanks you are life saver :) these all top voted answers are wrong , Question is about char and they are returning const char*
Is qPrintable output guaranteed to be zero terminated?
@WindyFields - As warned in the qPrintable() description: "The char pointer will be invalid after the statement in which qPrintable() is used."
A
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:

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

J
Jorge Luis

EDITED

this way also works

QString str ("Something");

char* ch = str.toStdString().C_str();

That looks like a different conversion (std::stringQString), not what's asked for.
g
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".


A
AlexDarkVoid

If your string contains non-ASCII characters - it's better to do it this way: s.toUtf8().data() (or s->toUtf8().data())


s
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<<"<---";

Forget to use C-style casting.
J
JBES

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

T
TCH

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