我目前正试图围绕模型 - 视图方法,编写缩略图查看器应用程序。
在这个例子中,我只是尝试绘制20个盒子,但是我得到了一个随机选择,它可以更新鼠标移动。滚动会使事情变得更糟,有时只会绘制框等。而且文本似乎根本没有呈现。
对我而言,它看起来像paint方法的刷新/更新问题。 我在这里错过了什么吗?
最终我需要展示很多项目。全部包含一些需要在运行时更新的标签和像素图。所以我认为画它们是最快的解决方案。或者,我尝试使用:
在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()
提前致谢。任何有关下一步的帮助或指示都将受到高度赞赏!
答案 0 :(得分:1)
事实证明,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()
与在观察者原点(0,0)绘制方框相结合,正在对抗自动定位并导致问题。
将代理转换为适当的位置由视图本身处理 - 无需手动执行。
工作代码:
{{1}}