Issue
Consider I have a class with this kind of structure (CustomClass
may/may not be on top of the hierarchy):
CustomClass
.. QTabWidget
.... QWidget
...... QTreeView
In QTreeView
I have a function that is trying to refer back to CustomClass
. Right now in order to do this I need to do: self.parent().parent().parent()
.
Although this works it just feels very sloppy, and if I ever need to change the structure this will fail. Is there some other way to get CustomClass
? Usually I would pass an instance of it during its constructor which I can call directly, but wondering what's the best practice to go about this.
Solution
This feels like a decent procedural way to get it:
customClassInst = self.parent()
while customClassInst is not None and type(customClassInst) != CustomClass:
customClassInst = customClassInst.parent()
Any other answers are still welcome :)
Answered By - Green Cell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.