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?
Forum Updated to NodeBB v4.3 + New Features

How to get pointer from QPointer?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 1.8k 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 11 Nov 2018, 07:29 last edited by kitfox 11 Nov 2018, 07:32
    #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(); }
    };
    
    K J 2 Replies Last reply 11 Nov 2018, 11:28
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 11 Nov 2018, 07:32 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 11 Nov 2018, 07:36 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
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 11 Nov 2018, 07:47 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 11 Nov 2018, 07:50 last edited by
            #5

            Yes. It subclasses QObject.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 11 Nov 2018, 08:35 last edited by Christian Ehrlicher 11 Nov 2018, 08:36
              #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
                11 Nov 2018, 07:29

                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(); }
                };
                
                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 11 Nov 2018, 11:28 last edited by kshegunov 11 Nov 2018, 11:29
                #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 14 Nov 2018, 19:08
                5
                • K kitfox
                  11 Nov 2018, 07:29

                  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(); }
                  };
                  
                  J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 12 Nov 2018, 06:09 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 14 Nov 2018, 19:08
                  0
                  • K kshegunov
                    11 Nov 2018, 11:28

                    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 14 Nov 2018, 19:08 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
                    • J JKSH
                      12 Nov 2018, 06:09

                      @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 14 Nov 2018, 19:08 last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      1/10

                      11 Nov 2018, 07:29

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved