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())
相关推荐
用户31187945592181 天前
Kylin Linux 10 安装 glib2-devel-2.62.5-7.ky10.x86_64.rpm 方法(附安装包)
linux
涛啊涛1 天前
Centos7非LVM根分区容量不足后扩容,对调硬盘挂载/
linux·磁盘管理
CYRUS_STUDIO2 天前
用 Frida 控制 Android 线程:kill 命令、挂起与恢复全解析
android·linux·逆向
熊猫李2 天前
rootfs-根文件系统详解
linux
dessler2 天前
Hadoop HDFS-高可用集群部署
linux·运维·hdfs
泽泽爱旅行2 天前
awk 语法解析-前端学习
linux·前端
轻松Ai享生活3 天前
5 节课深入学习Linux Cgroups
linux
christine-rr3 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神5553 天前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆3 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs