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. How to get pointer from QPointer?

How to get pointer from QPointer?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 2.1k Views 2 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by kitfox
    #1

    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(); }
    };
    
    kshegunovK JKSHJ 2 Replies Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I'm pretty sure the error does not come from the code above.
      But why can't you return a QPointer<Scene> here instead?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kitfox
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Scene is inherited from QObject ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kitfox
            wrote on last edited by
            #5

            Yes. It subclasses QObject.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #6

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

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              3
              • K kitfox

                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(); }
                };
                
                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #7

                Are these two classes in different headers?
                Please include the Scene header in the ScenePanel header and say how that goes.

                Read and abide by the Qt Code of Conduct

                K 1 Reply Last reply
                5
                • K kitfox

                  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(); }
                  };
                  
                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  @kitfox said in How to get pointer from QPointer?:

                  invalid static_cast from type 'QObject*' to type 'Gf::Scene*'
                  

                  What is Gf?

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  K 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    Are these two classes in different headers?
                    Please include the Scene header in the ScenePanel header and say how that goes.

                    K Offline
                    K Offline
                    kitfox
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • JKSHJ JKSH

                      @kitfox said in How to get pointer from QPointer?:

                      invalid static_cast from type 'QObject*' to type 'Gf::Scene*'
                      

                      What is Gf?

                      K Offline
                      K Offline
                      kitfox
                      wrote on last edited by
                      #10

                      @JKSH A namespace I'm using. Didn't include it in my sample code.

                      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