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. QVariant to int8 and uint8_t
Forum Updated to NodeBB v4.3 + New Features

QVariant to int8 and uint8_t

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 3.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.
  • M Offline
    M Offline
    MrLibya
    wrote on last edited by
    #1

    Hello
    I'm reading some value from DB and put it into varables with type ( uint8_t , int8 )
    e.g

    nationality = rec.value(rec.indexOf("nationality")).toUInt();
    

    but i get this

    warning: implicit conversion loses integer precision: 'uint' (aka 'unsigned int') to 'int8_t' (aka 'signed char')
    

    so should i use static cast here ? or there's another way .

    raven-worxR aha_1980A 2 Replies Last reply
    0
    • M MrLibya

      Hello
      I'm reading some value from DB and put it into varables with type ( uint8_t , int8 )
      e.g

      nationality = rec.value(rec.indexOf("nationality")).toUInt();
      

      but i get this

      warning: implicit conversion loses integer precision: 'uint' (aka 'unsigned int') to 'int8_t' (aka 'signed char')
      

      so should i use static cast here ? or there's another way .

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @MrLibya said in QVariant to int8 and uint8_t:

      so should i use static cast here ? or there's another way .

      will result in the same warning.
      the variable nationality should be of the of the target type (uint) instead of int8_t, thats what this warning means

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      M 1 Reply Last reply
      3
      • M MrLibya

        Hello
        I'm reading some value from DB and put it into varables with type ( uint8_t , int8 )
        e.g

        nationality = rec.value(rec.indexOf("nationality")).toUInt();
        

        but i get this

        warning: implicit conversion loses integer precision: 'uint' (aka 'unsigned int') to 'int8_t' (aka 'signed char')
        

        so should i use static cast here ? or there's another way .

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by aha_1980
        #3

        @MrLibya The warning clearly states, that your destination data type is too small to take values an uint can hold. Also, you mix signed and unsigned integers.

        The very correct way would be (assuming nationality is uint8_t:

        bool ok = false;
        uint value = rec.value(rec.indexOf("nationality")).toUInt(&ok);
        if (ok && value < 255)
          nationality = uint8_t(value); // or static_cast<uint8_t>
        else
          nationality = 0;
        

        If nationality is signed, i.e. int8_t (does that make sense?!), you should use toInt() instead and adjust the range checks.

        Regards

        Qt has to stay free or it will die.

        M 1 Reply Last reply
        1
        • raven-worxR raven-worx

          @MrLibya said in QVariant to int8 and uint8_t:

          so should i use static cast here ? or there's another way .

          will result in the same warning.
          the variable nationality should be of the of the target type (uint) instead of int8_t, thats what this warning means

          M Offline
          M Offline
          MrLibya
          wrote on last edited by
          #4

          @raven-worx
          i know what's that mean but this varable can't get value more then 127 so i don't want use uint for small values .

          1 Reply Last reply
          0
          • aha_1980A aha_1980

            @MrLibya The warning clearly states, that your destination data type is too small to take values an uint can hold. Also, you mix signed and unsigned integers.

            The very correct way would be (assuming nationality is uint8_t:

            bool ok = false;
            uint value = rec.value(rec.indexOf("nationality")).toUInt(&ok);
            if (ok && value < 255)
              nationality = uint8_t(value); // or static_cast<uint8_t>
            else
              nationality = 0;
            

            If nationality is signed, i.e. int8_t (does that make sense?!), you should use toInt() instead and adjust the range checks.

            Regards

            M Offline
            M Offline
            MrLibya
            wrote on last edited by
            #5

            @aha_1980
            the value from the db always postive and less then 127 so i only need to read it from the db but the function dosen't have toInt8 so this why i use toUInt , i'm asking is there a function for Int8 or i only should use static cast .

            aha_1980A 1 Reply Last reply
            0
            • M MrLibya

              @aha_1980
              the value from the db always postive and less then 127 so i only need to read it from the db but the function dosen't have toInt8 so this why i use toUInt , i'm asking is there a function for Int8 or i only should use static cast .

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @MrLibya said in QVariant to int8 and uint8_t:

              the value from the db always postive and less then 127

              Then either use uint or the approach I explained above. There is no need to use an signed integer like int8_tl

              Qt has to stay free or it will die.

              M 1 Reply Last reply
              0
              • aha_1980A aha_1980

                @MrLibya said in QVariant to int8 and uint8_t:

                the value from the db always postive and less then 127

                Then either use uint or the approach I explained above. There is no need to use an signed integer like int8_tl

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

                @aha_1980
                Well i'm only trying to not use memory more then i need , like why i use 4 Bytes and i can use 1 Byte ?

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

                  @MrLibya said in QVariant to int8 and uint8_t:

                  like why i use 4 Bytes and i can use 1 Byte ?

                  Do you use them in an array or another struct? If not due to the alignment it will at least use 32 bits for it.
                  Don't try to optimize without measuring first.

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

                  M 1 Reply Last reply
                  2
                  • Christian EhrlicherC Christian Ehrlicher

                    @MrLibya said in QVariant to int8 and uint8_t:

                    like why i use 4 Bytes and i can use 1 Byte ?

                    Do you use them in an array or another struct? If not due to the alignment it will at least use 32 bits for it.
                    Don't try to optimize without measuring first.

                    M Offline
                    M Offline
                    MrLibya
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher
                    i'm using them in class
                    alt text

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

                      First your class members are not optimal aligned for low memory usage so why do you care for this specific member and secondly - this class is so big that a max of 3 more bytes don't count in any way.

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

                      M 1 Reply Last reply
                      5
                      • Christian EhrlicherC Christian Ehrlicher

                        First your class members are not optimal aligned for low memory usage so why do you care for this specific member and secondly - this class is so big that a max of 3 more bytes don't count in any way.

                        M Offline
                        M Offline
                        MrLibya
                        wrote on last edited by
                        #11

                        @Christian-Ehrlicher
                        ok thanks :)

                        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