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())
相关推荐
AlfredZhao6 小时前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户97183563346612 小时前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪13 小时前
linux 拷贝文件或目录到指定的位置
linux
摇滚侠1 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
bush41 天前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5201 天前
Linux 11 动态监控指令top
linux
不会C语言的男孩1 天前
Linux 系统编程 · 第 8 章:进程基础
linux·c语言
古城小栈1 天前
Unix 与 Linux 异同小叙
linux·服务器·unix
凡人叶枫1 天前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
2601_961875241 天前
决战申论100题2026|最新|范文
linux·容器·centos·debian·ssh·fabric·vagrant