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())

你可能感兴趣的:(python3,linux)