如何在QtableView pyqt中禁用某些列?

时间:2014-04-05 21:15:38

标签: python pyqt qtableview

我想填写tableView但我想禁用某些列,因此用户无权修改其内容。

def remplissageTableView(self):

        headers=["Janvier", "fevrier","Mars","Avril","Mai","Juin","Juillet", "Aout","Septembre","Octobre","Novembre","Decembre"]
        rows=[]
        for i in range(5) :
            row = ["","","","","","","","","","","",""]
            rows.append(row)
        model =PrevisionTableModel(rows,headers) 
        self.tableView.setModel(model)

我最初想要一个tableView 12列5行是一个有点愚蠢的解决方案:p

1 个答案:

答案 0 :(得分:2)

使用proxy model来控制桌子型号上的flags

class ProxyModel(QtGui.QIdentityProxyModel):
    def __init__(self, parent=None):
        super(ProxyModel, self).__init__(parent)
        self._columns = set()

    def columnReadOnly(self, column):
        return column in self._columns

    def setColumnReadOnly(self, column, readonly=True):
        if readonly:
            self._columns.add(column)
        else:
            self._columns.discard(column)

    def flags(self, index):
        flags = super(ProxyModel, self).flags(index)
        if self.columnReadOnly(index.column()):
            flags &= ~QtCore.Qt.ItemIsEditable
        return flags

...
model = PrevisionTableModel(rows, headers) 
self.proxy = ProxyModel(self)
self.proxy.setSourceModel(model)
self.tableView.setModel(self.proxy)
self.tableView.model().setColumnReadOnly(3, True)