如何在pyqt中更改QTableview的单元格数据时调用任何方法?

时间:2018-08-09 14:29:10

标签: python pyqt4

我使用了QTableView和QSqlRelationalTableModel在PyQt4中创建了一个表,如图1所示(下面给出了链接)。我的最后一列是行选择选项“是”或“否”。

我正在尝试禁用整个行,同时在图2所示的列中选择“否”。

在此处添加图像

enter image description here

代码如下:

ID, ROLL, NAME, MATHS, CHEMISTRY, BIOLOGY, PHYSICS, ELECTRONICS, CS, TOTAL, SELECT = range(11)

class Student(QDialog):

    def __init__(self, table, parent=None): # The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

        super(Student, self).__init__(parent)

        self.setObjectName("Table")
        self.setFixedSize(1066, 210)
        self.setWindowTitle("STUDENT DATA")

        self.model = StudentModel(self)
        self.model.setTable(table)
        self.model.setRelation(SELECT, QSqlRelation("STATE", "id", "selection"))
        self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
        self.model.select()

        self.view = QTableView(self)
        self.view.setGeometry(QRect(10, 10, 1048, 166))
        self.view.setSelectionBehavior(QAbstractItemView.SelectItems)
        self.view.setFocusPolicy(Qt.StrongFocus)
        self.view.setModel(self.model)
        self.view.setColumnHidden(ID, True)
        self.view.setColumnWidth(SELECT, 60)
        self.view.setStyleSheet(("font: 8pt \"Verdana\";" "gridline-color: rgb(0, 0, 0);"))
        style = "::section {""background-color: lightgrey; }"
        self.view.horizontalHeader().setStyleSheet(style)
        self.view.verticalHeader().setStyleSheet(style)

        QSqlDatabase.database().commit()

        self.buttonBox = QDialogButtonBox(self)
        self.buttonBox.setGeometry(QRect(420, 170, 200, 45))
        self.buttonBox.setOrientation(Qt.Horizontal)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.buttonBox.button(QDialogButtonBox.Ok).clicked.connect(self.accept)
        self.buttonBox.button(QDialogButtonBox.Cancel).clicked.connect(self.reject)

STD1, STD2, STD3 = range(3)

class StudentModel(QSqlRelationalTableModel):

    def __init__(self, parent=None):
        super(StudentModel, self).__init__(parent)

    def rowCount(self, index=QModelIndex()):
        return 3

    def columnCount(self, index=QModelIndex()):
        return 11

    def data(self, index, role):

        SqlData = super(StudentModel, self).data(index)

        if not index.isValid():
            return QVariant()

        if role == Qt.DisplayRole:
            return QVariant(SqlData)

        if (role == Qt.TextAlignmentRole and index.column()):
            return QVariant(int(Qt.AlignCenter|Qt.AlignVCenter))

        return QSqlTableModel.data(self, index, role)

    def headerData(self, section, orientation, role):

        if role == Qt.TextAlignmentRole:
            if orientation == Qt.Horizontal:
                return QVariant(int(Qt.AlignCenter))
            return QVariant(int(Qt.AlignRight|Qt.AlignVCenter))

        if role != Qt.DisplayRole:
            return QVariant()

        if orientation == Qt.Horizontal:
            if section == ID:
                return QVariant("ID")
            elif section == ROLL:
                return QVariant("Roll No.")
            elif section == NAME:
                return QVariant("Name")
            elif section == MATHS:
                return QVariant("Maths")
            elif section == CHEMISTRY:
                return QVariant("Chemistry")
            elif section == BIOLOGY:
                return QVariant("Biology")
            elif section == PHYSICS:
                return QVariant("Physics")
            elif section == ELECTRONICS:
                return QVariant("Electronics")
            elif section == CS:
                return QVariant("CS")
            elif section == TOTAL:
                return QVariant("Total Marks")
            elif section == SELECT:
                return QVariant("Selection")

            return QVariant(section)

        if orientation == Qt.Vertical:
            if section == STD1:
                return QVariant("1")
            elif section == STD2:
                return QVariant("2")
            elif section == STD3:
                return QVariant("3")

0 个答案:

没有答案