1

how can I convert my QString composed of just one unicode to QChar and char?

I have this character cedilla. http://graphemica.com/%C3%A7

I don't have any idea yet about QChar but I tried using this code for converting QString to char.

char c[2];
strcpy(c, keyItem.contentText.toUtf8().constData());
printf("Character is: %c\n", c[0]);

but it shows this character �

My 'keyItem' is a class and contentText returns a QString although there's only one character. When I try to return the usual letters I could read them right.

Update: I am trying to create a keyboard and I have to get the int value of the QString that's why I have to convert it to char first.

I will be using this to create the keyboard.

send(int(c[0]);
void send(int key){ 
    QWSServer::sendKeyEvent(key, key, 0, true, false);
}
  • What OS are you using? – Iuliu Nov 20 '14 at 00:55
  • Why would you do this? QString already is Unicode. – MrEricSir Nov 20 '14 at 00:58
  • Because AFAIK `printf` doesn't print ok UTF-8 characters. And AFAIK on Windows for example you have `wprintf` for printing unicode...that's why the OS matters because it is possible that these functions are different depending on the OS. – Iuliu Nov 20 '14 at 01:04
  • I am actually using a POS device and I am using a "DEBUG" function and it is sort of like a printf. I am programming it here in linux desktop but the way it returns in my desktop is like that. I am using this function like printf `DEBUG("Character is: %c\n", c[0]);` – user3300650 Nov 20 '14 at 01:11

1 Answers1

4

Thanks anyway. I found now the solution.

QChar c;
c = keyItem.contentText[0];
send(c.unicode());

:)