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. QObject and parent - two questions

QObject and parent - two questions

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 455 Views
  • 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.
  • T Offline
    T Offline
    TomNow99
    wrote on 19 Jun 2023, 14:52 last edited by TomNow99
    #1

    Hi,

    I have two questions about parent in QT:

    1. I would like to use QThread in my application and I would like to move one object ( inherits QObject - class XYZ ) to other thread ( moveToThread() ). I have in this class XYZ member variable QProcess and QProcess pointer variable like this:
    class XYZ: public QObject {
       Q_OBJECT
       QProcess process;
       QProcess *process2;
    public:
       XYZ(QObject *parent = nullptr );
    }
    

    I know that, when I move object to thread, I have to move its children too. So I have to do:
    process2->setParent(this), so I have to change process2 parent
    I don't what should I do with member variable ( not pointer ). Something like that:
    process.setParent(this); ?
    Maybe member variable ( not pointer ) have parents by default and I don't have to setParent?

    1. What If I do:
    someQObject->setParent(someQObject2);
    delete someQObject
    

    ?

    I know that I don't have to delete pointer if it has parent, but what if I delete?

    Thank you for answer

    C 1 Reply Last reply 19 Jun 2023, 15:04
    0
    • T TomNow99
      19 Jun 2023, 14:52

      Hi,

      I have two questions about parent in QT:

      1. I would like to use QThread in my application and I would like to move one object ( inherits QObject - class XYZ ) to other thread ( moveToThread() ). I have in this class XYZ member variable QProcess and QProcess pointer variable like this:
      class XYZ: public QObject {
         Q_OBJECT
         QProcess process;
         QProcess *process2;
      public:
         XYZ(QObject *parent = nullptr );
      }
      

      I know that, when I move object to thread, I have to move its children too. So I have to do:
      process2->setParent(this), so I have to change process2 parent
      I don't what should I do with member variable ( not pointer ). Something like that:
      process.setParent(this); ?
      Maybe member variable ( not pointer ) have parents by default and I don't have to setParent?

      1. What If I do:
      someQObject->setParent(someQObject2);
      delete someQObject
      

      ?

      I know that I don't have to delete pointer if it has parent, but what if I delete?

      Thank you for answer

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 19 Jun 2023, 15:04 last edited by
      #2

      @TomNow99 said in QObject and parent - two questions:

      I know that, when I move object to thread, I have to move its children too.

      This is wrong - an object and it's children can only live in one thread so the children are moved automatically.

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

      T 1 Reply Last reply 19 Jun 2023, 17:55
      0
      • C Christian Ehrlicher
        19 Jun 2023, 15:04

        @TomNow99 said in QObject and parent - two questions:

        I know that, when I move object to thread, I have to move its children too.

        This is wrong - an object and it's children can only live in one thread so the children are moved automatically.

        T Offline
        T Offline
        TomNow99
        wrote on 19 Jun 2023, 17:55 last edited by TomNow99
        #3

        @Christian-Ehrlicher Thank you for answer.

        Question 1
        I see here 2 options: I have a class member, which isn't pointer, class member, which is pointer.

        1a) pointer:
        I have to set parent. When I not set parent, it will be wrong. Of course when I set parent and move object to other thread, the children are moved automaitcally. But first I have to tell QT "this is the child of this object".
        1b) not pointer:
        And here I don't know. Have I set parent like this:
        object.setParent(someObject);
        ?

        C J 2 Replies Last reply 19 Jun 2023, 18:06
        0
        • T TomNow99
          19 Jun 2023, 17:55

          @Christian-Ehrlicher Thank you for answer.

          Question 1
          I see here 2 options: I have a class member, which isn't pointer, class member, which is pointer.

          1a) pointer:
          I have to set parent. When I not set parent, it will be wrong. Of course when I set parent and move object to other thread, the children are moved automaitcally. But first I have to tell QT "this is the child of this object".
          1b) not pointer:
          And here I don't know. Have I set parent like this:
          object.setParent(someObject);
          ?

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 19 Jun 2023, 18:06 last edited by Christian Ehrlicher
          #4

          @TomNow99 said in QObject and parent - two questions:

          Have I set parent like this:
          object.setParent(someObject);
          ?

          According to the docs and the function name, yes.

          And it's completely unreleated if the children are pointers or not. QObjects should not created on the stack or as members unless you really know what you're doing.

          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
          • T TomNow99
            19 Jun 2023, 17:55

            @Christian-Ehrlicher Thank you for answer.

            Question 1
            I see here 2 options: I have a class member, which isn't pointer, class member, which is pointer.

            1a) pointer:
            I have to set parent. When I not set parent, it will be wrong. Of course when I set parent and move object to other thread, the children are moved automaitcally. But first I have to tell QT "this is the child of this object".
            1b) not pointer:
            And here I don't know. Have I set parent like this:
            object.setParent(someObject);
            ?

            J Offline
            J Offline
            jeremy_k
            wrote on 19 Jun 2023, 19:00 last edited by
            #5

            @TomNow99 said in QObject and parent - two questions:

            @Christian-Ehrlicher Thank you for answer.

            1b) not pointer:
            And here I don't know. Have I set parent like this:
            object.setParent(someObject);
            ?

            A QObject instance deletes its children on destruction. If the stack allocated child isn't reparented or deallocated prior to that point, the program will likely crash.

            #include <QCoreApplication>
            #include <QObject>
            
            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
                QObject *heap = new QObject;
                QObject stack;
                stack.setParent(heap);
                printf("about to crash?\n");
                delete heap;
                printf("are we still here?\n");
                return 0;
            }
            

            Asking a question about code? http://eel.is/iso-c++/testcase/

            1 Reply Last reply
            0

            1/5

            19 Jun 2023, 14:52

            • Login

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