gpt4 book ai didi

python - QStyledItemDelegate 绘制刷新问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:04:55 29 4
gpt4 key购买 nike

我目前正在尝试研究模型 View 方法并编写缩略图查看器应用程序。

在此示例中,我只是尝试绘制 20 个框,但我得到的似乎是随机选择,该选择会随着鼠标移动而更新。滚动使事情变得更糟,并且绘制有时只有框等。而且文本似乎根本没有渲染。

对我来说,这看起来像是paint方法的刷新/更新问题。我在这里遗漏了什么吗?

paint problem

最终我需要显示很多项目。所有这些都包含一些标签和一个需要在运行时更新的像素图。所以我认为画它们将是最快的解决方案。或者,我尝试使用 Paint 方法渲染我的小部件:

customWidget.render(painter, QtCore.QPoint(0,0), renderFlags=QtGui.QWidget.DrawChildren)

这似乎更慢,但至少可以工作。

这是一个最小的工作示例来说明我的问题:

import sys
from PySide import QtCore
from PySide import QtGui

elements = range(20)

class ElementListModel(QtCore.QAbstractListModel):
def __init__(self, elements = [], parent = None):
super(ElementListModel, self).__init__()
self.__elements = elements

def rowCount(self, parent):
return len(self.__elements)

def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
return str(self.__elements[index.row()])

class ElementThumbDelegate(QtGui.QStyledItemDelegate):
def __init__(self, view, parent=None):
super(ElementThumbDelegate, self).__init__(parent)

def paint(self, painter, options, index):

width = options.rect.width()
height = options.rect.height()

painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(QtGui.QColor(255, 255, 255))
painter.setBrush(QtGui.QColor(10, 10, 10))
painter.drawRect(0, 0, width, height)
painter.drawText(options.rect, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter, str(index.data()))
painter.translate(options.rect.topLeft())

def sizeHint(self, options, index):
return QtCore.QSize(50, 50)

def main():
app = QtGui.QApplication(sys.argv)
viewer = QtGui.QListView()

viewModel = ElementListModel(elements)
viewer.setModel(viewModel)
viewer.setViewMode(QtGui.QListView.IconMode)
viewer.setItemDelegate(ElementThumbDelegate(viewer))

viewer.show()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

提前致谢。任何帮助或下一步寻找方向的指示将不胜感激!

最佳答案

事实证明,painter.translate() 与在查看器原点 (0,0) 处绘制框相结合,与自动定位发生冲突并导致了问题。

委托(delegate)到适当位置的转换由 View 本身处理 - 无需手动执行。

工作代码:

import sys
from PySide import QtCore
from PySide import QtGui

elements = range(20)

class ElementListModel(QtCore.QAbstractListModel):
def __init__(self, elements = [], parent = None):
super(ElementListModel, self).__init__()
self.__elements = elements

def rowCount(self, parent):
return len(self.__elements)

def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
return str(self.__elements[index.row()])

class ElementThumbDelegate(QtGui.QStyledItemDelegate):
def __init__(self, view, parent=None):
super(ElementThumbDelegate, self).__init__(parent)

def paint(self, painter, options, index):
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(QtGui.QColor(255, 255, 255))
painter.setBrush(QtGui.QColor(10, 10, 10))
painter.drawRect(options.rect)
painter.drawText(options.rect, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter, str(index.data()))

def sizeHint(self, options, index):
return QtCore.QSize(50, 50)

def main():
app = QtGui.QApplication(sys.argv)
viewer = QtGui.QListView()

viewModel = ElementListModel(elements)
viewer.setModel(viewModel)
viewer.setViewMode(QtGui.QListView.IconMode)
viewer.setItemDelegate(ElementThumbDelegate(viewer))

viewer.show()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

关于python - QStyledItemDelegate 绘制刷新问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39933079/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com