我正在編寫一個應用程序,它使用自定義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