我正在编写一个应用程序,它使用自定义QWidget代替PyQt中的常规列表项或代表。我遵循Render QWidget in paint() method of QWidgetDelegate for a QListView中的回答 - 其中包括 - 使用自定义小部件实现QTableModel。结果示例代码位于此问题的底部。执行中存在一些问题,我不知道如何解决:在QListView中平滑延迟加载和卸载自定义IndexWidget
- 卸载项目时,他们不显示。我打算为我的应用程序建立一个有数千个条目的列表,并且我无法在内存中保存那么多小部件。
- 加载尚未进入或至少异步加载的项目。小部件需要一些时间来渲染,下面的示例代码在滚动列表时有一些明显的滞后。
- 当在下面的实现中滚动列表时,加载时每个新加载的按钮出现在QListView的左上角一秒钟之后跳回到位置。这怎么可以避免?
-
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt
class TestListModel(QtCore.QAbstractListModel):
def __init__(self, parent=None):
QtCore.QAbstractListModel.__init__(self, parent)
self.list = parent
def rowCount(self, index):
return 1000
def data(self, index, role):
if role == Qt.DisplayRole:
if not self.list.indexWidget(index):
button = QtGui.QPushButton("This is item #%s" % index.row())
self.list.setIndexWidget(index, button)
return QtCore.QVariant()
if role == Qt.SizeHintRole:
return QtCore.QSize(100, 50)
def columnCount(self, index):
pass
def main():
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
list = QtGui.QListView()
model = TestListModel(list)
list.setModel(model)
list.setVerticalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
layout = QtGui.QVBoxLayout(window)
layout.addWidget(list)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
为了解决问题3,你试图给按钮一个父母在创作时?即,QListView。 – Lorenz03Tx 2013-03-20 18:15:09