pyqt-数据库操作
pyqt操作sqlit3的基本步骤
import sys from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtSql import * from os.path import exists # 先判断db路径是否存在 if not exists('student.db'): print("student.db数据库不存在") sys.exit() # 1、指定连哪个类型数据库 db = QSqlDatabase.addDatabase('QSQLITE') # 2、指定连哪个库 db.setDatabaseName('student.db') # 3、打开库 db.open() # 4、根据数据库创建model model = QSqlTableModel(None,db) # 不需要父类,连接哪个db # 5、与哪张表相连 model.setTable('student') # 5、操作数据库数据 model.select() # 操作ui app = QApplication([]) view = QTableView() view.setModel(model) view.show() sys.exit(app.exec_())
修改表头
import sys from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtSql import * from os.path import exists # 先判断db路径是否存在 if not exists('student.db'): print("student.db数据库不存在") sys.exit() # 1、指定连哪个类型数据库 db = QSqlDatabase.addDatabase('QSQLITE') # 2、指定连哪个库 db.setDatabaseName('student.db') # 3、打开库 db.open() # 4、根据数据库创建model model = QSqlTableModel(None,db) # 不需要父类,连接哪个db # 5、与哪张表相连 model.setTable('student') # 修改表头 model.setHeaderData(0,Qt.Horizontal,'名字') model.setHeaderData(1,Qt.Horizontal,'电话') model.setHeaderData(2,Qt.Horizontal,'地址') # 6、操作数据库数据 model.select() # 操作ui app = QApplication([]) view = QTableView() view.setModel(model) view.show() sys.exit(app.exec_())
添加,删除按钮
我们不能直接在view上去操作了,给它一个dialog窗体,在窗体上操作了
import sys from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtSql import * from os.path import exists # 先判断db路径是否存在 if not exists('student.db'): print("student.db数据库不存在") sys.exit() # 1、指定连哪个类型数据库 db = QSqlDatabase.addDatabase('QSQLITE') # 2、指定连哪个库 db.setDatabaseName('student.db') # 3、打开库 db.open() # 4、根据数据库创建model model = QSqlTableModel(None,db) # 不需要父类,连接哪个db # 5、与哪张表相连 model.setTable('student') # 修改表头 model.setHeaderData(0,Qt.Horizontal,'名字') model.setHeaderData(1,Qt.Horizontal,'电话') model.setHeaderData(2,Qt.Horizontal,'地址') # 6、操作数据库数据 model.select() # 操作ui app = QApplication([]) view = QTableView() view.setModel(model) # 窗体 dialog = QDialog() dialog.setWindowTitle('学生信息表') # 布局 layout = QVBoxLayout() layout.addWidget(view) button_add = QPushButton("添加") button_remove = QPushButton("删除") layout.addWidget(button_add) layout.addWidget(button_remove) dialog.setLayout(layout) dialog.show() sys.exit(app.exec_())
添加,删除功能实现
import sys from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtSql import * from os.path import exists # 先判断db路径是否存在 if not exists('student.db'): print("student.db数据库不存在") sys.exit() # 1、指定连哪个类型数据库 db = QSqlDatabase.addDatabase('QSQLITE') # 2、指定连哪个库 db.setDatabaseName('student.db') # 3、打开库 db.open() # 4、根据数据库创建model model = QSqlTableModel(None,db) # 不需要父类,连接哪个db # 5、与哪张表相连 model.setTable('student') # 修改表头 model.setHeaderData(0,Qt.Horizontal,'名字') model.setHeaderData(1,Qt.Horizontal,'电话') model.setHeaderData(2,Qt.Horizontal,'地址') # 6、操作数据库数据 model.select() # 操作ui app = QApplication([]) view = QTableView() view.setModel(model) # 添加功能 def add_row(): print(model.rowCount()) # 打印行数 b = model.insertRows(model.rowCount(),1) # 在哪个地方插入几行,返回bool是否成功 print(b) # 窗体 dialog = QDialog() dialog.setWindowTitle('学生信息表') # 布局 layout = QVBoxLayout() layout.addWidget(view) button_add = QPushButton("添加") button_add.clicked.connect(add_row) button_remove = QPushButton("删除") # 删除一行,删除哪行?--》删除你鼠标点中的那行 button_remove.clicked.connect(lambda :model.removeRow(view.currentIndex().row())) layout.addWidget(button_add) layout.addWidget(button_remove) dialog.setLayout(layout) dialog.show() sys.exit(app.exec_())
pyqt中的mvc模型
from os.path import expanduser from PyQt5.QtWidgets import * import sys # 获取路径信息 data 展示的数据 home_directory = expanduser("~") # 构建QT程序 app = QApplication([])
# view V view = QTreeView()
# model m model = QDirModel() # 这个比较特殊,如果不写的话默认是全文件的磁盘结构 view.setModel(model) # 将model注册到view里面 view.show() sys.exit(app.exec_())
运行后结果展示

展示自己定义的路径
# 获取路径信息 data home_directory = expanduser("~") # 构建QT程序 app = QApplication([]) # view view = QTreeView() # model model = QDirModel() view.setModel(model) view.setRootIndex(model.index(home_directory)) # 将data的目录做为根目录 view.show() sys.exit(app.exec_())

QListView - --QStringListModel
from os.path import expanduser from PyQt5.QtWidgets import * import sys from PyQt5.QtCore import QStringListModel # 构建QT程序 app = QApplication([]) # view view = QListView() # model model = QStringListModel(['张三','李四','王五']) view.setModel(model) view.show() sys.exit(app.exec_())
常用的视图组件

model 模型


自定义TableModel
通过上述观察,发现QAbstractTableModel子类都是和数据库相关的tablemodel,所以我们需要自定义个
from PyQt5.QtWidgets import * import sys from PyQt5.QtCore import * headers = ['国家','感染病例','确诊人数'] rows = [('美国','10000','5000'),('英国','8000','2000'),('中国','2000','100')] class TableModel(QAbstractTableModel): # 重写rowCount 方法 ,统计多少行 def rowCount(self, parent: QModelIndex = ...): return len(rows) # 重写columnCount方法 ,统计多少列 def columnCount(self, parent: QModelIndex = ...): return len(headers) # 重写 数据内容 def data(self, index: QModelIndex, role: int = ...): return rows[index.row()][index.column()] # 重写表头方法 def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...): return headers[section] app = QApplication([]) model = TableModel() view = QTableView() view.setModel(model) view.show() import sys sys.exit(app.exec_())

发现结果比较不好看,且没又headers部分,所以我们需要对头和body处理下
# 重写 数据内容 def data(self, index: QModelIndex, role: int = ...): if role != Qt.DisplayRole : return QVariant() return rows[index.row()][index.column()] # 重写表头方法 def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...): if role != Qt.DisplayRole or orientation != Qt.Horizontal: return QVariant() return headers[section]


浙公网安备 33010602011771号