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 write ULONG into QVariant ?
QtWS25 Last Chance

How to write ULONG into QVariant ?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 625 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
    SPlatten
    wrote on 11 Dec 2020, 21:23 last edited by
    #1

    I have a 'ulong' with the value '18446744073709551615', this is the absolute maximum value.

    I can see in the debugger that a QVariant has the correct value stored:

    1.8446744073709552e+19
    

    I try to copy this from the variable into a type of ulong:

    ulong ulngCheck;
    memcpy(&ulngCheck, varCheck.data(), sizeof(ulngCheck));
    blnPass = ((ulngCheck - ulngData) == 0);
    

    ulngData is another ulong which does have the value:

    18446744073709551615
    

    When I single step in the debugger over the memcpy operation, ulngCheck has the value:

    4895412794951729152
    

    Why ?

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Dec 2020, 21:31 last edited by SGaist 12 Dec 2020, 08:35
      #2

      Hi,

      Using:

      ulong ulngCheck = varCheck.value<ulong>();
      

      would likely be simpler.

      [edit: fixed type SGaist]

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 12 Dec 2020, 08:14
      5
      • S SGaist
        11 Dec 2020, 21:31

        Hi,

        Using:

        ulong ulngCheck = varCheck.value<ulong>();
        

        would likely be simpler.

        [edit: fixed type SGaist]

        S Offline
        S Offline
        SPlatten
        wrote on 12 Dec 2020, 08:14 last edited by SPlatten 12 Dec 2020, 08:45
        #3

        @SGaist said in How to write ULONG into QVariant ?:

        long

        Thanks, slight alteration:

        ulong ulngCheck = varCheck.value<ulong>();
        

        There are a few types where this method fails, these are: char, uchar and ulong, in the case of char and uchar using:

        char uValue = varCheck.value<char>();
        
        or
        
        uchar ucValue = varCheck.value<uchar>();
        

        The returned value is 0, yet in the debugger varCheck contains 'a'.

        ulong ulngValue = varCheck.value<ulong>();
        

        varCheck contains: 1.8446744073709552e+19
        Yet, ulngValue is converted to: 9223372036854775808
        So the result still isn't correct.

        The only way I found of getting the correct value back out of varCheck when it is assigned a ulong is:

        double dblCheck = varCheck.value<double>();
        

        This then extracts: 1.8446744073709552e+19.

        Kind Regards,
        Sy

        1 Reply Last reply
        -1
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 12 Dec 2020, 08:47 last edited by Christian Ehrlicher 12 Dec 2020, 08:47
          #4

          Provide some code instead pretty printed output from the debugger:

          int main(int argc, char *argv[])
          {
            QApplication a(argc, argv);
            qulonglong ql = 18446744073709551615ULL;
            uint64_t ui = ql;
            unsigned long ul1 = ql;
            ulong ul2 = ql;
            qDebug() << sizeof(ql) << sizeof(ui) << sizeof(ul1) << sizeof(ul2);
            qDebug() << ql << ui << ul1 << ul2;
            QVariant v1(ql);
            ulong out = v1.value<ulong>();
            qDebug() << out;
          }
          

          -->
          8 8 8 8
          18446744073709551615 18446744073709551615 18446744073709551615 18446744073709551615
          18446744073709551615

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

          S 1 Reply Last reply 12 Dec 2020, 08:50
          0
          • C Christian Ehrlicher
            12 Dec 2020, 08:47

            Provide some code instead pretty printed output from the debugger:

            int main(int argc, char *argv[])
            {
              QApplication a(argc, argv);
              qulonglong ql = 18446744073709551615ULL;
              uint64_t ui = ql;
              unsigned long ul1 = ql;
              ulong ul2 = ql;
              qDebug() << sizeof(ql) << sizeof(ui) << sizeof(ul1) << sizeof(ul2);
              qDebug() << ql << ui << ul1 << ul2;
              QVariant v1(ql);
              ulong out = v1.value<ulong>();
              qDebug() << out;
            }
            

            -->
            8 8 8 8
            18446744073709551615 18446744073709551615 18446744073709551615 18446744073709551615
            18446744073709551615

            S Offline
            S Offline
            SPlatten
            wrote on 12 Dec 2020, 08:50 last edited by
            #5

            @Christian-Ehrlicher, does your example produce the same result with:

             ulong ul2 = 18446744073709551615;
             QVariant v1(ul2);
            

            ?

            Kind Regards,
            Sy

            C 1 Reply Last reply 12 Dec 2020, 08:51
            0
            • S SPlatten
              12 Dec 2020, 08:50

              @Christian-Ehrlicher, does your example produce the same result with:

               ulong ul2 = 18446744073709551615;
               QVariant v1(ul2);
              

              ?

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 12 Dec 2020, 08:51 last edited by Christian Ehrlicher 12 Dec 2020, 08:53
              #6

              @SPlatten said in How to write ULONG into QVariant ?:

              does your example produce the same result with:

              So hard to try it by yourself? Your compiler should inform you about what you're doing wrong btw...

              /edit: http://www.cplusplus.com/doc/tutorial/constants/
              "These literal constants have a type, just like variables. By default, integer literals are of type int. "

              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

              3/6

              12 Dec 2020, 08:14

              • Login

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