PyQt QAbstractTableModel复选框不可检查 [英] PyQt QAbstractTableModel checkbox not checkable

查看:191
本文介绍了PyQt QAbstractTableModel复选框不可检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将自己的表模型与QAbstractTableModel配合使用,其中我首先使用了复选框(可检查原因标志Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled).我在尝试使用复选框时遇到麻烦,因为它们不可检查(无法选中或取消选中他们)在显示的表格中.

I am using own table model with QAbstractTableModel, where I have first col with checkbox (checkable cause flags Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled). I have trouble when I am trying use checkboxes, cause they are not checkable (can not make check or uncheck in them) in showed table.

我做错了什么?我在自己的表模型类中使用此方法:

What am I doing wrong? I am using this methods in own table model class:

def data(self, index, role):

    row = index.row()
    col = index.column()

    if role == Qt.DisplayRole:
        return '{0}'.format(self.tableData[row][col])

    if role == Qt.CheckStateRole: 
        if col == 0:
            return Qt.Unchecked
        else: 
            return None

def setData(self, index, value, role):
    if not index.isValid():
       return False

    if (role == Qt.CheckStateRole):
        if (index.data(Qt.CheckStateRole) == Qt.Checked):
            return True
        else:
            return False
    else:
        return False

推荐答案

您必须为其存储状态,我们需要有一个永久性的引用作为参考,为此我们使用QPersistentModelIndex,在这种情况下为字典,其中密钥是QPersistentModelIndex,值是QCheckBox的状态.

You have to store the states for it, we need to have something permanent as a reference, for this we use QPersistentModelIndex, in this case a dictionary where the key is the QPersistentModelIndex and the value is the state of QCheckBox.

from PyQt4.QtGui import *
from PyQt4.QtCore import *


class TableModel(QAbstractTableModel):
    def __init__(self, parent=None):
        super(TableModel, self).__init__(parent)
        self.tableData = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
        self.checks = {}

    def columnCount(self, *args):
        return 3

    def rowCount(self, *args):
        return 3

    def checkState(self, index):
        if index in self.checks.keys():
            return self.checks[index]
        else:
            return Qt.Unchecked

    def data(self, index, role=Qt.DisplayRole):
        row = index.row()
        col = index.column()
        if role == Qt.DisplayRole:
            return '{0}'.format(self.tableData[row][col])
        elif role == Qt.CheckStateRole and col == 0:
            return self.checkState(QPersistentModelIndex(index))
        return None

    def setData(self, index, value, role=Qt.EditRole):

        if not index.isValid():
            return False
        if role == Qt.CheckStateRole:
            self.checks[QPersistentModelIndex(index)] = value
            return True
        return False

    def flags(self, index):
        fl = QAbstractTableModel.flags(self, index)
        if index.column() == 0:
            fl |= Qt.ItemIsEditable | Qt.ItemIsUserCheckable
        return fl


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    view = QTableView()
    model = TableModel()
    view.setModel(model)
    view.show()
    sys.exit(app.exec_())

这篇关于PyQt QAbstractTableModel复选框不可检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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