Solved How to get pointer from QPointer?
-
I'm having trouble trying to figure out how to get the wrapped pointer out of a QPointer. The below is failing to compile for me due to the method ScenePanel::scene(). What's the right way to extract the object in my QPointer so I can return it from this method?
C:\Qt\5.11.2\mingw53_32\include\QtCore\qpointer.h:86: error: invalid static_cast from type 'QObject*' to type 'Gf::Scene*' { return static_cast<T*>( wp.data()); } ^
My code:
class Scene : public QObject { Q_OBJECT }; class ScenePanel : public QWidget { Q_OBJECT QPointer<Scene> _scene; public: Scene* scene() { return _scene.data(); } };
-
I'm pretty sure the error does not come from the code above.
But why can't you return a QPointer<Scene> here instead? -
The error message itself actually points to the qpointer.h file, but the chain of error messages points back to the scene() method.
I could return a QPointer<Scene*>, but I feel that it ought to be possible to return a Scene* too. Also, I'm not sure how this will affect signals and slots if the setter takes a Scene*, but the getter returns a QPointer<Scene*>.
-
Scene is inherited from QObject ?
-
Yes. It subclasses QObject.
-
@kitfox said in How to get pointer from QPointer?:
class Scene : public QObject
{
Q_OBJECT
};class ScenePanel : public QWidget
{
Q_OBJECT
QPointer<Scene> _scene;
public:
Scene* scene() { return _scene.data(); }
};This code compiles perfectly - so as I said above your problem is somewhere else...
-
Are these two classes in different headers?
Please include theScene
header in theScenePanel
header and say how that goes. -
@kitfox said in How to get pointer from QPointer?:
invalid static_cast from type 'QObject*' to type 'Gf::Scene*'
What is
Gf
? -
@kshegunov You're right. They were in separate files. I was just using class Scene; to declare the class, but once I included the header instead I was able to compile.
-
@JKSH A namespace I'm using. Didn't include it in my sample code.