【发布时间】:2021-05-22 01:34:25
【问题描述】:
我正试图围绕 PyQT5 上的一些 ModelView。
我有一个 QListView,它可以显示存储在 QAbstractListModel 中的数据。
但我希望 QListView 的每一行都显示在 QDesigner 中创建的复杂 Widget。
我创建了一个带有 QLabel、间隔和 QPushButton 的小部件。
我想让我的 QListView 的每个元素都使用这个小部件来显示我的模型数据
这是一个简单的ModelView QListView的基本代码
import typing
from PyQt5 import QtCore
from PyQt5.QtCore import QModelIndex, Qt
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QListView
class MyListModel(QtCore.QAbstractListModel):
def __init__(self, parent=None):
super().__init__(parent)
self.data_list = []
def data(self, index: QModelIndex, role: int = ...) -> typing.Any:
if role == Qt.DisplayRole:
return self.data_list[index.row()]
def rowCount(self, parent: QModelIndex = ...) -> int:
return len(self.data_list)
data = ["emotion", "unliving", "brutally", "torch", "donut", "comet"]
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
layout = QWidget()
layout.setLayout(QVBoxLayout())
list_view = QListView()
model = MyListModel()
model.data_list = data
list_view.setModel(model)
layout.layout().addWidget(list_view)
layout.resize(640, 480)
layout.show()
sys.exit(app.exec_())
我尝试创建一个 QStyledItemDelegate,并在模型上设置委托,但我无法使其工作。
class LineDelegate(QStyledItemDelegate):
def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent)
self.line = Ui_Form()
self.line.setupUi(parent)
def paint(self, painter: QtGui.QPainter, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex) -> None:
# Not sure what to put here
文档似乎说应该使用委托来编辑视图内的数据,而不是复杂的小部件视图。
https://doc.qt.io/qtforpython/overviews/model-view-programming.html#a-simple-delegate
有什么可以得到我想要的吗?
我真的不知道如何处理这个。
【问题讨论】:
-
通常情况下,使用委托是首选。您尝试过哪些无法“使其发挥作用”的方法?
-
我尝试在委托中实例化小部件,但关于绘制方法,我不确定如何做到这一点“好方法”。我已经在主帖中添加了代码。出于性能考虑,QT 不建议在绘制方法中实例化