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 const QVector<quint32> to vector<int>.
QtWS25 Last Chance

How to convert const QVector<quint32> to vector<int>.

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 9 Posters 1.7k 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
    Shankar B
    wrote on 21 Oct 2019, 11:04 last edited by
    #1

    Example:
    std::vector<int> l_sensorAddressList= static_cast<quint32>p_sensorAddressList.toStdVector();

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JohanSolo
      wrote on 21 Oct 2019, 11:08 last edited by
      #2

      Once I'm sure I need the conversion, I would use std::transform with a lambda.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      3
      • V Offline
        V Offline
        VRonin
        wrote on 21 Oct 2019, 12:22 last edited by VRonin
        #3

        Apart from the ususal disclaimer about converting unsigned to siged, you can use

        std::vector<int> l_sensorAddressList(
            reinterpret_cast<const int*>(p_sensorAddressList.constData())
            , reinterpret_cast<const int*>(p_sensorAddressList.constData()+p_sensorAddressList.size())
        );
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 21 Oct 2019, 13:28 last edited by
          #4

          While we're at it:

          std::copy(in.begin(), in.end(), std::back_inserter(out));
          

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

          F 1 Reply Last reply 21 Oct 2019, 16:32
          2
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 21 Oct 2019, 15:14 last edited by
            #5

            Hi,

            One even simpler version:

            std::vector<int> l_sensorAddressList(std::begin(p_sensorAddressList), std::end(p_sensorAddressList));
            

            or

            std::vector<int> l_sensorAddressList(p_sensorAddressList.begin(), p_sensorAddressList.end());
            

            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
            • V Offline
              V Offline
              VRonin
              wrote on 21 Oct 2019, 15:46 last edited by VRonin
              #6

              Just to clarify: @JohanSolo , @Christian-Ehrlicher and @SGaist 's solutions are correct. My solution should be faster as it's a simple memcpy but the other's might be safer (I don't really see how though)

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              J 1 Reply Last reply 21 Oct 2019, 17:13
              0
              • C Christian Ehrlicher
                21 Oct 2019, 13:28

                While we're at it:

                std::copy(in.begin(), in.end(), std::back_inserter(out));
                
                F Offline
                F Offline
                fcarney
                wrote on 21 Oct 2019, 16:32 last edited by
                #7

                @Christian-Ehrlicher Are iterators faster than indexing ([index]) ?

                C++ is a perfectly valid school of magic.

                1 Reply Last reply
                1
                • V VRonin
                  21 Oct 2019, 15:46

                  Just to clarify: @JohanSolo , @Christian-Ehrlicher and @SGaist 's solutions are correct. My solution should be faster as it's a simple memcpy but the other's might be safer (I don't really see how though)

                  J Offline
                  J Offline
                  JonB
                  wrote on 21 Oct 2019, 17:13 last edited by JonB
                  #8

                  @VRonin

                  My solution should be faster as it's a simple memcpy

                  Does yours assume 32-bit where the others don't? Is (part of) the question to convert from qint32 to whatever int is, which I assumed is 64-bit now?

                  C 1 Reply Last reply 21 Oct 2019, 18:29
                  0
                  • J JonB
                    21 Oct 2019, 17:13

                    @VRonin

                    My solution should be faster as it's a simple memcpy

                    Does yours assume 32-bit where the others don't? Is (part of) the question to convert from qint32 to whatever int is, which I assumed is 64-bit now?

                    C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 21 Oct 2019, 18:29 last edited by
                    #9

                    @JonB said in How to convert const QVector<quint32> to vector<int>.:

                    which I assumed is 64-bit now?

                    int is 32 bit, on every platform Qt is running on :)

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

                    J 1 Reply Last reply 21 Oct 2019, 20:43
                    1
                    • C Christian Ehrlicher
                      21 Oct 2019, 18:29

                      @JonB said in How to convert const QVector<quint32> to vector<int>.:

                      which I assumed is 64-bit now?

                      int is 32 bit, on every platform Qt is running on :)

                      J Offline
                      J Offline
                      JonB
                      wrote on 21 Oct 2019, 20:43 last edited by
                      #10

                      @Christian-Ehrlicher
                      Wot? Really?? I thought it was 64 on 64....

                      J 1 Reply Last reply 22 Oct 2019, 02:28
                      0
                      • J JonB
                        21 Oct 2019, 20:43

                        @Christian-Ehrlicher
                        Wot? Really?? I thought it was 64 on 64....

                        J Offline
                        J Offline
                        JKSH
                        Moderators
                        wrote on 22 Oct 2019, 02:28 last edited by JKSH
                        #11

                        @JonB said in How to convert const QVector<quint32> to vector<int>.:

                        Wot? Really?? I thought it was 64 on 64....

                        Pointer size is increased to 64 but integer size remains 32 on today's common 64-bit desktop architectures.

                        sizeof(qintptr) == sizeof(void*) != sizeof(int)

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

                        J.HilkJ 1 Reply Last reply 22 Oct 2019, 05:07
                        3
                        • J JKSH
                          22 Oct 2019, 02:28

                          @JonB said in How to convert const QVector<quint32> to vector<int>.:

                          Wot? Really?? I thought it was 64 on 64....

                          Pointer size is increased to 64 but integer size remains 32 on today's common 64-bit desktop architectures.

                          sizeof(qintptr) == sizeof(void*) != sizeof(int)

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on 22 Oct 2019, 05:07 last edited by
                          #12

                          @JKSH said in How to convert const QVector<quint32> to vector<int>.:

                          sizeof(qintptr) == sizeof(void*) != sizeof(int)

                          wow, I totally assumed differently,
                          I should make it habit to use the explicitly size defined qintXX/quintXX in the future...


                          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.

                          J 1 Reply Last reply 22 Oct 2019, 05:49
                          1
                          • J.HilkJ J.Hilk
                            22 Oct 2019, 05:07

                            @JKSH said in How to convert const QVector<quint32> to vector<int>.:

                            sizeof(qintptr) == sizeof(void*) != sizeof(int)

                            wow, I totally assumed differently,
                            I should make it habit to use the explicitly size defined qintXX/quintXX in the future...

                            J Offline
                            J Offline
                            JKSH
                            Moderators
                            wrote on 22 Oct 2019, 05:49 last edited by
                            #13

                            @J-Hilk said in How to convert const QVector<quint32> to vector<int>.:

                            I should make it habit to use the explicitly size defined qintXX/quintXX in the future...

                            Good idea!

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

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              JonB
                              wrote on 22 Oct 2019, 08:28 last edited by
                              #14

                              @JKSH , @J-Hilk

                              Pointer size is increased to 64 but integer size remains 32 on today's common 64-bit desktop architectures.

                              Yes, now I vaguely remember reading this, I am glad @J-Hilk shared my mis-assumption!

                              This is what comes of my being forced to use Python for Qt or JavaScript over so many years now. Ignorance of underlying type sizes! It has been a long time since sizeof(int) != sizeof(long) and sizeof(int) != sizeof(void*), probably not since the initial move from 16-bit to 32-bit architecture! :)

                              1 Reply Last reply
                              0

                              2/14

                              21 Oct 2019, 11:08

                              topic:navigator.unread, 12
                              • Login

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