Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there a possibility to set a QBitArray using a long int?
Qt 6.12 LTS is out! See what's new in the release blog

Is there a possibility to set a QBitArray using a long int?

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 6 Posters 2.5k Views 1 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Qt5 or Qt6?

    https://doc.qt.io/qt-6/qbitarray.html#toUInt32

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

    K 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      Qt5 or Qt6?

      https://doc.qt.io/qt-6/qbitarray.html#toUInt32

      K Offline
      K Offline
      Kevin470
      wrote on last edited by
      #3

      @Christian-Ehrlicher Qt6.
      But I am looking for fromUInt32 and not toUInt32 unfortunately.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Then use https://doc.qt.io/qt-6/qbitarray.html#fromBits

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

        K 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          Then use https://doc.qt.io/qt-6/qbitarray.html#fromBits

          K Offline
          K Offline
          Kevin470
          wrote on last edited by Kevin470
          #5

          @Christian-Ehrlicher
          Could you help me out with an example please?
          I tried

              QBitArray bitArray;
              quint32 intvalue = 9999999;
          
              bitArray.fromBits(intvalue,32);
              qInfo() << "Size:" << bitArray.size();
          

          and it throws an error saying main.cpp:12:23: Cannot initialize a parameter of type 'const char *' with an lvalue of type 'quint32' (aka 'unsigned int')

          If I try the one below, the program crashes.

              QBitArray bitArray;
              quint32 intvalue = 9999999;
          
              bitArray.fromBits((char*)(intvalue),32);
              qInfo() << "Size:" << bitArray.size();
          
          jsulmJ 1 Reply Last reply
          0
          • K Kevin470

            @Christian-Ehrlicher
            Could you help me out with an example please?
            I tried

                QBitArray bitArray;
                quint32 intvalue = 9999999;
            
                bitArray.fromBits(intvalue,32);
                qInfo() << "Size:" << bitArray.size();
            

            and it throws an error saying main.cpp:12:23: Cannot initialize a parameter of type 'const char *' with an lvalue of type 'quint32' (aka 'unsigned int')

            If I try the one below, the program crashes.

                QBitArray bitArray;
                quint32 intvalue = 9999999;
            
                bitArray.fromBits((char*)(intvalue),32);
                qInfo() << "Size:" << bitArray.size();
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @Kevin470 said in Is there a possibility to set a QBitArray using a long int?:

            bitArray.fromBits((char*)(intvalue),32);

            Shouldn't it be

            bitArray.fromBits((char*)(&intvalue), 32);
            

            ?

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

            K 1 Reply Last reply
            3
            • jsulmJ jsulm

              @Kevin470 said in Is there a possibility to set a QBitArray using a long int?:

              bitArray.fromBits((char*)(intvalue),32);

              Shouldn't it be

              bitArray.fromBits((char*)(&intvalue), 32);
              

              ?

              K Offline
              K Offline
              Kevin470
              wrote on last edited by
              #7

              @jsulm Oops sorry. Yes it should be bitArray.fromBits((char*)(&intvalue), 32);

              But it still prints Size: 0

              jsulmJ 1 Reply Last reply
              0
              • K Kevin470

                @jsulm Oops sorry. Yes it should be bitArray.fromBits((char*)(&intvalue), 32);

                But it still prints Size: 0

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @Kevin470 said in Is there a possibility to set a QBitArray using a long int?:

                But it still prints Size: 0

                Yes, because https://doc.qt.io/qt-6/qbitarray.html#fromBits is a static method...

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

                K 1 Reply Last reply
                4
                • jsulmJ jsulm

                  @Kevin470 said in Is there a possibility to set a QBitArray using a long int?:

                  But it still prints Size: 0

                  Yes, because https://doc.qt.io/qt-6/qbitarray.html#fromBits is a static method...

                  K Offline
                  K Offline
                  Kevin470
                  wrote on last edited by
                  #9

                  @jsulm Thanks a lot. Works Perfectly!

                  QBitArray bitArray;
                      quint32 intvalue = 9999999;
                  
                      bitArray = QBitArray::fromBits((char*)(&intvalue),32);
                      qInfo() << "Size:" << bitArray.size();
                  
                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • K Kevin470

                    @jsulm Thanks a lot. Works Perfectly!

                    QBitArray bitArray;
                        quint32 intvalue = 9999999;
                    
                        bitArray = QBitArray::fromBits((char*)(&intvalue),32);
                        qInfo() << "Size:" << bitArray.size();
                    
                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @Kevin470 Then please mark this topic as solved, thx.

                    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
                    • J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #11

                      where's the downvote button!?!?! c-style casts 🫢


                      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.

                      Pl45m4P JonBJ 2 Replies Last reply
                      1
                      • J.HilkJ J.Hilk

                        where's the downvote button!?!?! c-style casts 🫢

                        Pl45m4P Offline
                        Pl45m4P Offline
                        Pl45m4
                        wrote on last edited by
                        #12

                        @J-Hilk said in Is there a possibility to set a QBitArray using a long int?:

                        where's the downvote button

                        Casted to (void)downvoteBtn;

                        :)


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        1 Reply Last reply
                        2
                        • J.HilkJ J.Hilk

                          where's the downvote button!?!?! c-style casts 🫢

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #13

                          @J-Hilk
                          I always have to go look it up, should this one be reinterpret_cast<>, or has C++ introduced yet another new one of these casting templates to use for this case?

                          Not to mention that the other day @Chris-Kawa wrote in https://forum.qt.io/topic/139176/qgraphicsitem-conversion-into-long-using-reinterpret_cast/2

                          I'll just mention that on some platforms pointers to different types can have different sizes too i.e. sizeof(char*) != sizeof(int*), so, although unlikely that it affects you, what you're doing is a risky business.

                          which seems to me to make a nonsense of half of these castings anyway...!

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #14

                            @JonB

                            • static_cast for basic types
                            • dynamic_cast for pointers or references to polymorphic class types;
                            • const_cast const or volatile qualifiers;
                            • reinterpret_cast is for special circumstances and you think you know better than the compiler. Converting between pointers or references and completely unrelated types.

                            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.

                            JonBJ 1 Reply Last reply
                            4
                            • JonBJ JonB

                              @J-Hilk
                              I always have to go look it up, should this one be reinterpret_cast<>, or has C++ introduced yet another new one of these casting templates to use for this case?

                              Not to mention that the other day @Chris-Kawa wrote in https://forum.qt.io/topic/139176/qgraphicsitem-conversion-into-long-using-reinterpret_cast/2

                              I'll just mention that on some platforms pointers to different types can have different sizes too i.e. sizeof(char*) != sizeof(int*), so, although unlikely that it affects you, what you're doing is a risky business.

                              which seems to me to make a nonsense of half of these castings anyway...!

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

                              @JonB said in Is there a possibility to set a QBitArray using a long int?:

                              which seems to me to make a nonsense of half of these castings anyway...!

                              They add validation if the cast is valid which is not available with the c-style cast.

                              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
                              • J.HilkJ J.Hilk

                                @JonB

                                • static_cast for basic types
                                • dynamic_cast for pointers or references to polymorphic class types;
                                • const_cast const or volatile qualifiers;
                                • reinterpret_cast is for special circumstances and you think you know better than the compiler. Converting between pointers or references and completely unrelated types.
                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #16

                                @J-Hilk said in Is there a possibility to set a QBitArray using a long int?:

                                reinterpret_cast is for special circumstances and you think you know better than the compiler. Converting between pointers or references and completely unrelated types.

                                And that (reinterpret_cast<>) is what should be used in the case we are discussing (int * -> char *), right?

                                [Separate: did I see recently that C++20 or whatever has introduced yet another new somethingelse_cast<> construct beyond these 4, or do you think I'm imagining this?]

                                @Christian-Ehrlicher said in Is there a possibility to set a QBitArray using a long int?:

                                They add validation if the cast is valid which is not available with the c-style cast.

                                But not in the reinterpret_cast<> case which (I believe) we are going to use here, right?

                                J.HilkJ 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @J-Hilk said in Is there a possibility to set a QBitArray using a long int?:

                                  reinterpret_cast is for special circumstances and you think you know better than the compiler. Converting between pointers or references and completely unrelated types.

                                  And that (reinterpret_cast<>) is what should be used in the case we are discussing (int * -> char *), right?

                                  [Separate: did I see recently that C++20 or whatever has introduced yet another new somethingelse_cast<> construct beyond these 4, or do you think I'm imagining this?]

                                  @Christian-Ehrlicher said in Is there a possibility to set a QBitArray using a long int?:

                                  They add validation if the cast is valid which is not available with the c-style cast.

                                  But not in the reinterpret_cast<> case which (I believe) we are going to use here, right?

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

                                  @JonB said in Is there a possibility to set a QBitArray using a long int?:

                                  [Separate: did I see recently that C++20 or whatever has introduced yet another new somethingelse_cast<> construct beyond these 4, or do you think I'm imagining this?]

                                  std::bit_cast

                                  but, it's a library feature not a language feature.

                                  Technically, if the OP uses c++20 it would be the "saver" option, otherwise reinterpret_cast


                                  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
                                  3

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt
                                  • Unsolved