在代码中定义 ListView
模板需要以下步骤:
from PyQt5.QtWidgets import QMainWindow, ListView
from PyQt5.QtWidgets import QTableWidget, QAbstractItemView
from PyQt5.QtWidgets import QTableView
import sys
import json
ListView
绑定:
class MyDataModel:
def __init__(self):
self.data = []
def append(self, name, description):
self.data.append({"id": len(self.data) + 1, "name": name, "description": description})
if name == 'main':
app = QApplication(sys.argv)
# 创建数据模型
data_model = MyDataModel()
data_model.append("item1", "Description for item 1")
data_model.append("item2", "Description for item 2")
data_model.append("item3", "Description for item 3")
view = MyListView()
# 将数据模型与列名绑定
view.setModel(data_model)
view.show()
sys.exit(app.exec_)
class MyListView(QTableView):
def init(self):
super().__init__()
# 设置自定义表样式
self.showGrid = True
self.verticalHeader().setDefaultSectionSize(40)
def setModel(self, model):
super(MyListView, self).setModel(model)
self.model.installEventFilter(self)
领取专属 10元无门槛券
手把手带您无忧上云