首先是pyside自定义模型
class pandasModel(QAbstractTableModel):
def __init__(self, data):
QAbstractTableModel.__init__(self)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parnet=None):
return self._data.shape[1]
def data(self, index, role=Qt.DisplayRole):
if index.isValid():
if role == Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
return None
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[col]
return None
槽函数这里这么写
返回值为QAbstractItemModel
因为tableview无法接收QAbstractTableModel
不过QAbstractTableModel继承自QAbstractItemModel
@Slot(str,result=QAbstractItemModel)
def getfilepath(self,filepath):
self.datafilepath = filepath
df = ppdd.readdatafile(filepath)
self.pandasmodel = pandasModel(df)
return self.pandasmodel
QML创建一个Rectangle,里面放TableView,要不然不显示
implicitWidth: 100
implicitHeight: 30
也必须定义,不然不显示
(这个地方应该有其他更好的方案,不能光写TableView)
Rectangle {
width: 400
height: 400
color: "transparent"
TableView {
id:table_view
anchors.fill: parent
columnSpacing: 1
rowSpacing: 1
clip: true
model: null
delegate: Rectangle {
implicitWidth: 100
implicitHeight: 30
Text {
text: display
}
}
}
然后用槽函数给他model定义一下就好了,TableView是实时刷新的