Unicode in the C++ code
-
Allo,
I am on Kubuntu 20.10.
I have Qt Creator 4.14.2, it says based on Qt 5.15.2 (GCC 7.3.1 20180303 (Red Hat 7.3.1-5), 64 bit)With VC++ + MFC, I had code like this
TCHAR mySentence[100]; mySentence[0] = L'ɐ'; mySentence[1] = L'q'; mySentence[2] = L'ɔ'; mySentence[2] = L'p';
I find unicode confusing. There is wchar_t, there is TCHAR.
Qt has QChar and QString.
What am I suppose to use?With 8 bit ASCII, it was clear.
I could doCompareTwoString(myString, “Giraffe”)
Any tips? Am I asking this in the right forum?
-
Allo,
I am on Kubuntu 20.10.
I have Qt Creator 4.14.2, it says based on Qt 5.15.2 (GCC 7.3.1 20180303 (Red Hat 7.3.1-5), 64 bit)With VC++ + MFC, I had code like this
TCHAR mySentence[100]; mySentence[0] = L'ɐ'; mySentence[1] = L'q'; mySentence[2] = L'ɔ'; mySentence[2] = L'p';
I find unicode confusing. There is wchar_t, there is TCHAR.
Qt has QChar and QString.
What am I suppose to use?With 8 bit ASCII, it was clear.
I could doCompareTwoString(myString, “Giraffe”)
Any tips? Am I asking this in the right forum?
@stretchthebits said in Unicode in the C++ code:
have Qt Creator 4.14.2, it says based on Qt 5.15.2 (GCC 7.3.1 20180303 (Red Hat 7.3.1-5), 64 bit)
First: Qt Creator is an IDE, you are only seeing the Qt Version using to build Qt Creator, it has no link with Qt Version/Kit you will use to build a Qt application.
Second:
QString
always holds internally strings in UTF-16 encoding (cf. https://doc.qt.io/qt-5/qstring.html#details).
QString offers many functions to handle string, so what is exactly your needs? -
16 bit? Ok, that sounds good to me.
I tend to use plain old array of char.
I don’t use STL.For example, if I want the text from QString, I do
char myarray[100]; QString qstring; strcpy(myarray, qstring.toLocal8Bit());
I guess I could do
ushort myarray[100]; ///Assuming ushort is 16 bit QString qstring; somecopyfunction(myarray, qstring.utf16());
So, let’s say I take that ushort myarray[100];
I do some modifications and now, I want to display it to a QLabel
Wouldlabel->setText(myarray);
work?
-
16 bit? Ok, that sounds good to me.
I tend to use plain old array of char.
I don’t use STL.For example, if I want the text from QString, I do
char myarray[100]; QString qstring; strcpy(myarray, qstring.toLocal8Bit());
I guess I could do
ushort myarray[100]; ///Assuming ushort is 16 bit QString qstring; somecopyfunction(myarray, qstring.utf16());
So, let’s say I take that ushort myarray[100];
I do some modifications and now, I want to display it to a QLabel
Wouldlabel->setText(myarray);
work?
@stretchthebits said in Unicode in the C++ code:
So, let’s say I take that ushort myarray[100];
I do some modifications and now, I want to display it to a QLabel
Would
label->setText(myarray);work?
Have you take time to read documentation?
QLabel::setText() uses QString, so why do you want to use an ushort array? -
@stretchthebits said in Unicode in the C++ code:
So, let’s say I take that ushort myarray[100];
I do some modifications and now, I want to display it to a QLabel
Would
label->setText(myarray);work?
Have you take time to read documentation?
QLabel::setText() uses QString, so why do you want to use an ushort array?@KroMignon said in Unicode in the C++ code:
So, let’s say I take that ushort myarray[100];
I do some modifications and now, I want to display it to a QLabel
Would
label->setText(myarray);
work?Have you take time to read documentation?
QLabel::setText() uses QString, so why do you want to use an ushort array?I don’t use Qt classes everywhere in my code. The Qt classes are only used in the GUI class so, elsewhere, I use char, ushort, float.
“Have you take time to read documentation?
QLabel::setText() uses QString”==That’s the thing I don’t understand.
If I send it an array of char, it compiles and it works. -
@KroMignon said in Unicode in the C++ code:
So, let’s say I take that ushort myarray[100];
I do some modifications and now, I want to display it to a QLabel
Would
label->setText(myarray);
work?Have you take time to read documentation?
QLabel::setText() uses QString, so why do you want to use an ushort array?I don’t use Qt classes everywhere in my code. The Qt classes are only used in the GUI class so, elsewhere, I use char, ushort, float.
“Have you take time to read documentation?
QLabel::setText() uses QString”==That’s the thing I don’t understand.
If I send it an array of char, it compiles and it works.@stretchthebits said in Unicode in the C++ code:
==That’s the thing I don’t understand.
If I send it an array of char, it compiles and it works.It's similar to the mechanism that allows you to pass a
double
into a function that takesint
: Your compiler implicitly converts yourdouble
to anint
first before passing it into the function.In your example, when you pass a
char[]
, the compiler implicitly calls the QString constructor that takesconst char*
before callingQLabel::setText()
. See the documentation forQString::QString(const char *str)
: https://doc.qt.io/qt-5/qstring.html#QString-7QString does not have a constructor that takes
ushort*
orwchar_t*
orTCHAR*
so you cannot pass those directly to setText(). However, you can do an explicit conversion by callingQString::fromWCharArray()
: https://doc.qt.io/qt-5/qstring.html#fromWCharArray -
@stretchthebits said in Unicode in the C++ code:
==That’s the thing I don’t understand.
If I send it an array of char, it compiles and it works.It's similar to the mechanism that allows you to pass a
double
into a function that takesint
: Your compiler implicitly converts yourdouble
to anint
first before passing it into the function.In your example, when you pass a
char[]
, the compiler implicitly calls the QString constructor that takesconst char*
before callingQLabel::setText()
. See the documentation forQString::QString(const char *str)
: https://doc.qt.io/qt-5/qstring.html#QString-7QString does not have a constructor that takes
ushort*
orwchar_t*
orTCHAR*
so you cannot pass those directly to setText(). However, you can do an explicit conversion by callingQString::fromWCharArray()
: https://doc.qt.io/qt-5/qstring.html#fromWCharArray@JKSH said in Unicode in the C++ code:
Thanks. So I settled on using wchar_t and also
QString::fromWCharArray()
and that is able to post text properly:
editbox->setText(stuff.........);