Issue
I have a QTreeView
of a QStandardItemModel
. I am painting/editing the data using a custom delegate. Within createEditor
method, I use parent.window()
to access the main window of the entire application (see below to link to some code from another question).
Question: what is the parent of createEditor
in the delegate? It is defined with the following parameters:
def createEditor(self, parent, option, index)
What is confusing is when QStyledItemDelegate
is initialized, and I print type(parent)
for that, I get the tree back (the tree that I have made this delegate to display). This is what I expect. However, when I do the same and print type(parent)
within createEditor
method implementation, it just returns QWidget
. I get the same when I run parent.metaObject().className()
which is a suggestion I got from here:
QT : get the class name of an object
When I try to pull attributes that I've defined in the tree view, such as parent.rootItem
, I get an attribute error. So, what is going on here? What is the parent of my editor?
I don't find much help from the PyQt documentation:
The parent argument, if not None, causes self to be owned by Qt instead of PyQt. Reimplemented from QAbstractItemDelegate.createEditor(). Returns the widget used to edit the item specified by index for editing. The parent widget and style option are used to control how the editor widget appears
Note this was all started by a solution to a different problem discussed in the comments to the solution here:
https://stackoverflow.com/a/32928091/1886357
Solution
The parent is the viewport widget of the view that is using the delegate. The viewport is part of the scrollarea that is inherited by the view.
So in your particular example:
def createEditor(self, parent, option, index):
print(parent is parent.window().tree.viewport()) # True
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.