I'm using QT 5.7 and, because of the libraries I have to use, I need to convert a QObject ('s derived class) to and from a void pointer.
A couple of definitions. I have a function that accepts only a void pointer, and my class derives from QObject:
void oneFunction(void *obj);
class MyObj : public QObject
{
...
};
Then I create and fill an object:
MyObj *ptr = new MyObj(parent);
ptr->.......
At some point I convert it to a void pointer, because I have to pass it to the function. This cast is done automatically:
oneFunction(ptr);
Then, after some time, I receive back the pointer I passed, and I need to convert it back to the original class. The pointer was not modified by the function:
void callbackFromOneFunction(void *theOldPointer)
{
MyObj *oldPtr = qobject_cast<MyObj*>(static_cast<QObject*>(theOldPointer));
if (oldPtr != nullptr)
{
... Now it's back, so use it
}
}
Now, is this whole procedure right? Is there some problem/leak you spot? Is there a better solution?
Thank you