What is the parent of createEditor in a QStyledItemDelegate (PySide/PyQt/Qt)?

橙三吉。 提交于 2019-12-21 21:19:45

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/32938507/what-is-the-parent-of-createeditor-in-a-qstyleditemdelegate-pyside-pyqt-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!