Pyqt Model View 的使用方法

class CheckBoxDeleta(QAbstractItemDelegate):
    def __init__(self, parent: QObject | None = ...) -> None:
        super().__init__(parent)
    
    def createEditor(self, parent: QWidget, option: QStyleOptionViewItem, index: QModelIndex | QPersistentModelIndex) -> QWidget:
        return super().createEditor(parent, option, index)
    
    def setEditorData(self, editor: QWidget, index: QModelIndex | QPersistentModelIndex) -> None:
        return super().setEditorData(editor, index)
    
    def setModelData(self, editor: QWidget, model: QAbstractItemModel, index: QModelIndex | QPersistentModelIndex) -> None:
        return super().setModelData(editor, model, index)
    
    def updateEditorGeometry(self, editor: QWidget, option: QStyleOptionViewItem, index: QModelIndex | QPersistentModelIndex) -> None:
        return super().updateEditorGeometry(editor, option, index)

class TableModel(QAbstractTableModel):
    def __init__(self, parent: QObject | None = ...) -> None:
        super(TableModel,self).__init__()
    
    # paint row
    def rowCount(self, parent: QModelIndex | QPersistentModelIndex = ...) -> int:
        return 12
    
    # paint col
    def columnCount(self, parent: QModelIndex | QPersistentModelIndex = ...) -> int:
        return 12
    
    # Data to View Change and Save data
    def setData(self, index: QModelIndex | QPersistentModelIndex, value: Any, role: int = ...) -> bool:
        print(f"row:{index.row()} col:{index.column()} value:{value} ")
        return True

    # Init Data
    def data(self, index: QModelIndex | QPersistentModelIndex, role: int = ...) -> Any:
        return 12

    def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...) -> Any:
        return super().headerData(section, orientation, role)
    
    #Table Can Selected and Checked
    def flags(self, index: QModelIndex | QPersistentModelIndex) -> Qt.ItemFlag:
        return Qt.ItemFlag.ItemIsEditable | Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEnabled

#TODO:User define functions
class QSqlTableModelV2(QSqlTableModel):
    __changed__={}
    def __init__(self, parent, db) -> None:
        super().__init__(parent, db)
    
    def changedList(self)->dict:
        return self.__changed__
    
    def setRemoteData(self):
        pass

    def setData(self, index: QModelIndex | QPersistentModelIndex, value: Any, role: int = ...) -> bool:
        self.__changed__[self.data(index.sibling(index.row(),0),Qt.ItemDataRole.DisplayRole)]=value
        return super().setData(index, value, role) #Commit to database
 
    #Set data
    def data(self, idx: QModelIndex | QPersistentModelIndex, role: int = ...) -> Any:
        print("data row:%d col:%d"%(idx.row(),idx.column()))
        return super().data(idx, role)

    def submitAll(self) -> bool:
        return super().submitAll()
    
    def submit(self) -> bool:
        self.__changed__.clear()
        return super().submit()
    
    def revertAll(self) -> None:
        self.__changed__.clear()
        return super().revertAll()

全是嵌套的callback 的实现方法,层级分离,通过类的继承,拓展基础类的功能,通用性不错,省了自己在,调用原始方法,构建view 和 控制数据库,再将数据同步到板卡,利用现成的基础类,自己在拓展一个功能就了

C 没有继承和多态,一言难尽,Python 的继承和多态应该是最容易理解的

你可能感兴趣的:(pyqt,数据库,python)