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. QFLAGS : signed value is out of range for enum constant
Forum Updated to NodeBB v4.3 + New Features

QFLAGS : signed value is out of range for enum constant

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 5 Posters 1.6k 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.
  • M MNGL

    Qt version: 4.8.7

    Compiler: Visual studio 2010:

    Following loop will not executed. gVersions[0].verNum is "xxxxx920P4", so I guess "xxxxx920P4"'s value has been truncated and now it equals to "0x00000000".
    for(int i=0; gVersions[i].verNum != xxxxxundefine ; i++)
    {

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

    @MNGL said in QFLAGS : signed value is out of range for enum constant:

    for(int i=0; gVersions[i].verNum != xxxxxundefine ; i++)

    Well, what do you think will happen if "gVersions[i].verNum != xxxxxundefine" never becomes false?

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

    M 1 Reply Last reply
    0
    • jsulmJ jsulm

      @MNGL said in QFLAGS : signed value is out of range for enum constant:

      for(int i=0; gVersions[i].verNum != xxxxxundefine ; i++)

      Well, what do you think will happen if "gVersions[i].verNum != xxxxxundefine" never becomes false?

      M Offline
      M Offline
      MNGL
      wrote on last edited by
      #7

      @jsulm said in QFLAGS : signed value is out of range for enum constant:

      xxxxxundefine

      "xxxxxundefine" is also in the gVersions[], "gVersions[i].verNum != xxxxxundefine" will become false.

      jsulmJ 1 Reply Last reply
      0
      • M MNGL

        @jsulm said in QFLAGS : signed value is out of range for enum constant:

        xxxxxundefine

        "xxxxxundefine" is also in the gVersions[], "gVersions[i].verNum != xxxxxundefine" will become false.

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

        @MNGL said in QFLAGS : signed value is out of range for enum constant:

        will become false

        Well, if you are sure.
        Else your app will crash with out of bounds exception. It is always a good idea to check the index for being in range.

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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MNGL
          wrote on last edited by
          #9

          Unfortunately, I'm the guy who gonna maintain this project, the guy who design this did not expect the enum value will be out of range in less than 40 items.

          JonBJ 1 Reply Last reply
          0
          • M MNGL

            Unfortunately, I'm the guy who gonna maintain this project, the guy who design this did not expect the enum value will be out of range in less than 40 items.

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

            @MNGL said in QFLAGS : signed value is out of range for enum constant:

            did not expect the enum value will be out of range in less than 40 items

            Your enum is a bit-flags enum. It does not hold sequential values (1, 2, 3, 4, ...), it holds sequential bits (1, 2, 4, 8, ...). So if you only give it 32-bits it will hold... up to 32 values. 40 items is not going to fit into it!

            1 Reply Last reply
            2
            • M Offline
              M Offline
              MNGL
              wrote on last edited by
              #11

              Hi, guys. Is there any example on using QBitArray to replace QFlag(bitmask enum in my case)?

              jsulmJ 1 Reply Last reply
              0
              • M MNGL

                Hi, guys. Is there any example on using QBitArray to replace QFlag(bitmask enum in my case)?

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

                @MNGL Did you try what @KroMignon suggested?

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

                M 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @MNGL Did you try what @KroMignon suggested?

                  M Offline
                  M Offline
                  MNGL
                  wrote on last edited by MNGL
                  #13

                  @jsulm Yes, I have. The QFlag class only works with int or unsinged int. https://doc.qt.io/qt-5/qflags.html.

                  Let'd assume we can use qint64 it just provides extra 32 items and will be used up soon in my case.

                  jsulmJ KroMignonK 2 Replies Last reply
                  0
                  • M MNGL

                    @jsulm Yes, I have. The QFlag class only works with int or unsinged int. https://doc.qt.io/qt-5/qflags.html.

                    Let'd assume we can use qint64 it just provides extra 32 items and will be used up soon in my case.

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

                    @MNGL Well, QBitArray and QFlag are two different things. You can't use QBitArray in code which expects QFlag.

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

                    M 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @MNGL Well, QBitArray and QFlag are two different things. You can't use QBitArray in code which expects QFlag.

                      M Offline
                      M Offline
                      MNGL
                      wrote on last edited by
                      #15

                      @jsulm

                      In below post, BitArray is mentioned as alternative solution.
                      https://stackoverflow.com/questions/1060760/what-to-do-when-bit-mask-flags-enum-gets-too-large

                      I guess I have to get ride of following QFlag code and rewrite the whole thing.

                      if(Versions.testFlag(xxxxx920P4) )

                      1 Reply Last reply
                      0
                      • M MNGL

                        @jsulm Yes, I have. The QFlag class only works with int or unsinged int. https://doc.qt.io/qt-5/qflags.html.

                        Let'd assume we can use qint64 it just provides extra 32 items and will be used up soon in my case.

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by
                        #16

                        @MNGL said in QFLAGS : signed value is out of range for enum constant:

                        Let'd assume we can use qint64 it just provides extra 32 items and will be used up soon in my case.

                        Just out of curiosity, it is not easier to change the "nature" of the enum and use plain values and not bit field?
                        Do you really need a bit field?

                        It maybe easier to replace the bitfield enum with and plain value enum and use QList/QVector on situation where you need to handle multiple values.

                        Just my 2 cts.

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          MNGL
                          wrote on last edited by
                          #17
                               typedef enum
                              {
                                  Undefined  = 0x00000000,
                                  H1      = 0x00000001,
                                  H2      = 0x00000002,
                                  H3      = 0x00000004,
                           
                              } Hardware;
                              Q_DECLARE_FLAGS(Hardwares, Hardware)
                          
                              typedef struct
                              {
                                  Version          verNum ;
                                  Hardwares   supportedHW ;
                          	    
                              } Settings ;
                          
                              const Settings gSettings[] =
                             {                                    
                                 {   xxxxx400 ,         H1|H2|H3  },
                                 {   xxxxx401 ,         H1|H3   },
                             } ;
                          

                          Now the "Hardwares" type is to store combinations of Hardware values. Due to the range limit, I want to change "supportedHW" to a different data structure. I tired following type:

                           QList<Hardware>     supportedHW;   //Got compiling errors:error C2552: 'supportedHW' : non-aggregates cannot be initialized with initializer list
                           Hardware  supportedHW[4] ;   //pass compile, but not easy to use as QList and QFlags
                          

                          Any suggestions? Thanks.

                          jsulmJ 1 Reply Last reply
                          0
                          • M MNGL
                                 typedef enum
                                {
                                    Undefined  = 0x00000000,
                                    H1      = 0x00000001,
                                    H2      = 0x00000002,
                                    H3      = 0x00000004,
                             
                                } Hardware;
                                Q_DECLARE_FLAGS(Hardwares, Hardware)
                            
                                typedef struct
                                {
                                    Version          verNum ;
                                    Hardwares   supportedHW ;
                            	    
                                } Settings ;
                            
                                const Settings gSettings[] =
                               {                                    
                                   {   xxxxx400 ,         H1|H2|H3  },
                                   {   xxxxx401 ,         H1|H3   },
                               } ;
                            

                            Now the "Hardwares" type is to store combinations of Hardware values. Due to the range limit, I want to change "supportedHW" to a different data structure. I tired following type:

                             QList<Hardware>     supportedHW;   //Got compiling errors:error C2552: 'supportedHW' : non-aggregates cannot be initialized with initializer list
                             Hardware  supportedHW[4] ;   //pass compile, but not easy to use as QList and QFlags
                            

                            Any suggestions? Thanks.

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

                            @MNGL @KroMignon asked you whether your enum really has to contain bitfields: means 2^x values (0001, 0010, 0100, ...).
                            If this is not required then use just normal numbers, then your range is what an int provides and is for sure enough.

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

                            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