PyQt5模型视图委托
Model-View-Delegate
模型视图委托(MVD)是PyQt中特有的设计模式,类似MVC设计模式,将MVC设计模式中的Controller当做MVD中的Delegate,两者的概念基本相同。不同的是委托不是独立存在,而是包含在视图里面。
模型视图委托设计模式中,模型负责存储和管理数据;视图负责显示数据,其中界面的框架和基础信息是视图负责,具体数据的显示是委托负责;委托不仅仅负责数据的显示,还有一个重要的功能是负责数据的编辑,如在视图中双击就可以编辑数据。
视图是怎么获取模型数据?首先初始化视图时需要给视图设置模型,然后通过索引获取模型中对应位置的数据。
模型
数据的存储一般是列表,表格和树,不同的存储方式有不同的操作和管理方法,为了适应这种差异性,PyQt中提供了一种统一的操作方法,如下图所示:
对于列表数据:
根节点永远是NULL
row递增,column是0
对于表结构:
根节点永远是NULL
row和column递增
对于树结构:
根节点是NULL,父节点可变
row递增,column是0
模型类:
QStandardItemModel 通用存储,可以存储任意结构,最常用
QStringListModel 存储一组字符串
QDirModel 存储文件系统
QSqlQueryModel 对SQL查询的结果进行封装
QSqlTableModel 对SQL中的表格进行封装
例子:
import sys from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QApplication, QWidget, QTableView, QHBoxLayout class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__() self.mode = QStandardItemModel()
root = self.mode.invisibleRootItem() item1 = QStandardItem()
item1.setData('', Qt.DisplayRole)
item2 = QStandardItem()
item2.setData('', Qt.DisplayRole)
item3 = QStandardItem()
item3.setData('', Qt.DisplayRole)
item4 = QStandardItem()
item4.setData('', Qt.DisplayRole) root.setChild(0, 0, item1)
root.setChild(0, 1, item2)
root.setChild(1, 0, item3)
root.setChild(1, 1, item4) # 表结构存储
tableView = QTableView(self)
tableView.setModel(self.mode) layout = QHBoxLayout()
layout.addWidget(tableView)
self.setLayout(layout) if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.resize(500, 300)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
import sys from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QTreeView class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__() self.mode = QStandardItemModel()
root = self.mode.invisibleRootItem() item1 = QStandardItem()
item1.setData('', Qt.DisplayRole)
item2 = QStandardItem()
item2.setData('', Qt.DisplayRole)
item3 = QStandardItem()
item3.setData('', Qt.DisplayRole)
item4 = QStandardItem()
item4.setData('', Qt.DisplayRole) # 树结构存储
root.setChild(0, 0, item1)
item1.setChild(0, 0, item2)
item1.setChild(1, 0, item3)
item3.setChild(0, 0, item4) treeView = QTreeView(self)
treeView.setModel(self.mode) layout = QHBoxLayout()
layout.addWidget(treeView)
self.setLayout(layout) if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.resize(500, 300)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
视图
视图主要是用来显示数据,不同的视图对象用于显示不同存储结构的数据,主要的视图对象如下:
QListView 列表形式显示
QTableView 表格形式显示
QTreeView 树结构显示
单独的视图需要配合模型使用,因此PyQt对视图进行了再次封装,直接内部封装模型,主要对象如下:
QListWidget 列表形式显示的界面
QTableWidget 表格形式显示的界面
QTreeWidget 树结构形式显示的界面
委托
委托被封装在视图里面,主要是负责数据的显示和编辑功能。
数据的编辑主要涉及的方法:
createEditor 在双击进入编辑时,创建编辑器,如创建QLineEdit,QTextEdit
updateEditorGeometry 设置编辑器显示的位置和大小
setEditorData 更新数据到视图
setModeData 通过索引更新数据到模型
如果需要修改编辑时操作数据的方式,就需要重写上述方法。
视图主要是负责显示,其中涉及的方法:
paint 负责绘制
editorEvent 负责处理事件
如果要实现自定义视图显示,需要重写paint方法,在paint方法中绘制需要显示的控件,然后在editorEvent方法中处理事件,更新数据。
例子
在TableView中默认的整型数据编辑使用的是计数器控件,本例中是将计数器变成单行文本控件,实现数据的编辑功能。
import sys from PyQt5.QtCore import Qt, QVariant
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QApplication, QWidget, QTableView, QHBoxLayout, QStyledItemDelegate, QLineEdit class MyTableView(QTableView):
def __init__(self):
super(MyTableView, self).__init__() class MyDelgate(QStyledItemDelegate):
def __init__(self):
super(MyDelgate, self).__init__() # 创建编辑器
def createEditor(self, parent, option, index):
print('createEditor')
if index.column() == 1:
return QLineEdit(parent)
else:
return QStyledItemDelegate.createEditor(self, parent, option, index) # 设置编辑器的位置
def updateEditorGeometry(self, edit, option, index):
print('updateEditorGeometry')
if index.column() == 1:
edit.setGeometry(option.rect)
else:
return QStyledItemDelegate.updateEditorGeometry(self, edit, option, index) # 设置数据到模型
def setModelData(self, edit, model, index):
print('setModelData')
if index.column() == 1:
model.setData(index, int(edit.text()), Qt.DisplayRole)
else:
return QStyledItemDelegate.setModelData(self, edit, model, index) # 设置数据到视图
def setEditorData(self, edit, index):
print('setEditorData')
if index.column() == 1:
edit.setText(str(index.data(Qt.DisplayRole)))
else:
return QStyledItemDelegate.setEditorData(self, edit, index) class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__() self.mode = QStandardItemModel()
root = self.mode.invisibleRootItem() item1 = QStandardItem()
item1.setData('a', Qt.DisplayRole) item2 = QStandardItem()
item2.setData(1, Qt.DisplayRole) item3 = QStandardItem()
item3.setData(False, Qt.DisplayRole) item4 = QStandardItem()
item4.setData('b', Qt.DisplayRole) item5 = QStandardItem()
item5.setData(2, Qt.DisplayRole) item6 = QStandardItem()
item6.setData(True, Qt.DisplayRole) root.setChild(0, 0, item1)
root.setChild(0, 1, item2)
root.setChild(0, 2, item3)
root.setChild(1, 0, item4)
root.setChild(1, 1, item5)
root.setChild(1, 2, item6) tableView = MyTableView()
tableView.setModel(self.mode)
tableView.setItemDelegate(MyDelgate()) layout = QHBoxLayout()
layout.addWidget(tableView)
self.setLayout(layout) if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.resize(500, 300)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
在TableView中默认的整型显示是数字字符串,本例中将数字字符串变成进度条显示。
import sys from PyQt5.QtCore import Qt, QEvent
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QApplication, QWidget, QTableView, QHBoxLayout, QStyledItemDelegate, QStyle, QStyleOptionProgressBar, QStyleOptionButton class MyTableView(QTableView):
def __init__(self):
super(MyTableView, self).__init__() # 一定要注意复写方法的返回值
class MyDelgate(QStyledItemDelegate):
def __init__(self):
super(MyDelgate, self).__init__() # 委托负责具体数据的显示,因此重新paint方法
def paint(self, painter, option, index):
if index.column() == 1:
style = QStyleOptionProgressBar()
style.minimum = 0
style.maximum = 10
style.progress= index.data(Qt.DisplayRole)
style.rect = option.rect
QApplication.style().drawControl(QStyle.CE_ProgressBar, style, painter) elif index.column() == 2:
style = QStyleOptionButton()
if index.data(Qt.DisplayRole) == True:
style.state = QStyle.State_On
else:
style.state = QStyle.State_Off
style.state |= QStyle.State_Enabled
style.rect = option.rect
style.rect.setX(option.rect.x() + option.rect.width() / 2 - 7)
QApplication.style().drawControl(QStyle.CE_CheckBox, style, painter)
else:
return QStyledItemDelegate.paint(self, painter, option, index) def editorEvent(self, event, model, option, index):
if index.column() == 2:
if event.type() == QEvent.MouseButtonPress and option.rect.contains(event.pos()):
data = not index.data(Qt.DisplayRole)
model.setData(index, data, Qt.DisplayRole)
return True
else:
return QStyledItemDelegate.editorEvent(self, event, model, option, index) class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__() self.mode = QStandardItemModel()
root = self.mode.invisibleRootItem() item1 = QStandardItem()
item1.setData('a', Qt.DisplayRole) item2 = QStandardItem()
item2.setData(1, Qt.DisplayRole) item3 = QStandardItem()
item3.setData(False, Qt.DisplayRole) item4 = QStandardItem()
item4.setData('b', Qt.DisplayRole) item5 = QStandardItem()
item5.setData(2, Qt.DisplayRole) item6 = QStandardItem()
item6.setData(True, Qt.DisplayRole) root.setChild(0, 0, item1)
root.setChild(0, 1, item2)
root.setChild(0, 2, item3)
root.setChild(1, 0, item4)
root.setChild(1, 1, item5)
root.setChild(1, 2, item6) tableView = MyTableView()
tableView.setModel(self.mode)
tableView.setItemDelegate(MyDelgate()) layout = QHBoxLayout()
layout.addWidget(tableView)
self.setLayout(layout) if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.resize(500, 300)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
总结:如果修改视图中数据的显示方式,需要重写委托的paint方法,editorEvent方法是处理视图中的点击事件,根据需要绝对是否重写;如果要修改视图中数据的编辑方式,需要重写createEditor方法、updateEditorGeometry方法、setEditorData方法以及setModeData方法。
PyQt5模型视图委托的更多相关文章
- Qt 模型/视图/委托
模型.视图.委托 模型/视图架构基于MVC设计模式发展而来.MVC中,模型(Model)用来表示数据:视图(View)是界面,用来显示数据:控制(Controller)定义界面对用户输入的反应方式. ...
- Qt模型/视图框架----简单的例子
#include<qapplication.h> #include<qfilesystemmodel.h> #include<qtreeview.h> #inclu ...
- 【转】Qt之模型/视图
[本文转自]http://blog.sina.com.cn/s/blog_a6fb6cc90101hh20.html 作者: 一去丶二三里 关于Qt中MVC的介绍与使用,助手中有一节模型/视图编程 ...
- Qt模型/视图、委托
MVC视图和控制器对象相结合,其结果是模型/视图结构,仍然分离了数据与呈现给用户的方式,使得它可以在几个不同的视图中显示相同的数据,并实现新类型的视图而无需改变底层的数据结构.为了灵活的处理数据输入, ...
- Qt之模型/视图(委托)
概念 不同于模型 - 视图 - 控制器模式,模型/视图设计不包括用于管理与用户交互的一个完全独立的组件.一般情况,视图负责将模型数据呈现给用户以及处理用户输入.为了输入更加具有灵活性,则由委托来执行交 ...
- 设计模式 --- 模型-视图-控制器(Model View Controller)
模型-视图-控制器(Model-View-Controller,MVC)是Xerox PARC在20世纪80年代为编程语言Smalltalk-80发明的一种软件设计模式,至今已广泛应用于用户交互应用程 ...
- Qt之模型/视图(自定义进度条)
简述 在之前的章节中分享过关于QHeaderView表头排序.添加复选框等内容,相信大家模型/视图.自定义风格有了一定的了解,下面我们来分享一个更常用的内容-自定义进度条. 实现方式: 从QAbstr ...
- Qt之模型/视图(自定义风格)
Qt之模型/视图(自定义风格) 关于自定义风格是针对视图与委托而言的,使用事件与QSS都可以进行处理,今天关于美化的细节讲解一下. 先看下图: 先撇开界面的美观性(萝卜青菜,各有所爱),就现有的这些风 ...
- Qt之模型/视图(实时更新数据)
上两节简单介绍了Qt中对于模型/视图的编程,大部分助手里说的很清楚了,现在就开始实战部分吧! 在实际应用中,视图展示的数据往往并非一成不变的,那么如何实时更新成了一个很重要的问题!功能:(1)添加委托 ...
随机推荐
- 微信小程序入门-刘志敏-专题视频课程
微信小程序入门-269人已学习 课程介绍 微信小程序入门基础,给入门级程序员好的教程.教程中对小程序的介绍到小程序的基本使用都做了详细的介绍,教程以实用的实现作为案例,如列表下拉刷新.抽 ...
- Flutter学习笔记(36)--常用内置动画
如需转载,请注明出处:Flutter学习笔记(36)--常用内置动画 Flutter给我们提供了很多而且很好用的内置动画,这些动画仅仅需要简单的几行代码就可以实现一些不错的效果,Flutter的动画分 ...
- web安全中的session攻击
运行着个简单的demo后,打开login.jsp,使用firebug或chrome会发现,即使没有登录,我们也会有一个JSESSIONID,这是由服务器端在会话开始是通过set-cookie来设置的匿 ...
- 数据库char varchar nchar nvarchar,编码Unicode,UTF8,GBK等,Sql语句中文前为什么加N(一次线上数据存储乱码排查)
背景 公司有一个数据处理线,上面的数据经过不同环境处理,然后上线到正式库.其中一个环节需要将数据进行处理然后导入到另外一个库(Sql Server).这个处理的程序是老大用python写的,处理完后进 ...
- 入门大数据---Elasticsearch搭建与应用
项目版本 构建需要: JDK1.7 Elasticsearch2.2.1 junit4.10 log4j1.2.17 spring-context3.2.0.RELEASE spring-core3. ...
- 入门大数据---SparkSQL_Dataset和DataFrame简介
一.Spark SQL简介 Spark SQL 是 Spark 中的一个子模块,主要用于操作结构化数据.它具有以下特点: 能够将 SQL 查询与 Spark 程序无缝混合,允许您使用 SQL 或 Da ...
- 实战笔记丨JDBC问题定位指南
JDBC(Java数据库连接性)是Java API,用于管理与数据库的连接,发出查询和命令以及处理从数据库获得的结果集.JDBC在1997年作为JDK 1.1的一部分发布,是为Java持久层开发的首批 ...
- int与bigdecimal的相互转换
int转bigdecimal BigDecimal number = new BigDecimal(0); int value=score; number=BigDecimal.valueOf((in ...
- 使用Visual Studio 开发SharePoint项目时的快捷键
组合键:ctrl+c,alt+c,Shift+ctrl+c,可以快速的将文件拷贝到对应的部署目录下.
- Flutter —快速开发的IDE快捷方式
老孟导读:这是老孟翻译的精品文章,文章所有权归原作者所有. 欢迎加入老孟Flutter交流群,每周翻译2-3篇付费文章,精彩不容错过. 原文地址:https://medium.com/flutter- ...