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. QtObject derived class as class member
Forum Updated to NodeBB v4.3 + New Features

QtObject derived class as class member

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 368 Views 1 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.
  • R Offline
    R Offline
    Redman
    wrote on 4 Sept 2024, 05:57 last edited by Redman 9 Apr 2024, 05:57
    #1

    Hello,

    I often find myself in a situation like this

    class A : public QObject {
    Q_OBJECT
    public:
    .
    .
    private:
    QObjectDerivedClass m_obj1;
    }
    

    How should this handled best in qt?

    • QObjectDerivedClass should be a raw pointer and in its ctor, class A should be set as parent. So when A gets destroyed, QObjectDerivedClass also does?
    • QObjectDerivedClass should be a QScopedPointer, following the rest as in the previous suggestion
    • QObjectDerivedClass should be a QSharedPointer, leaving open the possiblity that the QObjectDerivedClass is used in some other context and it doesnt get destroyed if A is destroyed
    C 1 Reply Last reply 4 Sept 2024, 06:02
    0
    • R Redman
      4 Sept 2024, 05:57

      Hello,

      I often find myself in a situation like this

      class A : public QObject {
      Q_OBJECT
      public:
      .
      .
      private:
      QObjectDerivedClass m_obj1;
      }
      

      How should this handled best in qt?

      • QObjectDerivedClass should be a raw pointer and in its ctor, class A should be set as parent. So when A gets destroyed, QObjectDerivedClass also does?
      • QObjectDerivedClass should be a QScopedPointer, following the rest as in the previous suggestion
      • QObjectDerivedClass should be a QSharedPointer, leaving open the possiblity that the QObjectDerivedClass is used in some other context and it doesnt get destroyed if A is destroyed
      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 4 Sept 2024, 06:02 last edited by
      #2

      @Redman said in QtObject derived class as class member:

      QObjectDerivedClass

      What is this class and what should it do? Note: You can't copy classes which derive from QObject.

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

      R 1 Reply Last reply 4 Sept 2024, 06:04
      0
      • C Christian Ehrlicher
        4 Sept 2024, 06:02

        @Redman said in QtObject derived class as class member:

        QObjectDerivedClass

        What is this class and what should it do? Note: You can't copy classes which derive from QObject.

        R Offline
        R Offline
        Redman
        wrote on 4 Sept 2024, 06:04 last edited by
        #3

        @Christian-Ehrlicher The use of that class is specific to class A mostly. It's not some generic QObjectDerivedClass which is needed throughout the application.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 4 Sept 2024, 06:04 last edited by
          #4

          Then simply use it as value.

          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
          1
          • S Offline
            S Offline
            SimonSchroeder
            wrote on 4 Sept 2024, 06:53 last edited by
            #5

            Qt is designed around proper OO principles. This means that in theory m_obj1 can be of type QObjectDerivedClass or maybe classes derived from QObjectDerivedClass in the future. It makes the most sense to use a pointer to get proper polymorphism.

            One major thing of Qt is that you create objects with a parent. This does not work with objects created on the stack as Qt would still try to call delete on all of its children which will fail for objects allocated on the stack. With Qt you should be using plain pointers (and plain new) for your objects. And always set the parent for objects derived from QObject. Then you also don't need any smart pointer classes.

            I've also never use QScopedPointer or QSharedPointer. QScopedPointer hasn't been updated with move semantics. I would rather prefer std::unique_ptr instead. I also don't know if QSharedPointer has any advantages over std::shared_ptr. Wherever it makes sense I personally go for standard C++ instead of Qt. (I never had to use any smart pointer with objects derived from QObject because the parent takes care of this.)

            R 1 Reply Last reply 4 Sept 2024, 11:06
            1
            • S SimonSchroeder
              4 Sept 2024, 06:53

              Qt is designed around proper OO principles. This means that in theory m_obj1 can be of type QObjectDerivedClass or maybe classes derived from QObjectDerivedClass in the future. It makes the most sense to use a pointer to get proper polymorphism.

              One major thing of Qt is that you create objects with a parent. This does not work with objects created on the stack as Qt would still try to call delete on all of its children which will fail for objects allocated on the stack. With Qt you should be using plain pointers (and plain new) for your objects. And always set the parent for objects derived from QObject. Then you also don't need any smart pointer classes.

              I've also never use QScopedPointer or QSharedPointer. QScopedPointer hasn't been updated with move semantics. I would rather prefer std::unique_ptr instead. I also don't know if QSharedPointer has any advantages over std::shared_ptr. Wherever it makes sense I personally go for standard C++ instead of Qt. (I never had to use any smart pointer with objects derived from QObject because the parent takes care of this.)

              R Offline
              R Offline
              Redman
              wrote on 4 Sept 2024, 11:06 last edited by
              #6

              @SimonSchroeder said in QtObject derived class as class member:

              prefer std::unique_ptr instead. I also don't know if QSharedPointer has any advantages over std::shared_ptr. Wherever it makes

              So as long as I make sure, that the hierarchy parent child is satisfied I can use raw pointers.
              When passing childs to other objects, I can still rely on the parent of child to take care of its deletion.

              But what happens if the other objects lives longer than the parent and so child gets deleted while the other objects still acces the child.
              Should I work with shared pointers here?

              J 1 Reply Last reply 5 Sept 2024, 05:46
              0
              • R Redman
                4 Sept 2024, 11:06

                @SimonSchroeder said in QtObject derived class as class member:

                prefer std::unique_ptr instead. I also don't know if QSharedPointer has any advantages over std::shared_ptr. Wherever it makes

                So as long as I make sure, that the hierarchy parent child is satisfied I can use raw pointers.
                When passing childs to other objects, I can still rely on the parent of child to take care of its deletion.

                But what happens if the other objects lives longer than the parent and so child gets deleted while the other objects still acces the child.
                Should I work with shared pointers here?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 5 Sept 2024, 05:46 last edited by
                #7

                @Redman said in QtObject derived class as class member:

                Should I work with shared pointers here?

                I would say you should rethink your design.
                Other objects should not store pointers to children of other objects.
                Pass these pointers only when needed to other objects and don't store these pointers there.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2

                1/7

                4 Sept 2024, 05:57

                • Login

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