Getting the parent from QGraphicsScene?
-
Hello!
I've googled for this, read the docs, and asked ChatGPT, so I think there might not be a direct way. However, I'll ask just in case: Is there a way that I can get the "parent" that's passed in to a QGraphicsScene?
I was just considering inheriting from QGraphicsScene to store the parent when I realized that it already takes a parent as an optional constructor. If I could access that, I won't need to create an additional class.
Thanks!
-
Hello!
I've googled for this, read the docs, and asked ChatGPT, so I think there might not be a direct way. However, I'll ask just in case: Is there a way that I can get the "parent" that's passed in to a QGraphicsScene?
I was just considering inheriting from QGraphicsScene to store the parent when I realized that it already takes a parent as an optional constructor. If I could access that, I won't need to create an additional class.
Thanks!
-
@Clone45 QGraphicsScene is a QObject, so just call QObject::parent()... or have I missed something.
Yup, like Chris said, just call parent().
In general, the documentation is way more useful than a chatbot: https://doc.qt.io/qt-6/qgraphicsscene-members.html#:~:text=parent() const %3A QObject *
-
Hello!
I've googled for this, read the docs, and asked ChatGPT, so I think there might not be a direct way. However, I'll ask just in case: Is there a way that I can get the "parent" that's passed in to a QGraphicsScene?
I was just considering inheriting from QGraphicsScene to store the parent when I realized that it already takes a parent as an optional constructor. If I could access that, I won't need to create an additional class.
Thanks!
@Clone45
As both above have said. It's aQObject
, which optionally takes aQObject *parent
constructor argument which can be accessed viaparent()
method thereafter.However, just as a tip, why do you want/need to access this parent? It's even optional, so could be null. There are always reasons/exceptions, but by and large
QObject
s really should not know about or need to access their parent. Only parents access children. Food for thought. -
@Clone45
As both above have said. It's aQObject
, which optionally takes aQObject *parent
constructor argument which can be accessed viaparent()
method thereafter.However, just as a tip, why do you want/need to access this parent? It's even optional, so could be null. There are always reasons/exceptions, but by and large
QObject
s really should not know about or need to access their parent. Only parents access children. Food for thought.@JonB said in Getting the parent from QGraphicsScene?:
However, just as a tip, why do you want/need to access this parent? It's even optional, so could be null. There are always reasons/exceptions, but by and large QObjects really should not know about or need to access their parent. Only parents access children. Food for thought.
Thanks for the insight. I'll keep that in mind. It would take a while to explain what I'm doing, and unfortunately I have to run off to work, but if I post again about this, I'll follow up with additional details. Thanks!!
-
@JonB said in Getting the parent from QGraphicsScene?:
However, just as a tip, why do you want/need to access this parent? It's even optional, so could be null. There are always reasons/exceptions, but by and large QObjects really should not know about or need to access their parent. Only parents access children. Food for thought.
Thanks for the insight. I'll keep that in mind. It would take a while to explain what I'm doing, and unfortunately I have to run off to work, but if I post again about this, I'll follow up with additional details. Thanks!!
-
@Clone45
You can always subclassQGraphicsScene
to add your own explicit pointer to some specialized class with relevant stuff in it. Just it would be best if it does not rely on itsQObject
s parent's characteristics too much.@JonB said in Getting the parent from QGraphicsScene?:
@Clone45
You can always subclass QGraphicsScene to add your own explicit pointer to some specialized class with relevant stuff in it. Just it would be best if it does not rely on its QObjects parent's characteristics too much.Thank you. I think that I'm leaning that the hard way tonight. I'll go ahead and subclass QGraphicsScene and manage the relationship myself.