Python PyqT6 QSqlTableModel

116 Views Asked by At

I trying to build my QSTableModel. I need display data from DB along with checkbox in every cell.

Here is my code

class MyModel(QSqlTableModel):
    def __init__(self, parent=None, db=QSqlDatabase()):
        super(MyModel, self).__init__(parent, db)
        
    def columnCount(self, index):
        count = QSqlTableModel.columnCount(self, index)
        print("column count - ", QSqlTableModel.columnCount(self, index))
        return count
    def rowCount(self, parent=None):
        print("row count - ", QSqlTableModel.rowCount(self))
        return QSqlTableModel.rowCount(self)
    

    def data(self, index, role=Qt.ItemDataRole.DisplayRole):
        row = index.row()
        col = index.column()

        if role == Qt.ItemDataRole.DisplayRole:
            if row == 0 and col == 0:
                return "<--left"
            if row == 1 and col == 1:
                return "right-->"
            return "SOMETHING"

This IF gives me checkboxes in every cell.

        if role == Qt.ItemDataRole.CheckStateRole:
            return Qt.CheckState.Checked

If I tell what to display something explicitly in the cell its working OK. But I want to display data from database. What should replace "SOMETHING" ?

0

There are 0 best solutions below