# -*- coding: utf-8 -*- ''' 【简介】 PyQt5中 处理database 例子 ''' import sys,time from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtSql import QSqlDatabase, QSqlTableModel from PyQt5.QtCore import Qt,pyqtSignal from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(QObject): single_del_row = pyqtSignal() def __init__(self,parent =None): super(Ui_Form,self).__init__(parent) #自定义信号 self.single_del_row.connect(self.show_message_del_row) db = QSqlDatabase.addDatabase('QSQLITE') # addDatabase设定链接数据库格式为SQLITE即sqlite3和以上的版本 db.setDatabaseName('../data_db/date_of_cj.db') # 设定链接数据库的名称 self.model = QSqlTableModel() self.delrow = 1 # 设置读取开始位置 ####################### #初始化model self.model.setTable('date_of_01cj') self.model.setEditStrategy(QSqlTableModel.OnFieldChange) self.model.select() self.model.setHeaderData(0, Qt.Horizontal, "车间编号") self.model.setHeaderData(1, Qt.Horizontal, "发酵罐编号") self.model.setHeaderData(2, Qt.Horizontal, "温度计编号") self.model.setHeaderData(3, Qt.Horizontal, "二维码图片") ############################# def setupUi(self, Form): Form.setObjectName("Form") Form.resize(500, 300) self.horizontalLayout = QtWidgets.QHBoxLayout(Form) self.horizontalLayout.setObjectName("horizontalLayout") self.tabWidget = QtWidgets.QTabWidget(Form) self.tabWidget.setObjectName("tabWidget") self.tab = QtWidgets.QWidget() self.tab.setObjectName("tab") self.create_view() self.pushButton_5 = QtWidgets.QPushButton(self.tab) self.pushButton_5.setGeometry(QtCore.QRect(220, 230, 75, 23)) self.pushButton_5.setObjectName("pushButton_5") self.tabWidget.addTab(self.tab, "") self.tab_2 = QtWidgets.QWidget() self.tab_2.setObjectName("tab_2") self.widget = QtWidgets.QWidget(self.tab_2) self.widget.setGeometry(QtCore.QRect(20, 12, 331, 241)) self.widget.setObjectName("widget") self.gridLayout = QtWidgets.QGridLayout(self.widget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") self.comboBox = QtWidgets.QComboBox(self.widget) self.comboBox.setObjectName("comboBox") self.comboBox.addItem("") self.gridLayout.addWidget(self.comboBox, 0, 1, 1, 1) self.label_2 = QtWidgets.QLabel(self.widget) self.label_2.setObjectName("label_2") self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1) self.lineEdit = QtWidgets.QLineEdit(self.widget) self.lineEdit.setObjectName("lineEdit") self.gridLayout.addWidget(self.lineEdit, 1, 1, 1, 1) self.label_3 = QtWidgets.QLabel(self.widget) self.label_3.setObjectName("label_3") self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1) self.label = QtWidgets.QLabel(self.widget) self.label.setObjectName("label") self.gridLayout.addWidget(self.label, 0, 0, 1, 1) self.lineEdit_3 = QtWidgets.QLineEdit(self.widget) self.lineEdit_3.setObjectName("lineEdit_3") self.gridLayout.addWidget(self.lineEdit_3, 3, 1, 1, 1) self.lineEdit_2 = QtWidgets.QLineEdit(self.widget) self.lineEdit_2.setObjectName("lineEdit_2") self.gridLayout.addWidget(self.lineEdit_2, 2, 1, 1, 1) self.label_4 = QtWidgets.QLabel(self.widget) self.label_4.setObjectName("label_4") self.gridLayout.addWidget(self.label_4, 3, 0, 1, 1) self.pushButton = QtWidgets.QPushButton(self.widget) self.pushButton.setObjectName("pushButton") self.gridLayout.addWidget(self.pushButton, 4, 1, 1, 1) self.pushButton_2 = QtWidgets.QPushButton(self.widget) self.pushButton_2.setObjectName("pushButton_2") self.gridLayout.addWidget(self.pushButton_2, 4, 2, 1, 1) self.tabWidget.addTab(self.tab_2, "") self.horizontalLayout.addWidget(self.tabWidget) self.retranslateUi(Form) self.pushButton_5.clicked.connect(Form.update) self.tabWidget.setCurrentIndex(0) QtCore.QMetaObject.connectSlotsByName(Form) def show_message_del_row(self): # show_date = self.tableView. button=QMessageBox.information(self.tableView,u"通知", u"您将删除:{}".format(), QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok) if button == QMessageBox.Ok: self.model.removeRow(self.tableView.currentIndex().row()) def findrow(self,i): self.delrow = i.row() s = self.tableView.currentIndex().row() s1 = self.tableView.currentIndex().data() print('del row=%s' % str(self.delrow),s1) def create_Menu(self,pos): menu = QMenu() item1 = menu.addAction(u'删除') self.action = menu.exec_(self.tableView.mapToGlobal(pos)) if self.action==item1: self.single_del_row.emit() def create_view(self): self.tableView = QtWidgets.QTableView(self.tab) self.tableView.setGeometry(QtCore.QRect(10, 11, 451, 211)) self.tableView.setObjectName("tableView") self.tableView.setModel(self.model) #允许右键产生菜单 self.tableView.setContextMenuPolicy(Qt.CustomContextMenu) self.tableView.customContextMenuRequested.connect(self.create_Menu) self.pushButton_3 = QtWidgets.QPushButton(self.tab) self.pushButton_3.setGeometry(QtCore.QRect(380, 230, 75, 23)) self.pushButton_3.setObjectName("pushButton_3") self.pushButton_4 = QtWidgets.QPushButton(self.tab) #pushButton_4新增 self.pushButton_4.setGeometry(QtCore.QRect(300, 230, 75, 23)) self.pushButton_4.setObjectName("pushButton_4") self.tableView.clicked.connect(self.findrow) self.pushButton_4.clicked.connect(lambda :self.model.insertRows(self.model.rowCount(),1)) self.pushButton_3.clicked.connect(lambda: self.model.removeRow(self.tableView.currentIndex().row())) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton_3.setText(_translate("Form", "删除")) self.pushButton_4.setText(_translate("Form", "新增")) self.pushButton_5.setText(_translate("Form", "刷新")) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Form", "查询-发酵罐")) self.comboBox.setItemText(0, _translate("Form", "#1号车间")) self.label_2.setText(_translate("Form", "发酵罐编号:")) self.label_3.setText(_translate("Form", "温度计编号:")) self.label.setText(_translate("Form", "车间编号:")) self.label_4.setText(_translate("Form", "上传二维码")) self.pushButton.setText(_translate("Form", "提交")) self.pushButton_2.setText(_translate("Form", "重置")) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Form", "新增-发酵罐")) def initializeModel(model): model.setTable('date_of_01cj') model.setEditStrategy(QSqlTableModel.OnFieldChange) model.select() model.setHeaderData(0, Qt.Horizontal, "id_cj") model.setHeaderData(1, Qt.Horizontal, "id_fa") model.setHeaderData(2, Qt.Horizontal, "id_temp") ################### ################### if __name__ == '__main__': app = QApplication(sys.argv) QMainWindow = QWidget() fjg_gl = Ui_Form() fjg_gl.setupUi(QMainWindow) fjg_gl.pushButton_5.clicked.connect(lambda :QApplication.processEvents()) QMainWindow.show() sys.exit(app.exec_()) '''db = QSqlDatabase.addDatabase('QSQLITE') db.setDatabaseName('../data_db/date_of_cj.db') model = QSqlTableModel() delrow = -1 initializeModel(model) view1 = createView("Table Model (View 1)", model) view1.clicked.connect(findrow) dlg = QDialog() layout = QVBoxLayout() layout.addWidget(view1) addBtn = QPushButton("添加一行") addBtn.clicked.connect(addrow) layout.addWidget(addBtn) delBtn = QPushButton("删除一行") delBtn.clicked.connect(lambda: model.removeRow(view1.currentIndex().row())) layout.addWidget(delBtn) dlg.setLayout(layout) dlg.setWindowTitle("Database 例子") dlg.resize(430, 450) dlg.show() sys.exit(app.exec_())'''