【问题标题】:Custom ItemDelegate for QListView in PySide: Items invisiblePySide中QListView的自定义ItemDelegate:项目不可见
【发布时间】:2017-03-10 15:50:30
【问题描述】:

问题:

为什么this 的 Python 等效项回答

class CustomDelegate(QtGui.QStyledItemDelegate):
    def paint(self, painter, option, index):
        opt = QtGui.QStyleOptionViewItem(option)
        self.initStyleOption(opt, index)
        opt.text = "Test Text"
        QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_ItemViewItem, opt, painter)

不要在我的列表视图中绘制任何内容,而我可以通过传递正确的 QStyleOption 包括必要的成员和 text-member 设置为所需的文本来绘制具有任意标签的各种其他小部件,如下所示:

class CustomDelegate(QtGui.QStyledItemDelegate):
    def paint(self, painter, option, index):
        opt = QtGui.QStyleOptionButton()
        opt.rect = option.rect
        opt.text = "Test Text"
        QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_PushButton, opt, painter)

问题背景:

在 PySide 中,我将 QFileSystemModel 应用于 QListView,并希望显示不带文件扩展名的文件名。
我的计划是应用一个继承QStyledItemDelegateCustomDelegate 并在paint() 函数中更改QStyleOptionViewItem 的文本成员,正如您在上面的第一个代码示例中看到的那样。唯一的区别:"Test Text"os.path.splitext(index.data())[0] 替换。

虽然项目被插入到列表视图中,(我可以通过出现的滚动条和单击列表中的任意位置并打印活动项目来判断)项目根本没有绘制并且保持不可见。

如果我不尝试更改任何内容并传递原始的option-parameter,也会发生同样的情况:

class CustomDelegate(QtGui.QStyledItemDelegate):
    def paint(self, painter, option, index):
        QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_ItemViewItem, option, painter)

更多信息:

如果我只是调用super-paint()-函数,项目正常显示:

class CustomDelegate(QtGui.QStyledItemDelegate):
    def paint(self, painter, option, index):
        QtGui.QStyledItemDelegate.paint(self, painter, option, index)

这让我产生了将自己的opt 传递给超级paint() 的想法:

class CustomDelegate(QtGui.QStyledItemDelegate):
    def paint(self, painter, option, index):
        opt = QtGui.QStyleOptionViewItem(option)
        self.initStyleOption(opt, index)
        opt.text = os.path.splitext(index.data())[0]
        print(opt.text)
        QtGui.QStyledItemDelegate.paint(self, painter, opt, index)

但这也会显示文件名带有扩展名...尽管print()将名称放在控制台不带扩展名。

奇怪的是,尝试打印opt.text 之前将它设置为任何东西让我:

AttributeError: 'PySide.QtGui.QStyleOptionViewItem' object has no attribute 'text'

最后:省略initStyleOption()-call 似乎对任何配置都没有任何影响。

【问题讨论】:

    标签: python-3.x pyside qlistview qstyleditemdelegate


    【解决方案1】:

    我终于找到了实现目标的方法。我仍然不知道为什么我的CustomDelegate 表现得如此奇怪和不合逻辑,但我意识到我可以在更早的阶段解决我的问题并实现自定义QFileSystemModel 以分配给我的QListView

    class CustomFileSystemModel(QtGui.QFileSystemModel):
        def data(self, index, role):
            if role == QtCore.Qt.DisplayRole:
                return os.path.splitext(QtGui.QFileSystemModel.data(self, index, role))[0]
            else:
                return QtGui.QFileSystemModel.data(self, index, role)
    

    这样我就可以删除我的CustomDelegate 并且默认分配给QListView 的标准QStyledItemDelegate 会传递已经缩短的文件名。我认为这是一种更优雅的方式,但不知道这个CustomDelegate 发生了什么仍然令人不满意。因此,如果有人找到了所有这些问题的真实答案,我将非常感谢她/他分享它!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多