Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unicode in the C++ code

Unicode in the C++ code

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 739 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.
  • S Offline
    S Offline
    stretchthebits
    wrote on last edited by
    #1

    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 do

    CompareTwoString(myString, “Giraffe”)
    

    Any tips? Am I asking this in the right forum?

    KroMignonK 1 Reply Last reply
    0
    • S stretchthebits

      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 do

      CompareTwoString(myString, “Giraffe”)
      

      Any tips? Am I asking this in the right forum?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

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

      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
      3
      • S Offline
        S Offline
        stretchthebits
        wrote on last edited by
        #3

        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
        Would

        label->setText(myarray);
        

        work?

        KroMignonK 1 Reply Last reply
        0
        • S stretchthebits

          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
          Would

          label->setText(myarray);
          

          work?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

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

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

          S 1 Reply Last reply
          0
          • KroMignonK KroMignon

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

            S Offline
            S Offline
            stretchthebits
            wrote on last edited by
            #5

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

            JKSHJ 1 Reply Last reply
            0
            • S stretchthebits

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

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @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 takes int: Your compiler implicitly converts your double to an int first before passing it into the function.

              In your example, when you pass a char[], the compiler implicitly calls the QString constructor that takes const char* before calling QLabel::setText(). See the documentation for QString::QString(const char *str): https://doc.qt.io/qt-5/qstring.html#QString-7

              QString does not have a constructor that takes ushort* or wchar_t* or TCHAR* so you cannot pass those directly to setText(). However, you can do an explicit conversion by calling QString::fromWCharArray(): https://doc.qt.io/qt-5/qstring.html#fromWCharArray

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

              S 1 Reply Last reply
              3
              • JKSHJ JKSH

                @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 takes int: Your compiler implicitly converts your double to an int first before passing it into the function.

                In your example, when you pass a char[], the compiler implicitly calls the QString constructor that takes const char* before calling QLabel::setText(). See the documentation for QString::QString(const char *str): https://doc.qt.io/qt-5/qstring.html#QString-7

                QString does not have a constructor that takes ushort* or wchar_t* or TCHAR* so you cannot pass those directly to setText(). However, you can do an explicit conversion by calling QString::fromWCharArray(): https://doc.qt.io/qt-5/qstring.html#fromWCharArray

                S Offline
                S Offline
                stretchthebits
                wrote on last edited by
                #7

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

                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