Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. how to create special characters and insert into strings
Forum Updated to NodeBB v4.3 + New Features

how to create special characters and insert into strings

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 6 Posters 3.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mzimmers
    wrote on 14 Jun 2021, 01:13 last edited by
    #1

    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...

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hskoglund
      wrote on 14 Jun 2021, 02:21 last edited by
      #2

      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:
      Screenshot 2021-06-14 at 04.18.24.png

      M 1 Reply Last reply 14 Jun 2021, 02:24
      1
      • H hskoglund
        14 Jun 2021, 02:21

        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:
        Screenshot 2021-06-14 at 04.18.24.png

        M Offline
        M Offline
        mzimmers
        wrote on 14 Jun 2021, 02:24 last edited by
        #3

        @hskoglund thanks for the answer; this appears to be a viable C++ solution. Any idea how I can apply this to QML?

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hskoglund
          wrote on 14 Jun 2021, 02:34 last edited by
          #4

          Hi, try:

          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: qsTr("It's 25\u00B0 outside.")
                  anchors.centerIn: parent
              }
          }
          

          Screenshot 2021-06-14 at 04.33.42.png

          1 Reply Last reply
          4
          • M Offline
            M Offline
            mzimmers
            wrote on 14 Jun 2021, 02:37 last edited by
            #5

            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.

            J K 2 Replies Last reply 14 Jun 2021, 04:55
            0
            • M mzimmers
              14 Jun 2021, 02:37

              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.

              J Offline
              J Offline
              JKSH
              Moderators
              wrote on 14 Jun 2021, 04:55 last edited by
              #6

              @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
                  }
              }
              

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              1
              • Q Offline
                Q Offline
                qtprogrammer123
                wrote on 14 Jun 2021, 08:38 last edited by
                #7

                You can just copy->paste from some char base

                Mam moc jak Harry Potter, w zębach mogę przenieść hotel.

                1 Reply Last reply
                0
                • M mzimmers
                  14 Jun 2021, 02:37

                  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.

                  K Offline
                  K Offline
                  KroMignon
                  wrote on 14 Jun 2021, 09:16 last edited by
                  #8

                  @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().

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  2
                  • M Offline
                    M Offline
                    mzimmers
                    wrote on 14 Jun 2021, 14:45 last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Paul_Friesen
                      wrote on 7 May 2025, 11:38 last edited by
                      #10

                      Easy way - QChar has a constructor that takes unicode.

                      QChar sdegree(0x00B0);
                      
                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved