可编辑QTableView中的Pandas df:删除复选框 [英] Pandas df in editable QTableView: remove check boxes
问题描述
我有一个熊猫数据框,我想在QtableView中展示它并使之可编辑.我创建了以下模型,但是由于某种原因,输出在每个字段中都有复选框.我该如何摆脱它们?
I have a pandas dataframe that I would like to present in a QtableView and make it editable. I have create the below model, but for some reason the output has checkboxes in every field. How can I get rid of them?
出局看起来像这样:
The outout looks like this:
这是用于制作qtavleview中显示的熊猫数据框并使其可编辑的模型(我正在使用PySide)
And this this is the model that is used to make the pandas dataframe shown in a qtavleview and make it editable (I'm using PySide)
class PandasModelEditable(QtCore.QAbstractTableModel):
def __init__(self, data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self._data = data
def rowCount(self, parent=None):
return len(self._data.values)
def columnCount(self, parent=None):
return self._data.columns.size
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
return unicode(self._data.iloc[index.row(), index.column()])
return unicode()
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
if role != QtCore.Qt.DisplayRole:
return None
if orientation == QtCore.Qt.Horizontal:
try:
return '%s' % unicode(self._data.columns.tolist()[section])
except (IndexError,):
return unicode()
elif orientation == QtCore.Qt.Vertical:
try:
return '%s' % unicode(self._data.index.tolist()[section])
except (IndexError,):
return unicode()
def flags(self, index):
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | \
QtCore.Qt.ItemIsEditable
def setData(self, index, value, role=QtCore.Qt.EditRole):
if index.isValid():
self._data.iloc[index.row(), index.column()] = value
if self.data(index, QtCore.Qt.DisplayRole) == value:
self.dataChanged.emit(index, index)
return True
return unicode()
删除QtCore.Qt.ItemIsSelectable
并不能解决问题,因为它似乎没有效果.
Removing QtCore.Qt.ItemIsSelectable
does not solve the problem as it doesn't seem to have any effet.
推荐答案
您从data
和setaData
返回错误的默认值.前者应返回None
(因此您可以删除最后一行),而后者应返回False
.
You are returning the wrong default values from data
and setaData
. The former should return None
(so you could just remove the last line), whilst the latter should return False
.
这篇关于可编辑QTableView中的Pandas df:删除复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!