how to create special characters and insert into strings
-
wrote on 14 Jun 2021, 01:13 last edited by
I need to represent some temperature readings, and need the degree symbol. My search effort, surprisingly didn't turn up anything. Can someone please show me the light?
One suggestion I tried was using the B0 code, but when I assign "\uB0" I get an "Expected token ';'" error.
Thanks...
-
wrote on 14 Jun 2021, 02:21 last edited by
-
Hi, B0 code should work, but you need to go through some hoops for the compiler to be happy, try something like:
QString sDegree = u8"\u00B0"; QString sMessage = "Hello " + sDegree + " world"; ui->label->setText(sMessage);
should display:
wrote on 14 Jun 2021, 02:24 last edited by@hskoglund thanks for the answer; this appears to be a viable C++ solution. Any idea how I can apply this to QML?
-
wrote on 14 Jun 2021, 02:34 last edited by
-
wrote on 14 Jun 2021, 02:37 last edited by
Beautiful! Thank you for the suggestion.
BTW: the wrapping of the text in qsTr() is what did the trick...without it, I get the error I mentioned above.
-
Beautiful! Thank you for the suggestion.
BTW: the wrapping of the text in qsTr() is what did the trick...without it, I get the error I mentioned above.
@mzimmers said in how to create special characters and insert into strings:
BTW: the wrapping of the text in qsTr() is what did the trick...without it, I get the error I mentioned above.
Works fine for me without qsTr() on Qt 5.14.1
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") Label { text: "It's 25\u00B0 outside." anchors.centerIn: parent } }
-
wrote on 14 Jun 2021, 08:38 last edited by
You can just copy->paste from some char base
-
Beautiful! Thank you for the suggestion.
BTW: the wrapping of the text in qsTr() is what did the trick...without it, I get the error I mentioned above.
wrote on 14 Jun 2021, 09:16 last edited by@mzimmers said in how to create special characters and insert into strings:
BTW: the wrapping of the text in qsTr() is what did the trick...without it, I get the error I mentioned above.
It depends which text encoding you have used to store your source files.
If you are using "UTF-8", then all literal strings are UTF-8 encoded, then it also works without qsTr(). -
wrote on 14 Jun 2021, 14:45 last edited by
Interesting...it wasn't working without the qsTr() on my laptop (Windows 10 running WSL) but it does work on my work desktop (essentially the same configuration). I think I'll leave it in, JIC.
-
wrote on 7 May 2025, 11:38 last edited by
Easy way - QChar has a constructor that takes unicode.
QChar sdegree(0x00B0);