SqlTableModel 的使用

同时生效到数据库的修改
实际使用需要将QSqlTableModel 作为基类,重写某些方法,本地数据库的修改,作为设备的备份数据库,保留每次修改的数据库副本,也方便设备数据库的回滚操作
插入 和 删除 对于特定设备的参数,是不允许的,所以不需要考虑,这两个功能,当然维护数据库的小工具,可以添加
嵌入式现在对设备的数据可视化,也有一定的要求,方便数据统计,和分析,通信行业,亦是如此
Pyqt 和 QT 唯一区别在于 ,实时性的要求不一样,一般的问题分析和产品管理界面,Pyqt 完全够用,小白必备
pyqt 也是对C++ QT 的功能封装,还没有,指针的考虑,多舒服

python 复制代码
# 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())
相关推荐
wdxylb1 小时前
云原生俱乐部-shell知识点归纳(1)
linux·云原生
飞雪20072 小时前
Alibaba Cloud Linux 3 在 Apple M 芯片 Mac 的 VMware Fusion 上部署的完整密码重置教程(二)
linux·macos·阿里云·vmware·虚拟机·aliyun·alibaba cloud
路溪非溪2 小时前
关于Linux内核中头文件问题相关总结
linux
Lovyk5 小时前
Linux 正则表达式
linux·运维
Fireworkitte6 小时前
Ubuntu、CentOS、AlmaLinux 9.5的 rc.local实现 开机启动
linux·ubuntu·centos
sword devil9006 小时前
ubuntu常见问题汇总
linux·ubuntu
ac.char6 小时前
在CentOS系统中查询已删除但仍占用磁盘空间的文件
linux·运维·centos
淮北也生橘128 小时前
Linux的ALSA音频框架学习笔记
linux·笔记·学习
华强笔记11 小时前
Linux内存管理系统性总结
linux·运维·网络
十五年专注C++开发12 小时前
CMake进阶: CMake Modules---简化CMake配置的利器
linux·c++·windows·cmake·自动化构建