how to create special characters and insert into strings
-
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...
-
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:
@hskoglund thanks for the answer; this appears to be a viable C++ solution. Any idea how I can apply this to QML?
-
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 } }
-
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.
@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(). -
Easy way - QChar has a constructor that takes unicode.
QChar sdegree(0x00B0);