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