QObject and parent - two questions
-
wrote on 19 Jun 2023, 14:52 last edited by TomNow99
Hi,
I have two questions about parent in QT:
- 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 variableQProcess
andQProcess
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?- 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
- I would like to use QThread in my application and I would like to move one object ( inherits QObject - class XYZ ) to other thread (
-
Hi,
I have two questions about parent in QT:
- 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 variableQProcess
andQProcess
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?- 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
@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.
- I would like to use QThread in my application and I would like to move one object ( inherits QObject - class XYZ ) to other thread (
-
@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.
wrote on 19 Jun 2023, 17:55 last edited by TomNow99@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);
? -
@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);
?Lifetime Qt Championwrote on 19 Jun 2023, 18:06 last edited by Christian Ehrlicher@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.
-
@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);
?wrote on 19 Jun 2023, 19:00 last edited by@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; }
1/5