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. effiently convert qstring to std::string or char
QtWS25 Last Chance

effiently convert qstring to std::string or char

Scheduled Pinned Locked Moved Solved General and Desktop
qstringstring
11 Posts 6 Posters 8.8k 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.
  • N Offline
    N Offline
    noone
    wrote on 9 Mar 2021, 06:05 last edited by
    #1

    If I'm correct then QString::toStdString() create new copy the underlying string no ? or does it not?

    What is most efficient way of converting QString to std::string or char* ? I won't need QString after conversion. I am going to receive large QString's from QML/JS and I don't wish to have two copies of same string in form of QString and std::string.

    J 1 Reply Last reply 9 Mar 2021, 06:10
    0
    • N noone
      9 Mar 2021, 07:13

      @jsulm right, That's what I'm currently doing. but is there any way to reuse the existing underlying data of QString ?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 9 Mar 2021, 07:25 last edited by jsulm 3 Sept 2021, 07:26
      #4

      @noone said in effiently convert qstring to std::string or char:

      but is there any way to reuse the existing underlying data of QString ?

      No, how would that work? QString and std::string are two completely different classes with their own internal memory handling. Also, QString uses UTF16 internally as far as I know, std::strinig does not. I'm wondering why you care so much? Do you experience any problems because of that (like memory consumption)?

      You can see what toStdString() does here: https://code.woboq.org/qt5/include/qt/QtCore/qstring.h.html#_ZNK7QString11toStdStringB5cxx11Ev

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

      toUtf8() returns a QByteArray and toStdString from QByteArray does:

      inline std::string QByteArray::toStdString() const
      { return std::string(constData(), length()); }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply 9 Mar 2021, 07:59
      3
      • N noone
        9 Mar 2021, 06:05

        If I'm correct then QString::toStdString() create new copy the underlying string no ? or does it not?

        What is most efficient way of converting QString to std::string or char* ? I won't need QString after conversion. I am going to receive large QString's from QML/JS and I don't wish to have two copies of same string in form of QString and std::string.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 9 Mar 2021, 06:10 last edited by
        #2

        @noone said in effiently convert qstring to std::string or char:

        QString::toStdString() create new copy the underlying string no ? or does it not?

        Sure it does.
        If you don't need both copies then don't keep the QString. You can also assign an empty string to your QString after conversion to std::string, so the QString frees the memory.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        N 1 Reply Last reply 9 Mar 2021, 07:13
        2
        • J jsulm
          9 Mar 2021, 06:10

          @noone said in effiently convert qstring to std::string or char:

          QString::toStdString() create new copy the underlying string no ? or does it not?

          Sure it does.
          If you don't need both copies then don't keep the QString. You can also assign an empty string to your QString after conversion to std::string, so the QString frees the memory.

          N Offline
          N Offline
          noone
          wrote on 9 Mar 2021, 07:13 last edited by
          #3

          @jsulm right, That's what I'm currently doing. but is there any way to reuse the existing underlying data of QString ?

          J 1 Reply Last reply 9 Mar 2021, 07:25
          0
          • N noone
            9 Mar 2021, 07:13

            @jsulm right, That's what I'm currently doing. but is there any way to reuse the existing underlying data of QString ?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 9 Mar 2021, 07:25 last edited by jsulm 3 Sept 2021, 07:26
            #4

            @noone said in effiently convert qstring to std::string or char:

            but is there any way to reuse the existing underlying data of QString ?

            No, how would that work? QString and std::string are two completely different classes with their own internal memory handling. Also, QString uses UTF16 internally as far as I know, std::strinig does not. I'm wondering why you care so much? Do you experience any problems because of that (like memory consumption)?

            You can see what toStdString() does here: https://code.woboq.org/qt5/include/qt/QtCore/qstring.h.html#_ZNK7QString11toStdStringB5cxx11Ev

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

            toUtf8() returns a QByteArray and toStdString from QByteArray does:

            inline std::string QByteArray::toStdString() const
            { return std::string(constData(), length()); }
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            N 1 Reply Last reply 9 Mar 2021, 07:59
            3
            • J jsulm
              9 Mar 2021, 07:25

              @noone said in effiently convert qstring to std::string or char:

              but is there any way to reuse the existing underlying data of QString ?

              No, how would that work? QString and std::string are two completely different classes with their own internal memory handling. Also, QString uses UTF16 internally as far as I know, std::strinig does not. I'm wondering why you care so much? Do you experience any problems because of that (like memory consumption)?

              You can see what toStdString() does here: https://code.woboq.org/qt5/include/qt/QtCore/qstring.h.html#_ZNK7QString11toStdStringB5cxx11Ev

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

              toUtf8() returns a QByteArray and toStdString from QByteArray does:

              inline std::string QByteArray::toStdString() const
              { return std::string(constData(), length()); }
              
              N Offline
              N Offline
              noone
              wrote on 9 Mar 2021, 07:59 last edited by
              #5

              @jsulm said in effiently convert qstring to std::string or char:

              . I'm wondering why you care so much?

              Just wanted to make sure I am not doing something wrong. I want get max performance as possible which is why I choose Qt :)

              J 1 Reply Last reply 9 Mar 2021, 15:19
              0
              • N noone
                9 Mar 2021, 07:59

                @jsulm said in effiently convert qstring to std::string or char:

                . I'm wondering why you care so much?

                Just wanted to make sure I am not doing something wrong. I want get max performance as possible which is why I choose Qt :)

                J Online
                J Online
                JoeCFD
                wrote on 9 Mar 2021, 15:19 last edited by
                #6

                @noone Qt is made for GUI, not for performance. If performance is involved, try to use std::string only.

                J 1 Reply Last reply 9 Mar 2021, 15:33
                0
                • J JoeCFD
                  9 Mar 2021, 15:19

                  @noone Qt is made for GUI, not for performance. If performance is involved, try to use std::string only.

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 9 Mar 2021, 15:33 last edited by
                  #7

                  @JoeCFD said in effiently convert qstring to std::string or char:

                  Qt is made for GUI, not for performance

                  I have to disagree here. Qt is way more than GUI. And Qt contains optimizations like https://doc.qt.io/qt-5/implicit-sharing.html in its containers (and also QString if I'm not mistaken), which C++ std lib does not have.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • J Online
                    J Online
                    JoeCFD
                    wrote on 9 Mar 2021, 15:49 last edited by
                    #8

                    https://stackoverflow.com/questions/14716053/do-stl-containers-use-implicit-sharing

                    Generally speaking, std C++ is better optimized. I do not use Qt at all if any speed related code is developed.

                    1 Reply Last reply
                    1
                    • S Offline
                      S Offline
                      SimonSchroeder
                      wrote on 10 Mar 2021, 07:51 last edited by
                      #9

                      @JoeCFD said in effiently convert qstring to std::string or char:

                      Generally speaking, std C++ is better optimized. I do not use Qt at all if any speed related code is developed.

                      I don't know of the current status, especially I don't know about differences between different STL implementations. However, my experience so far is that Qt is better optimized than STL. There is also a reason why EA implemented their own STL derivative. Especially Microsoft's STL used to be slow. I don't know how much that has changed.

                      Measurements are key for these kinds of statements. My last measurement dates back to 2011. Back then, I used Linux with GCC. I did some profiling of my code and found that most of the time was spend on some simple operations on std::vector. Profiling revealed some unneccesarily deep nesting of function calls within std::vector. I just swapped the variable declaration from std::vector to QVector. This brought a 3x performance gain for my specific problem.

                      Things might have changed since then (it's been a long time). Nevertheless, I would claim that Qt has different approaches to their implementation of containers. This might give them a performance advantage in certain situations.

                      Last, but not least, I think to recall that Qt wrote in a blog post about Qt 6 that their container classes are faded out as the STL has gotten so good over time.

                      J 1 Reply Last reply 10 Mar 2021, 08:45
                      0
                      • S SimonSchroeder
                        10 Mar 2021, 07:51

                        @JoeCFD said in effiently convert qstring to std::string or char:

                        Generally speaking, std C++ is better optimized. I do not use Qt at all if any speed related code is developed.

                        I don't know of the current status, especially I don't know about differences between different STL implementations. However, my experience so far is that Qt is better optimized than STL. There is also a reason why EA implemented their own STL derivative. Especially Microsoft's STL used to be slow. I don't know how much that has changed.

                        Measurements are key for these kinds of statements. My last measurement dates back to 2011. Back then, I used Linux with GCC. I did some profiling of my code and found that most of the time was spend on some simple operations on std::vector. Profiling revealed some unneccesarily deep nesting of function calls within std::vector. I just swapped the variable declaration from std::vector to QVector. This brought a 3x performance gain for my specific problem.

                        Things might have changed since then (it's been a long time). Nevertheless, I would claim that Qt has different approaches to their implementation of containers. This might give them a performance advantage in certain situations.

                        Last, but not least, I think to recall that Qt wrote in a blog post about Qt 6 that their container classes are faded out as the STL has gotten so good over time.

                        J Offline
                        J Offline
                        JKSH
                        Moderators
                        wrote on 10 Mar 2021, 08:45 last edited by JKSH 3 Oct 2021, 12:51
                        #10

                        @SimonSchroeder said in effiently convert qstring to std::string or char:

                        I think to recall that Qt wrote in a blog post about Qt 6 that their container classes are faded out as the STL has gotten so good over time.

                        Not quite. Qt algorithms were deprecated in favour of STL algorithms (e.g. qSort() -> std::sort(), see https://doc.qt.io/qt-5/qtalgorithms-obsolete.html ), but Qt containers and STL containers are apples-to-oranges; Qt containers are here to stay.

                        If you compare QVector with std::vector, profiling might reveal a minor performance advantage in one of them. For most applications though, raw performance is not the deciding factor, so this is not always important.

                        However! If you compare QString with std::string... QString is a powerful toolbox with an intuitive API; std::string is a puny toy box in comparison.

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

                        J 1 Reply Last reply 10 Mar 2021, 08:51
                        6
                        • J JKSH
                          10 Mar 2021, 08:45

                          @SimonSchroeder said in effiently convert qstring to std::string or char:

                          I think to recall that Qt wrote in a blog post about Qt 6 that their container classes are faded out as the STL has gotten so good over time.

                          Not quite. Qt algorithms were deprecated in favour of STL algorithms (e.g. qSort() -> std::sort(), see https://doc.qt.io/qt-5/qtalgorithms-obsolete.html ), but Qt containers and STL containers are apples-to-oranges; Qt containers are here to stay.

                          If you compare QVector with std::vector, profiling might reveal a minor performance advantage in one of them. For most applications though, raw performance is not the deciding factor, so this is not always important.

                          However! If you compare QString with std::string... QString is a powerful toolbox with an intuitive API; std::string is a puny toy box in comparison.

                          J Offline
                          J Offline
                          J.Hilk
                          Moderators
                          wrote on 10 Mar 2021, 08:51 last edited by
                          #11

                          @JKSH
                          I'll leave that here...
                          856158c1-5380-4d6f-8a1c-344983719657-image.png


                          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.

                          1 Reply Last reply
                          4

                          5/11

                          9 Mar 2021, 07:59

                          • Login

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