• SqlTableModel 的使用


    同时生效到数据库的修改

    实际使用需要将QSqlTableModel 作为基类,重写某些方法,本地数据库的修改,作为设备的备份数据库,保留每次修改的数据库副本,也方便设备数据库的回滚操作

    插入 和 删除 对于特定设备的参数,是不允许的,所以不需要考虑,这两个功能,当然维护数据库的小工具,可以添加

    嵌入式现在对设备的数据可视化,也有一定的要求,方便数据统计,和分析,通信行业,亦是如此

    Pyqt 和 QT 唯一区别在于 ,实时性的要求不一样,一般的问题分析和产品管理界面,Pyqt 完全够用,小白必备

    pyqt 也是对C++ QT 的功能封装,还没有,指针的考虑,多舒服

    # This Python file uses the following encoding: utf-8
    import sys
    
    from PySide6.QtWidgets import QApplication, QWidget
    
    # Important:
    # You need to run the following command to generate the ui_form.py file
    #     pyside6-uic form.ui -o ui_form.py, or
    #     pyside2-uic form.ui -o ui_form.py
    from form_ui import Ui_Widget
    
    from PySide6.QtCore import *
    from PySide6.QtWidgets import *
    from PySide6.QtSql import *
    from PySide6.QtGui import *
    
    class Widget(QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.ui = Ui_Widget()
            self.ui.setupUi(self)
            self.ui.Save.clicked.connect(self.Save)
            self.ui.Delete.clicked.connect(self.Delete)
            self.ui.Cancel.clicked.connect(self.Cancel)
            self.ui.Insert.clicked.connect(self.Insert)
    
            self.db=QSqlDatabase.addDatabase("QSQLITE")
            self.db.setDatabaseName("./drurmu.db")
            if self.db.open() is not True:
                print("open database error")
                return
            else:
                print("open database ok")
    
            self.tableModel=QSqlTableModel()
            self.tableModel.setEditStrategy(QSqlTableModel.EditStrategy.OnManualSubmit)
            self.tableModel.setTable("rmu")
            self.selModel=QItemSelectionModel(self.tableModel)
            self.selModel.currentChanged.connect(self.do_channged)
            self.selModel.currentRowChanged.connect(self.do_Row_Channged)
            
            if self.tableModel.select() :
                print("database querry successful")
            else:
                print("database querry failed")
                
            #Auto get filed name for ui tableView
            self.__getFiledNames()
            #QSqlTableModel need user define call functions
            self.ui.tableView.setModel(self.tableModel)
            
        def __getFiledNames(self):
            emptyRec=self.tableModel.record()
            self.fidNum={}
            for i in range(emptyRec.count()):
                self.fidNum[emptyRec.fieldName(i)]=i
            print(self.fidNum)
        
        def do_Row_Channged(self,current,preious):
            self.ui.Delete.setEnabled(current.isVaild())
            self.ui.Save.setEnabled(current.isVaild())
            
        def do_channged(self,current,preious):
            self.ui.Save.setEnabled(self.tableModel.isDirty())
            self.ui.Cancel.setEnabled(self.tableModel.isDirty())
        
        def Insert(self):
            curIndex=self.selModel.currentIndex()
            self.tableModel.insertRow(curIndex.row(),QModelIndex())
            self.selModel.clearSelection()
            self.selModel.setCurrentIndex(curIndex,QItemSelectionModel.SelectionFlag.Select)
    
        def Delete(self):
            curIndex=self.selModel.currentIndex()
            self.tableModel.removeRow(curIndex)
        
        def Cancel(self):
            self.tableModel.revertAll()
    
        def Save(self):
            if self.tableModel.submitAll():
                print("Commit all data successful")
            else:
                print("Commit all data erro")
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        widget = Widget()
        widget.show()
        sys.exit(app.exec())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
  • 相关阅读:
    ise使用ChipScope时报错NgdBuild:604
    js获取对象值得方式(在不确定对象参数的情况下获取值)
    Stm32_标准库_12_串口_发送数据
    【学生管理系统】权限管理之角色管理—查询所有角色并给角色授予权限(菜单)
    垃圾回收 - 标记压缩算法
    Scala的一等公民和至简原则
    程序员最容易读错的单词,听到status我炸了
    基于JavaSwing开发图书销售管理系统 课程设计 大作业 毕业设计
    yamot:一款功能强大的基于Web的服务器安全监控工具
    水电远程抄表:现代智能生活的创新实践
  • 原文地址:https://blog.csdn.net/weixin_45647912/article/details/133804835