QStyledItemDelegate (PySide/PyQt/Qt) 中 createEditor 的父级是什么? [英] What is the parent of createEditor in a QStyledItemDelegate (PySide/PyQt/Qt)?

查看:104
本文介绍了QStyledItemDelegate (PySide/PyQt/Qt) 中 createEditor 的父级是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QStandardItemModelQTreeView.我正在使用自定义委托绘制/编辑数据.在 createEditor 方法中,我使用 parent.window() 访问整个应用程序的主窗口(请参阅下面链接到另一个问题的一些代码).

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).

问题:委托中 createEditor 的父级是什么?它使用以下参数定义:

Question: what is the parent of createEditor in the delegate? It is defined with the following parameters:

def createEditor(self, parent, option, index)

令人困惑的是,当 QStyledItemDelegate 被初始化,并且我为 那个 打印 type(parent) 时,我得到了树(我让这个委托显示的树).这是我所期望的.但是,当我执行相同操作并在 createEditor 方法实现中打印 type(parent) 时,它只返回 QWidget.当我运行 parent.metaObject().className() 时,我得到了同样的结果,这是我从这里得到的建议:

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:获取对象的类名

当我尝试提取已在树视图中定义的属性(例如 parent.rootItem)时,出现属性错误.那么,这里发生了什么?我的编辑器的父级是什么?

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?

我在 PyQt 文档中找不到太多帮助:

I don't find much help from the PyQt documentation:

parent 参数,如果不是 None,导致 self 归 Qt 所有而不是 PyQt.重新实现QAbstractItemDelegate.createEditor().返回用于编辑的小部件index 指定的项目进行编辑.父小部件和样式选项用于控制编辑器小部件的显示方式

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

推荐答案

父级是

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

这篇关于QStyledItemDelegate (PySide/PyQt/Qt) 中 createEditor 的父级是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆