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 16.7k 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.
  • O Offline
    O Offline
    opengpu2
    wrote on 2 Jun 2015, 08:04 last edited by opengpu2 6 Mar 2015, 01:23
    #1

    how to convert QString to const char* (utf8)
    (const char** )xxx.toLocal8Bit();

    (const char* )xxx.toUtf8();
    which one is correct?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 2 Jun 2015, 08:05 last edited by mcosta 6 Feb 2015, 08:07
      #2

      Hi,

      if you need UTF-8 you should do xxx.toUtf8().constData()

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 2 Jun 2015, 09:31 last edited by
        #3

        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
        

        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 5 Jun 2015, 03:19
        1
        • M Offline
          M Offline
          mcosta
          wrote on 2 Jun 2015, 09:32 last edited by
          #4

          Yep, you're right.

          Sorry

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • O Offline
            O Offline
            opengpu2
            wrote on 2 Jun 2015, 10:33 last edited by opengpu2 6 Mar 2015, 01:23
            #5

            and i should use :
            QString::fromUtf8((const char**)xxx);
            to conver utf8 const char* to QString?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 2 Jun 2015, 22:48 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 3 Jun 2015, 01:24
              0
              • S SGaist
                2 Jun 2015, 22:48

                What is xxx ?

                O Offline
                O Offline
                opengpu2
                wrote on 3 Jun 2015, 01:24 last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • S SGaist
                  2 Jun 2015, 22:48

                  What is xxx ?

                  O Offline
                  O Offline
                  opengpu2
                  wrote on 3 Jun 2015, 01:25 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
                  • S SGaist
                    2 Jun 2015, 22:48

                    What is xxx ?

                    O Offline
                    O Offline
                    opengpu2
                    wrote on 3 Jun 2015, 11:28 last edited by
                    #9

                    @SGaist const char*

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 3 Jun 2015, 11:34 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 3 Jun 2015, 11:36
                      0
                      • S SGaist
                        3 Jun 2015, 11:34

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

                        O Offline
                        O Offline
                        opengpu2
                        wrote on 3 Jun 2015, 11:36 last edited by
                        #11

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

                        O 1 Reply Last reply 3 Jun 2015, 12:18
                        0
                        • S SGaist
                          3 Jun 2015, 11:34

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

                          O Offline
                          O Offline
                          opengpu2
                          wrote on 3 Jun 2015, 11:39 last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • O opengpu2
                            3 Jun 2015, 11:36

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

                            O Offline
                            O Offline
                            opengpu2
                            wrote on 3 Jun 2015, 12:18 last edited by
                            #13
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • joaopagottoJ Offline
                              joaopagottoJ Offline
                              joaopagotto
                              wrote on 3 Jun 2015, 20:12 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
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 3 Jun 2015, 20:41 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 4 Jun 2015, 01:45
                                0
                                • S SGaist
                                  3 Jun 2015, 20:41

                                  @joaopagotto where is the utf-8 handling ?

                                  O Offline
                                  O Offline
                                  opengpu2
                                  wrote on 4 Jun 2015, 01:45 last edited by opengpu2 6 Apr 2015, 11:19
                                  #16

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

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 4 Jun 2015, 21:27 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 5 Jun 2015, 01:32
                                    0
                                    • S SGaist
                                      4 Jun 2015, 21:27

                                      Again, what is xxx ?

                                      O Offline
                                      O Offline
                                      opengpu2
                                      wrote on 5 Jun 2015, 01:32 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
                                      • S SGaist
                                        2 Jun 2015, 09:31

                                        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 5 Jun 2015, 03:19 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
                                        • S Offline
                                          S Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 5 Jun 2015, 19:23 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 6 Jun 2015, 08:26
                                          0

                                          1/22

                                          2 Jun 2015, 08:04

                                          • Login

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