Qt - How to convert QString to char (NOT char*)

14,331

Solution 1

I'm pretty sure I found a way to convert the value without getting the error about QCharRef, I found if I use the QString::at() function to get the first index instead of QString("myText")[0], and then use toAscii() on that value, it seems to work and produce the correct value I want. In other words, this is what I did:

char c = QString("A").at(0).toAscii();

Solution 2

Use the index operator to get the first character of the string:

QString("myTextHere").toStdString()[0]

However, there is no need to convert to std::string, so the following is a better idea:

The main problem is that QString is in UTF-16 and contains QChars, two-byte characters. If your character can be represented using latin-1 charset, you should use

char ch = QString("myTextHere")[0].latin1();

Otherwise you need a bigger type (NOTE: even this cuts non-BMP characters in half):

int i = QString("myTextHere")[0].unicode();

Solution 3

If you claim that this returns a char*:

QString("myTextHere").toStdString.c_str();

Then obviously, this will get the first character, which would be char:

QString("myTextHere").toStdString.c_str()[0];

It may not look pretty, and probably there are better ways of getting the first character, but by definition, this code should (probably must) work.

Solution 4

Like others have said, use QString::operator[]. Using this on a QString will return a QCharRef, a helper class that is equivalent to a QChar. Since QChar is meant to support unicode characters there is only one further specification. From QChar in qt documentation (4.7):

QChar provides constructors and cast operators that make it easy to convert to and from traditional 8-bit chars. If you defined QT_NO_CAST_FROM_ASCII and QT_NO_CAST_TO_ASCII, as explained in the QString documentation, you will need to explicitly call fromAscii() or fromLatin1(), or use QLatin1Char, to construct a QChar from an 8-bit char, and you will need to call toAscii() or toLatin1() to get the 8-bit value back.

My Qt build will not allow

char c = QString("myTextHere")[0];

and instead I would use

char c = QString("myTextHere")[0].toAscii();

Solution 5

this worked for me in case you are getting error

error: reference to non-static member function must be called; did you mean to call it with no arguments?

QString mystr = "mystring";
char myvar = mystr.toStdString().c_str()[0];
Share:
14,331
thnkwthprtls
Author by

thnkwthprtls

C/C++ (specialty in Qt environment) Python Bash HTML/CSS Java JavaScript Ruby SQL

Updated on June 27, 2022

Comments

  • thnkwthprtls
    thnkwthprtls almost 2 years

    This seems like it should be a fairly simple thing to do, but I'm having a hard time figuring out how to do it. Basically I just have a QString object that will always be one character (specifically a letter) long, and I need to convert it to a char to set it to an object in my class of that type. I know how to make it char*, with QString("myTextHere").toStdString.c_str(), but the object that the value needs to be set to isn't char*, it's char. Is there a way to do this? Thanks!