QDataWidgetMapper 数据库绑定 QLineEdit控件

This commit is contained in:
625781186 2018-10-29 15:48:48 +08:00
parent c306711edd
commit f5f27ff59e
10 changed files with 322 additions and 9 deletions

View file

@ -19,14 +19,19 @@
"CreateTime": "2018-09-18-18:45:14",
"ModifyTime": "2018-09-19-18:16:08"
},
"PluginPage3_addAction2": {
"Allow": true,
"CreateTime": "2018-09-24-20:54:48",
"ModifyTime": "2018-09-24-20:57:03"
},
"PluginPage3_addAction1": {
"Allow": true,
"CreateTime": "2018-09-24-18:26:10",
"ModifyTime": "2018-09-24-20:50:02"
},
"PluginPage1": {
"Allow": true,
"CreateTime": "2018-09-27-16:46:40",
"ModifyTime": "2018-10-07-21:37:19"
},
"PluginPage2": {
"Allow": true,
"CreateTime": "2018-09-27-16:46:40",
"ModifyTime": "2018-09-27-16:47:19"
}
}

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Project SYSTEM "Project-6.0.dtd">
<!-- eric project file for project Plugins -->
<!-- Saved: 2018-09-24, 21:05:10 -->
<!-- Saved: 2018-10-01, 18:41:58 -->
<!-- Copyright (C) 2018 , -->
<Project version="6.0">
<Language></Language>
@ -24,7 +24,6 @@
<Source>Plugins/PluginPage1_addTab4.py</Source>
<Source>Plugins/PluginPage2_error.py</Source>
<Source>Plugins/PluginPage3_addAction1.py</Source>
<Source>Plugins/PluginPage3_addAction2.py</Source>
<Source>Plugins/__init__.py</Source>
<Source>Plugins/page1/Ui_PluginPage1.py</Source>
<Source>Plugins/page1/__init__.py</Source>

View file

@ -0,0 +1,100 @@
#-*- coding: utf-8 -*-
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtSql import *
import sys
sys.path.append('./ui')
from Ui_MainWindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None, *args):
super(MainWindow, self).__init__(parent, *args)
self.setupUi(self)
self.resize(800,600)
#=============================== db ======================================#
# self.db = QSqlDatabase.addDatabase('QMYSQL')
# self.db.setHostName("127.0.0.1") # set address
# self.db.setUserName("root") # set user name
# self.db.setPassword('123456') # set user pwd
# self.db.setDatabaseName("database")
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('./db/database.db')
#================================= codemodel =====================================#
# 实例化model
self.codeModel = QSqlRelationalTableModel()
# model设置表
self.initializeModel(self.codeModel, 'Mongo')
# 设置编辑策略
# self.codeModel.setEditStrategy(QSqlTableModel.OnFieldChange)
# !!! 这里要注意 , 只能用这个策略 , 才可以实现自动提交
self.codeModel.setEditStrategy(QSqlTableModel.OnManualSubmit)
self.codeView = self.createView("code_View", self.codeModel)
self.verticalLayout.addWidget(self.codeView)
#================================ initData ==================================#
# 数据映射
self.mapper = QDataWidgetMapper()
# 提交策略
self.mapper.setSubmitPolicy(QDataWidgetMapper.AutoSubmit)
# 映射的模型源
self.mapper.setModel(self.codeModel)
self.mapper.addMapping(self.l1,0)
self.mapper.addMapping(self.l2,1)
self.mapper.addMapping(self.l3,2)
self.mapper.addMapping(self.l4,3)
self.mapper.addMapping(self.l5,4)
self.mapper.toFirst()
#================================ pushButton ==================================#
self.sub_btn.clicked.connect(self.mapper.submit)
self.sub_btn.clicked.connect(self.codeModel.submitAll)
self.pre_btn.clicked.connect(self.mapper.toPrevious)
self.next_btn.clicked.connect(self.mapper.toNext)
def initializeModel(self, model, tablename):
'''重关联。'''
model.setTable(tablename)
# model.setEditStrategy(QSqlTableModel.OnRowChange)
model.select()
def createView(self, title, model):
'''创建TableView视图'''
view = QTableView()
view.setModel(model)
view.setWindowTitle(title)
#列宽设置
view.horizontalHeader().setSectionResizeMode(3)
#行高设置
view.verticalHeader().setSectionResizeMode(1)
#充满列宽
view.horizontalHeader().setStretchLastSection(True)
# view.verticalHeader().setVisible(False)#隐藏行标题
#标题左对齐
view.horizontalHeader().setDefaultAlignment(Qt.AlignLeft)
#标题左对齐
view.verticalHeader().setDefaultAlignment(Qt.AlignLeft)
return view
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
app.setStyle(QStyleFactory.create("Fusion"))
ui = MainWindow()
ui.show()
sys.exit(app.exec_())

View file

@ -0,0 +1,4 @@
# QDataWidgetMapper 数据库绑定 QLineEdit控件
![1](ScreenShot/1.gif)

Binary file not shown.

After

Width:  |  Height:  |  Size: 137 KiB

Binary file not shown.

View file

@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>486</width>
<height>313</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true">QListView::item:alternate {
background-color: rgb(85, 255, 255);
}</string>
</property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="dockOptions">
<set>QMainWindow::AllowTabbedDocks|QMainWindow::AnimatedDocks</set>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="verticalLayout">
<item row="6" column="0">
<widget class="QLineEdit" name="l3"/>
</item>
<item row="4" column="0">
<widget class="QLineEdit" name="l1"/>
</item>
<item row="5" column="0">
<widget class="QLineEdit" name="l2"/>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="pre_btn">
<property name="text">
<string>上一条</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QTableView" name="tableView"/>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="next_btn">
<property name="text">
<string>下一条</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QListView" name="listView"/>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="sub_btn">
<property name="text">
<string>提交</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLineEdit" name="l4"/>
</item>
<item row="8" column="0">
<widget class="QLineEdit" name="l5"/>
</item>
</layout>
</widget>
<action name="saveaction">
<property name="text">
<string>保存</string>
</property>
<property name="shortcut">
<string>Ctrl+D</string>
</property>
</action>
<action name="action12">
<property name="text">
<string>查看帮助文档</string>
</property>
</action>
<action name="actionas">
<property name="text">
<string>关于</string>
</property>
</action>
<action name="actiondz">
<property name="text">
<string>dz</string>
</property>
</action>
<action name="actiongc">
<property name="text">
<string>gc</string>
</property>
</action>
<action name="actiondb">
<property name="text">
<string>db</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>

View file

@ -0,0 +1,95 @@
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'D:\QGroup_432987409\WoHowLearn\0.M_I_pyqt\partner_625781186\CodeTip\ui\MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(486, 313)
MainWindow.setStyleSheet("QListView::item:alternate {\n"
" \n"
" background-color: rgb(85, 255, 255);\n"
"}")
MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
MainWindow.setDockOptions(QtWidgets.QMainWindow.AllowTabbedDocks|QtWidgets.QMainWindow.AnimatedDocks)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.verticalLayout = QtWidgets.QGridLayout(self.centralWidget)
self.verticalLayout.setObjectName("verticalLayout")
self.l3 = QtWidgets.QLineEdit(self.centralWidget)
self.l3.setObjectName("l3")
self.verticalLayout.addWidget(self.l3, 6, 0, 1, 1)
self.l1 = QtWidgets.QLineEdit(self.centralWidget)
self.l1.setObjectName("l1")
self.verticalLayout.addWidget(self.l1, 4, 0, 1, 1)
self.l2 = QtWidgets.QLineEdit(self.centralWidget)
self.l2.setObjectName("l2")
self.verticalLayout.addWidget(self.l2, 5, 0, 1, 1)
self.pre_btn = QtWidgets.QPushButton(self.centralWidget)
self.pre_btn.setObjectName("pre_btn")
self.verticalLayout.addWidget(self.pre_btn, 1, 0, 1, 1)
self.tableView = QtWidgets.QTableView(self.centralWidget)
self.tableView.setObjectName("tableView")
self.verticalLayout.addWidget(self.tableView, 0, 0, 1, 1)
self.next_btn = QtWidgets.QPushButton(self.centralWidget)
self.next_btn.setObjectName("next_btn")
self.verticalLayout.addWidget(self.next_btn, 2, 0, 1, 1)
self.listView = QtWidgets.QListView(self.centralWidget)
self.listView.setObjectName("listView")
self.verticalLayout.addWidget(self.listView, 0, 1, 1, 1)
self.sub_btn = QtWidgets.QPushButton(self.centralWidget)
self.sub_btn.setObjectName("sub_btn")
self.verticalLayout.addWidget(self.sub_btn, 3, 0, 1, 1)
self.l4 = QtWidgets.QLineEdit(self.centralWidget)
self.l4.setObjectName("l4")
self.verticalLayout.addWidget(self.l4, 7, 0, 1, 1)
self.l5 = QtWidgets.QLineEdit(self.centralWidget)
self.l5.setObjectName("l5")
self.verticalLayout.addWidget(self.l5, 8, 0, 1, 1)
MainWindow.setCentralWidget(self.centralWidget)
self.saveaction = QtWidgets.QAction(MainWindow)
self.saveaction.setObjectName("saveaction")
self.action12 = QtWidgets.QAction(MainWindow)
self.action12.setObjectName("action12")
self.actionas = QtWidgets.QAction(MainWindow)
self.actionas.setObjectName("actionas")
self.actiondz = QtWidgets.QAction(MainWindow)
self.actiondz.setObjectName("actiondz")
self.actiongc = QtWidgets.QAction(MainWindow)
self.actiongc.setObjectName("actiongc")
self.actiondb = QtWidgets.QAction(MainWindow)
self.actiondb.setObjectName("actiondb")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pre_btn.setText(_translate("MainWindow", "上一条"))
self.next_btn.setText(_translate("MainWindow", "下一条"))
self.sub_btn.setText(_translate("MainWindow", "提交"))
self.saveaction.setText(_translate("MainWindow", "保存"))
self.saveaction.setShortcut(_translate("MainWindow", "Ctrl+D"))
self.action12.setText(_translate("MainWindow", "查看帮助文档"))
self.actionas.setText(_translate("MainWindow", "关于"))
self.actiondz.setText(_translate("MainWindow", "dz"))
self.actiongc.setText(_translate("MainWindow", "gc"))
self.actiondb.setText(_translate("MainWindow", "db"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

View file

@ -132,8 +132,8 @@ class MyEngineView(QWebEngineView):
def _downloadProgress(self , bytesReceived:"qint64", bytesTotal:"qint64"):
# bytesReceived 当前下载值 bytesTotal 文件总大小值
self.bytesReceived = bytesReceived
self.bytesTotal = bytesTotal
# self.bytesReceived = bytesReceived
# self.bytesTotal = bytesTotal
print(bytesReceived , bytesTotal )
def _finished(self):