如何解决在QAbstractTableModel
我试图构建一个简单的QAbstractTableModel来使自己熟悉这些概念。
但是,我建立的模型并不符合我的预期。我希望模型在单元格中的“状态”值为True时显示刻度线。
条件if role == Qt.DecorationRole
从不成立。因此,我的问题是:在哪里设置角色标志或如何确保设置Qt.DecorationRole?
最小代码如下:
import sys
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtCore import Qt
import pandas as pd
class TableModel(QtCore.QAbstractTableModel):
def __init__(self,data):
super(TableModel,self).__init__()
self._data = data
def data(self,index,role):
if role == Qt.DisplayRole:
status,value = self._data.iloc[index.row(),index.column()]
return str(value)
if role == Qt.DecorationRole:
status,index.column()]
if status:
return QtGui.QIcon('tick.png')
def rowCount(self,index):
return self._data.shape[0]
def columnCount(self,index):
return self._data.shape[1]
def headerData(self,section,orientation,role):
# section is the index of the column/row.
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return str(self._data.columns[section])
if orientation == Qt.Vertical:
return str(self._data.index[section])
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.table = QtWidgets.QTableView()
data = pd.DataFrame([
[(False,1),(True,9)],[(True,(False,0)],],columns=['A','B'],index=['Row 1','Row 2'])
self.model = TableModel(data)
self.table.setModel(self.model)
self.setCentralWidget(self.table)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。