可编辑 QTableView 中的 Pandas df:删除复选框 [英] Pandas df in editable QTableView: remove check boxes

查看:51
本文介绍了可编辑 QTableView 中的 Pandas df:删除复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Pandas 数据框,我想在 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?

输出看起来像这样:

这是用于制作在 qtavleview 中显示的 Pandas 数据框并使其可编辑的模型(我正在使用 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.

推荐答案

您从 datasetaData 返回了错误的默认值.前者应该返回 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆