QString to char* conversion

C++QtQstringQtcore

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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?

Solution 2 - C++

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.

Solution 3 - C++

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

Solution 4 - C++

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

Solution 5 - C++

EDITED

this way also works

QString str ("Something");

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

Solution 6 - C++

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".

Solution 7 - C++

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

Solution 8 - C++

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

Solution 9 - C++

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

Solution 10 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmawiaView Question on Stackoverflow
Solution 1 - C++Eli BenderskyView Answer on Stackoverflow
Solution 2 - C++davidnrView Answer on Stackoverflow
Solution 3 - C++RobertView Answer on Stackoverflow
Solution 4 - C++AlexView Answer on Stackoverflow
Solution 5 - C++Jorge LuisView Answer on Stackoverflow
Solution 6 - C++gregsethView Answer on Stackoverflow
Solution 7 - C++AlexDarkVoidView Answer on Stackoverflow
Solution 8 - C++samView Answer on Stackoverflow
Solution 9 - C++user2042397View Answer on Stackoverflow
Solution 10 - C++TCHView Answer on Stackoverflow