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. QObject-derived class address

QObject-derived class address

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 351 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.
  • J Offline
    J Offline
    jars121
    wrote on last edited by
    #1

    Hi all,

    This should be a really quick question; I'm really not sure how I haven't come up with a solution.

    I have a class which is ultimately a sub-class of QObject (via QQuickItem). I have a number of instantiated class objects in a QVector, and I'm looking to log various parameters for each of the instantiated class objects.

    I'd like to use the class object's address as a unique ID for the class (for logging purposes), but I can't seem to get (i.e. assign to an integer) the address. If I send the class object to qDebug, I get the following information in the console:

    myClass(0x23649724dc1)
    

    I'd like to store the hexadecimal value as an integer.

    The relevant part of the logging function which produces the above output is as follows:

    const QVector<myClass *> &objects = getClassObjects();
    
    for (int i = 0; i < objects.size(); i++) {
        qDebug() << objects.at(i);
    }
    

    If I deference the class object pointer and take its address:

    qDebug() << &*objects.at(i);
    

    I get the same output as above.

    W 1 Reply Last reply
    0
    • J jars121

      Hi all,

      This should be a really quick question; I'm really not sure how I haven't come up with a solution.

      I have a class which is ultimately a sub-class of QObject (via QQuickItem). I have a number of instantiated class objects in a QVector, and I'm looking to log various parameters for each of the instantiated class objects.

      I'd like to use the class object's address as a unique ID for the class (for logging purposes), but I can't seem to get (i.e. assign to an integer) the address. If I send the class object to qDebug, I get the following information in the console:

      myClass(0x23649724dc1)
      

      I'd like to store the hexadecimal value as an integer.

      The relevant part of the logging function which produces the above output is as follows:

      const QVector<myClass *> &objects = getClassObjects();
      
      for (int i = 0; i < objects.size(); i++) {
          qDebug() << objects.at(i);
      }
      

      If I deference the class object pointer and take its address:

      qDebug() << &*objects.at(i);
      

      I get the same output as above.

      W Offline
      W Offline
      wrosecrans
      wrote on last edited by
      #2

      @jars121 You don't say anything about the actual problem you hare having storing an address in an it.

      The question isn't QObject or Qt specifc. And it's generally bad practice to mix up pointers and integers this way. It often leads to bugs. But, as long as you use a large enough integer, it should work fine. Here's what it looks like to take the address of an object and store it in an integer: https://godbolt.org/z/cxG58nd8v

      #include <cstdint>
      class MyCoolClass {
      };
      
      int main() {
          MyCoolClass c;
          uint64_t foo = (uint64_t)&c;
          return foo;
      }
      
      J 1 Reply Last reply
      1
      • W wrosecrans

        @jars121 You don't say anything about the actual problem you hare having storing an address in an it.

        The question isn't QObject or Qt specifc. And it's generally bad practice to mix up pointers and integers this way. It often leads to bugs. But, as long as you use a large enough integer, it should work fine. Here's what it looks like to take the address of an object and store it in an integer: https://godbolt.org/z/cxG58nd8v

        #include <cstdint>
        class MyCoolClass {
        };
        
        int main() {
            MyCoolClass c;
            uint64_t foo = (uint64_t)&c;
            return foo;
        }
        
        J Offline
        J Offline
        jars121
        wrote on last edited by
        #3

        @wrosecrans said in QObject-derived class address:

        @jars121 You don't say anything about the actual problem you hare having storing an address in an it.

        The question isn't QObject or Qt specifc. And it's generally bad practice to mix up pointers and integers this way. It often leads to bugs. But, as long as you use a large enough integer, it should work fine. Here's what it looks like to take the address of an object and store it in an integer: https://godbolt.org/z/cxG58nd8v

        #include <cstdint>
        class MyCoolClass {
        };
        
        int main() {
            MyCoolClass c;
            uint64_t foo = (uint64_t)&c;
            return foo;
        }
        

        Thank you very much, this was exactly what I was looking for. I had mistakenly tried this exact approach without the (uint64_t) cast.

        C 1 Reply Last reply
        0
        • J jars121 has marked this topic as solved on
        • J jars121

          @wrosecrans said in QObject-derived class address:

          @jars121 You don't say anything about the actual problem you hare having storing an address in an it.

          The question isn't QObject or Qt specifc. And it's generally bad practice to mix up pointers and integers this way. It often leads to bugs. But, as long as you use a large enough integer, it should work fine. Here's what it looks like to take the address of an object and store it in an integer: https://godbolt.org/z/cxG58nd8v

          #include <cstdint>
          class MyCoolClass {
          };
          
          int main() {
              MyCoolClass c;
              uint64_t foo = (uint64_t)&c;
              return foo;
          }
          

          Thank you very much, this was exactly what I was looking for. I had mistakenly tried this exact approach without the (uint64_t) cast.

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          If you want a QObject to carry an identifier for logging I would suggest you set the objectName property rather than mangle a pointer. You can use anything you like in the QString. A UUID, time of creation, or a simple sequence number are all reasonable options if there is no "natural" identifier.

          J 1 Reply Last reply
          1
          • C ChrisW67

            If you want a QObject to carry an identifier for logging I would suggest you set the objectName property rather than mangle a pointer. You can use anything you like in the QString. A UUID, time of creation, or a simple sequence number are all reasonable options if there is no "natural" identifier.

            J Offline
            J Offline
            jars121
            wrote on last edited by
            #5

            @ChrisW67 That's exactly what I'm using the QObject address for. It's only to allocate parameters to that given object during the logging process, the longevity of the address is not a concern, I'm literally using it as unique uint64_t ID.

            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