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

how to create special characters and insert into strings

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 6 Posters 3.1k Views
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on 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
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on 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

      mzimmersM 1 Reply Last reply
      1
      • hskoglundH hskoglund

        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

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on 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
        • hskoglundH Offline
          hskoglundH Offline
          hskoglund
          wrote on 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
          • mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on 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.

            JKSHJ KroMignonK 2 Replies Last reply
            0
            • mzimmersM mzimmers

              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.

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on 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
              • qtprogrammer123Q Offline
                qtprogrammer123Q Offline
                qtprogrammer123
                wrote on 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
                • mzimmersM mzimmers

                  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.

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on 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
                  • mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on 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 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