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. how to convert QString to const char* (utf8)
Forum Updated to NodeBB v4.3 + New Features

how to convert QString to const char* (utf8)

Scheduled Pinned Locked Moved General and Desktop
22 Posts 4 Posters 17.1k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #6

    What is xxx ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    O 3 Replies Last reply
    0
    • SGaistS SGaist

      What is xxx ?

      O Offline
      O Offline
      opengpu2
      wrote on last edited by
      #7
      This post is deleted!
      1 Reply Last reply
      0
      • SGaistS SGaist

        What is xxx ?

        O Offline
        O Offline
        opengpu2
        wrote on last edited by
        #8

        @SGaist
        eg. the const char* got from QString

        QString myString("foo/bar");
        QByteArray inUtf8 = myString.toUtf8();
        const char *data = inUtf8.constData();

        1 Reply Last reply
        0
        • SGaistS SGaist

          What is xxx ?

          O Offline
          O Offline
          opengpu2
          wrote on last edited by
          #9

          @SGaist const char*

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #10

            Why would you need to call fromUtf8 on the const char * you just got from that QString ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            O 2 Replies Last reply
            0
            • SGaistS SGaist

              Why would you need to call fromUtf8 on the const char * you just got from that QString ?

              O Offline
              O Offline
              opengpu2
              wrote on last edited by
              #11

              @SGaist i need to convert with each other...thank you

              O 1 Reply Last reply
              0
              • SGaistS SGaist

                Why would you need to call fromUtf8 on the const char * you just got from that QString ?

                O Offline
                O Offline
                opengpu2
                wrote on last edited by
                #12

                @SGaist sometimes i need to convert the const char* (utf8) to QString

                1 Reply Last reply
                0
                • O opengpu2

                  @SGaist i need to convert with each other...thank you

                  O Offline
                  O Offline
                  opengpu2
                  wrote on last edited by
                  #13
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • joaopagottoJ Offline
                    joaopagottoJ Offline
                    joaopagotto
                    wrote on last edited by
                    #14

                    //----------------------------
                    // QString to *char

                    QString aux;
                    aux.append("aa");
                    aux.append("bb");

                    char *texto;
                    texto = aux.toStdString().c_str();

                    //----------------------------
                    // *char to QString

                    char text[64];
                    strcpy(text, "text123");

                    QString aux;
                    aux.append(text);

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      @joaopagotto where is the utf-8 handling ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      O 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        @joaopagotto where is the utf-8 handling ?

                        O Offline
                        O Offline
                        opengpu2
                        wrote on last edited by opengpu2
                        #16

                        @SGaist QString::fromUtf8((const char*)xxx); is this correct ? thank you..

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #17

                          Again, what is xxx ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          O 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            Again, what is xxx ?

                            O Offline
                            O Offline
                            opengpu2
                            wrote on last edited by
                            #18

                            @SGaist it's const char* in utf8...eg. the const char* that converted from QString above...
                            thank you

                            1 Reply Last reply
                            0
                            • SGaistS SGaist

                              Hi,

                              It should rather be:

                              QString myString("foo/bar");
                              QByteArray inUtf8 = myString.toUtf8();
                              const char *data = inUtf8.constData();
                              

                              Because

                              const char *data = myString.toUtf8().constData();
                              on the next line the QByteArray returned by toUtf8 has been destroyed
                              
                              O Offline
                              O Offline
                              opengpu2
                              wrote on last edited by
                              #19

                              @SGaist
                              Hi,

                              It should rather be:

                              QString myString("foo/bar");
                              QByteArray inUtf8 = myString.toUtf8();
                              const char *data = inUtf8.constData();

                              Because

                              const char *data = myString.toUtf8().constData();
                              on the next line the QByteArray returned by toUtf8 has been destroyed

                              //////////////////////////////////////
                              if as a function param, is this right?
                              void function(myString.toUtf8().constData()) {}

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #20

                                void function(myString.toUtf8().constData()) {} ?

                                It's not a valid function signature

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                O 1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  void function(myString.toUtf8().constData()) {} ?

                                  It's not a valid function signature

                                  O Offline
                                  O Offline
                                  opengpu2
                                  wrote on last edited by
                                  #21

                                  @SGaist void func(const char* xxx);
                                  func(myString.toUtf8().constData());

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #22

                                    It will work, but it's not the best thing to do from a readability point of view.

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    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