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. QString to char* conversion
Forum Updated to NodeBB v4.3 + New Features

QString to char* conversion

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 9 Posters 7.4k Views 4 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.
  • JonBJ JonB

    @Chris-Kawa said in QString to char* conversion:

    toLocal8Bit returns a temporary object.

    I'm beginning to sense that all (not just some of) the QString::to...() methods return a temporary object. I wish the docs said so!!!!

    Chris KawaC Offline
    Chris KawaC Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by Chris Kawa
    #16

    @JonB Well most functions return temporaries e.g. std::string foo() { return "something"; } is a temporary. It just depends on how you use them. The docs would have to say that on everything that doesn't return a pointer or a reference.

    foo();  // returns an r-value (temporary) that is never used, so destroyed immediately
    
    SomeFunction(foo()); //returns an r-value and passes it into the function. Lives in the scope of the function call.
    
    std::string s = foo(); //returns an r-value and assigns it to a new variable. 
    

    in this last case RVO also kicks in.

    1 Reply Last reply
    1
    • JonBJ JonB

      @Chris-Kawa said in QString to char* conversion:

      toLocal8Bit returns a temporary object.

      I'm beginning to sense that all (not just some of) the QString::to...() methods return a temporary object. I wish the docs said so!!!!

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #17

      @JonB said in QString to char* conversion:

      I wish the docs said so!!!!

      As soon as an object is returned from any function (even a plain C function) it's a temporary until you assign it to a local variable.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      J.HilkJ 1 Reply Last reply
      0
      • M mpergand

        @JonB said in QString to char* conversion:

        @Emre-MUTLU
        Yep, I agree, toLocal8Bit() seems to be the way to go. Maybe that's all std::string does anyway?

        std::string is encoding agnostic, so the encoding is the one you choose to use.
        ISO-8859 tried to be a "de facto standard" at least on windows and linux, but apple used MacRoman.
        Before unicode/utf8 transcoding between OS was pure nightmare.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #18

        @mpergand said in QString to char* conversion:

        std::string is encoding agnostic, so the encoding is the one you choose to use.

        And Qt defines that every std::string created from QString is UTF-8 encoded:

        inline std::string QString::toStdString() const
        { return toUtf8().toStdString(); }
        

        Even QString::toLocal8Bit() on Linux assumes the locale is UTF-8 - encoded without looking at the real locale, on Window the locale is respected.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • Chris KawaC Chris Kawa

          const char* ch will compile, but you can't use that afterwards. toStdString creates a temporary, which is destroysed after ;, so your variable points to released memory.

          If you want to use it in some function that takes a const char* as parameter you can do it either like this:

          SomeFunction(text.toStdString().c_str());   //temporary is in scope still
          

          or like this:

          std::string  s = text.toStdString();
          SomeFunction(s.c_str()); 
          

          but you can't do this:

          const char* ch = text.toStdString().c_str();
          //ch is pointing to garbage at this point
          SomeFunction(ch);
          
          D Offline
          D Offline
          Damian7546
          wrote on last edited by Damian7546
          #19

          @Chris-Kawa
          I would like to pass QString to pszTxt parameter in this function:
          fun1(HANDLE hDocument, PSTR pszTxt)

          Below doesn't works:

          void C56SdkApp::PrintTest(QString text)
          {
              char* ch = text.toUtf8().toStdString();
              fun1(hDocument,  ch );
          }
          
          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #20

            @Damian7546 Well it doesn't work because you're assigning std::string to char* variable. It doesn't make any sense.

            You have to check what encoding the function fun1 expects in its pszTxt parameter.
            If its local codepage then

            fun1(hDocument,  text.toLocal8Bit().constData());
            

            If it's UTF-8 then

            fun1(hDocument,  text.toUtf8().constData());
            
            D 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @JonB said in QString to char* conversion:

              I wish the docs said so!!!!

              As soon as an object is returned from any function (even a plain C function) it's a temporary until you assign it to a local variable.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #21

              @Christian-Ehrlicher said in QString to char* conversion:

              As soon as an object is returned from any function (even a plain C function) it's a temporary until you assign it to a local variable.

              any ? what if I returned a static object ?


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              Chris KawaC 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @Christian-Ehrlicher said in QString to char* conversion:

                As soon as an object is returned from any function (even a plain C function) it's a temporary until you assign it to a local variable.

                any ? what if I returned a static object ?

                Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #22

                @J-Hilk said in QString to char* conversion:

                any ? what if I returned a static object ?

                If you return it by value it's still a temporary (copy), unless you return a reference. But even if you do it's technically still a temporary. It's just that the reference is a temporary, not the object it references. To simplify - anything that doesn't have a name is a temporary.

                1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  @Damian7546 Well it doesn't work because you're assigning std::string to char* variable. It doesn't make any sense.

                  You have to check what encoding the function fun1 expects in its pszTxt parameter.
                  If its local codepage then

                  fun1(hDocument,  text.toLocal8Bit().constData());
                  

                  If it's UTF-8 then

                  fun1(hDocument,  text.toUtf8().constData());
                  
                  D Offline
                  D Offline
                  Damian7546
                  wrote on last edited by
                  #23

                  @Chris-Kawa said in QString to char* conversion:

                  @Damian7546 Well it doesn't work because you're assigning std::string to char* variable. It doesn't make any sense.
                  You have to check what encoding the function fun1 expects in its pszTxt parameter.
                  If its local codepage then
                  fun1(hDocument, text.toLocal8Bit().constData());

                  If it's UTF-8 then
                  fun1(hDocument, text.toUtf8().constData());

                  But in this way doesn't works. -> No matching fuction for call to fun1

                  Christian EhrlicherC Chris KawaC 2 Replies Last reply
                  0
                  • D Damian7546

                    @Chris-Kawa said in QString to char* conversion:

                    @Damian7546 Well it doesn't work because you're assigning std::string to char* variable. It doesn't make any sense.
                    You have to check what encoding the function fun1 expects in its pszTxt parameter.
                    If its local codepage then
                    fun1(hDocument, text.toLocal8Bit().constData());

                    If it's UTF-8 then
                    fun1(hDocument, text.toUtf8().constData());

                    But in this way doesn't works. -> No matching fuction for call to fun1

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #24

                    @Damian7546 said in QString to char* conversion:

                    No matching fuction for call to fun1

                    Then you should show the signature of this function....

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • D Damian7546

                      @Chris-Kawa said in QString to char* conversion:

                      @Damian7546 Well it doesn't work because you're assigning std::string to char* variable. It doesn't make any sense.
                      You have to check what encoding the function fun1 expects in its pszTxt parameter.
                      If its local codepage then
                      fun1(hDocument, text.toLocal8Bit().constData());

                      If it's UTF-8 then
                      fun1(hDocument, text.toUtf8().constData());

                      But in this way doesn't works. -> No matching fuction for call to fun1

                      Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by Chris Kawa
                      #25

                      @Damian7546 Ugh, right, the function takes PSTR, which is a non-const pointer. It's not nice of it, but it just means you have to give it a non-const pointer i.e.

                      fun1(hDocument,  text.toLocal8Bit().data());
                      

                      or

                      fun1(hDocument,  text.toUtf8().data());
                      

                      Just keep in mind that since it takes a non-const pointer it indicates that it can change the content of that string, which, if it does, will be lost, since it's a temporary.

                      Christian EhrlicherC D 2 Replies Last reply
                      0
                      • Chris KawaC Chris Kawa

                        @Damian7546 Ugh, right, the function takes PSTR, which is a non-const pointer. It's not nice of it, but it just means you have to give it a non-const pointer i.e.

                        fun1(hDocument,  text.toLocal8Bit().data());
                        

                        or

                        fun1(hDocument,  text.toUtf8().data());
                        

                        Just keep in mind that since it takes a non-const pointer it indicates that it can change the content of that string, which, if it does, will be lost, since it's a temporary.

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #26

                        @Chris-Kawa said in QString to char* conversion:

                        , if it does, will be lost, since it's a temporary.

                        And more than that - it will crash when the new string is longer than the actual one.

                        You really should read the API documentation for this instead doing try & error...

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        1
                        • Chris KawaC Chris Kawa

                          @Damian7546 Ugh, right, the function takes PSTR, which is a non-const pointer. It's not nice of it, but it just means you have to give it a non-const pointer i.e.

                          fun1(hDocument,  text.toLocal8Bit().data());
                          

                          or

                          fun1(hDocument,  text.toUtf8().data());
                          

                          Just keep in mind that since it takes a non-const pointer it indicates that it can change the content of that string, which, if it does, will be lost, since it's a temporary.

                          D Offline
                          D Offline
                          Damian7546
                          wrote on last edited by
                          #27

                          @Chris-Kawa Thank you very much. It works.

                          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