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 pointer not respond value
Qt 6.11 is out! See what's new in the release blog

QVariant pointer not respond value

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.5k Views 4 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.
  • KutyusK Offline
    KutyusK Offline
    Kutyus
    wrote on last edited by
    #1

    Hi!

    I have a QVariant pointer:

    QVariant *param;
    

    I passing to an integer value:

    int p = 12;
    
    param = (QVariant*)(&p);
    

    But i do not get the value, i get always 0:

    param->toInt();
    

    The gdb write this:

    (gdb) print *param
    $7 = {d = {data = {c = 12 '\f', uc = 12 '\f', s = 12, sc = 12 '\f', us = 12, i = 12, u = 12, l = 12, ul = 12, b = 12, d = 5.1185933671348506e-309, f = 1.68155816e-44, real = 5.1185933671348506e-309, ll = 1036014831271948,
          ull = 1036014831271948, o = 0xc, ptr = 0xc, shared = 0xc}, type = 0, is_shared = 0, is_null = 0}}
    
    

    What do I get rid of?

    Thank you for answers.

    raven-worxR Venkatesh VV 2 Replies Last reply
    1
    • KutyusK Kutyus

      Hi!

      I have a QVariant pointer:

      QVariant *param;
      

      I passing to an integer value:

      int p = 12;
      
      param = (QVariant*)(&p);
      

      But i do not get the value, i get always 0:

      param->toInt();
      

      The gdb write this:

      (gdb) print *param
      $7 = {d = {data = {c = 12 '\f', uc = 12 '\f', s = 12, sc = 12 '\f', us = 12, i = 12, u = 12, l = 12, ul = 12, b = 12, d = 5.1185933671348506e-309, f = 1.68155816e-44, real = 5.1185933671348506e-309, ll = 1036014831271948,
            ull = 1036014831271948, o = 0xc, ptr = 0xc, shared = 0xc}, type = 0, is_shared = 0, is_null = 0}}
      
      

      What do I get rid of?

      Thank you for answers.

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

      @Kutyus said in QVariant pointer not respond value:

      param->toInt();

      honestly i do not know why your code doesn't crash o.O
      You are casting the stack-pointer address of p to a QVariant pointer and later you access this invalid pointer.

      I see what you are trying to do and it should rather look like this:

      param = new QVariant(p);
      

      --- 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

      kshegunovK 1 Reply Last reply
      3
      • KutyusK Kutyus

        Hi!

        I have a QVariant pointer:

        QVariant *param;
        

        I passing to an integer value:

        int p = 12;
        
        param = (QVariant*)(&p);
        

        But i do not get the value, i get always 0:

        param->toInt();
        

        The gdb write this:

        (gdb) print *param
        $7 = {d = {data = {c = 12 '\f', uc = 12 '\f', s = 12, sc = 12 '\f', us = 12, i = 12, u = 12, l = 12, ul = 12, b = 12, d = 5.1185933671348506e-309, f = 1.68155816e-44, real = 5.1185933671348506e-309, ll = 1036014831271948,
              ull = 1036014831271948, o = 0xc, ptr = 0xc, shared = 0xc}, type = 0, is_shared = 0, is_null = 0}}
        
        

        What do I get rid of?

        Thank you for answers.

        Venkatesh VV Offline
        Venkatesh VV Offline
        Venkatesh V
        wrote on last edited by
        #3

        Hi,
        @Kutyus said in QVariant pointer not respond value:

        param = (QVariant*)(&p);

        instead you do like this, try this
        param =QVariant::fromValue(&p);

        KutyusK raven-worxR 2 Replies Last reply
        1
        • KutyusK Offline
          KutyusK Offline
          Kutyus
          wrote on last edited by
          #4

          I did not write down everything, of course there is a

          param = new QVariant();
          

          line.

          1 Reply Last reply
          0
          • Venkatesh VV Venkatesh V

            Hi,
            @Kutyus said in QVariant pointer not respond value:

            param = (QVariant*)(&p);

            instead you do like this, try this
            param =QVariant::fromValue(&p);

            KutyusK Offline
            KutyusK Offline
            Kutyus
            wrote on last edited by
            #5

            @Venkatesh-V
            It is wrong, param is QVariant*, not QVariant.

            1 Reply Last reply
            0
            • Venkatesh VV Venkatesh V

              Hi,
              @Kutyus said in QVariant pointer not respond value:

              param = (QVariant*)(&p);

              instead you do like this, try this
              param =QVariant::fromValue(&p);

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

              @Venkatesh-V said in QVariant pointer not respond value:

              param =QVariant::fromValue(&p);

              this also results in an invalid pointer! DO NOT use stack-pointer addresses for such cases.

              @Kutyus said in QVariant pointer not respond value:

              I did not write down everything, of course there is a
              param = new QVariant();

              line.

              Still you want to use QVariant implicit conversion constructor / assignment operator. So pass the int value not it's pointer address!

              --- 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

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

                @Kutyus said in QVariant pointer not respond value:

                param->toInt();

                honestly i do not know why your code doesn't crash o.O
                You are casting the stack-pointer address of p to a QVariant pointer and later you access this invalid pointer.

                I see what you are trying to do and it should rather look like this:

                param = new QVariant(p);
                
                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                @raven-worx said in QVariant pointer not respond value:

                honestly i do not know why your code doesn't crash o.O

                Nothing to crash, but it's pure "luck". ;)

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • KutyusK Offline
                  KutyusK Offline
                  Kutyus
                  wrote on last edited by Kutyus
                  #8

                  I modified the concept, and use QVariant, not QVariant*, it's working.

                  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