在PySide6/PyQt6的开发框架中,增加对表格多种格式录入的处理,以及主从表的数据显示和保存操作。

在PySide6/PyQt6的开发框架中, 为了方便对表格数据的快速录入,有时候包括多种录入的类型,包括文本框、数字格式(整数、小数)、日期、时间、下拉列表、自定义弹出框、单选框组、百分比、金额、颜色、图片、内置图标等多样化的处理需要,本篇就是基于此需求,在PySide6/PyQt6的开发框架中,增加对表格多种格式录入的处理,以及主从表的数据显示和保存操作。

PySide6/PyQt6的开发框架主界面支持多文档管理,可包括菜单栏、工具栏、内容区和状态栏等,内容区以选项卡方式展示多个窗口,如下所示。

常规的编辑界面如用户界面,双击列表弹出展示,如下所示。

主从表展示界面如下所示。

主从表直接编辑界面,在弹出对话框中进行编辑主表信息和明细记录,明细记录通过表格方式直接录入,方便各种类型的数据录入处理。

 

1、在PySide6/PyQt6的开发框架中表格的数据录入

我们先来看看测试案例,查看下表格中多种格式录入的效果

视频效果如下所示

如我们在产品报价单中进行主从表编辑数据的时候,界面如下所示,其中产品信息通过弹出自定义产品列表进行选择。

其他如产品类型、产品规格、产品型号、标准单位、产品尺寸等通过绑定系统字典类型,作为下拉列表的数据源。

上面就是实际的报价单的界面录入,可以通过自定义的对话框选择,也可以直接录入文本,还可以通过绑定字典类型获得下拉列表选择等处理操作。

 

2、表格数据直接录入的委托类处理

在PySide6/PyQt6中,对于 QTableView 的定制化输入,是通过继承 QStyledItemDelegate 来实现定制化的表格单元格输入或者显示的。如我们自定义类如下所示。

class CustomDelegate(QStyledItemDelegate):

我们让它支持的类型包括:

  •     - text: 普通文本 (QLineEdit)
  •     - int: 整数 (QSpinBox)
  •     - double: 浮点数 (QDoubleSpinBox)
  •     - date: 日期 (QDateEdit)
  •     - combo: 下拉选择 (QComboBox)
  •     - check: 复选框 (直接显示)
  •     - radio: 单选按钮组 (QRadioButton)
  •     - slider: 滑动条 (QSlider)
  •     - multiline: 多行文本 (QTextEdit)
  •     - password: 密码文本 (QLineEdit)
  •     - percent: 百分比 (QDoubleSpinBox)
  •     - currency: 货币 (QDoubleSpinBox)
  •     - time: 时间 (QTimeEdit)
  •     - datetime: 日期时间 (QDateTimeEdit)
  •     - color: 颜色选择 (QPushButton)
  •     - icon: 图标选择 (QPushButton)
  •     - bitmap: 位图选择 (QPushButton)
  •     - custom: 自定义不可编辑控件,同时触发 customTriggered 信号,传出单元格索引和字段名称

最终它的配置示例如下代码所示。

 config = {
 "name": {"type": "text"},
 "age": {"type": "int", "min": 0, "max": 120},
 "score": {"type": "double", "min": 0, "max": 1000, "decimals": 3, "step": 0.01},
 "birthday": {"type": "date", "format": "yyyy-MM-dd"},
 "tag": {"type": "combo", "items": ["选项A", "选项B", "选项C"]},
 "married": {"type": "check", "true": "是", "false": "否"},
 "gender": {"type": "radio", "items": ["男", "女", "未知"], "width": 180}, 
 "score_2": {"type": "slider", "min": 0, "max": 100, "step": 1},
 "description": {"type": "multiline"},
 "password": {"type": "password"},
 "percent": {"type": "percent", "min": 0, "max": 100, "decimals": 2, "step": 0.1},
 "currency": {"type": "currency", "min": 0, "max": 1000000, "decimals": 2, "step": 0.1},
 "time": {"type": "time", "format": "HH:mm:ss"},
 "datetime": {"type": "datetime", "format": "yyyy-MM-dd HH:mm:ss"},
 "color": {"type": "color", "format": "color"},
 "icon": {"type": "icon", "default": "SP_ArrowDown"},
 "bitmap": {"type": "bitmap", "default": "bitmap.png"},
 } 
 view = QTableView()
 view.setModel(model)
 delegate = CustomDelegate(config, view)
 view.setItemDelegate(delegate)

同时我们为了支持自定义的列表对话框选择,那么我们通过触发信号来获得对应的事件处理即可,如下所示。

 #自定义单元格编辑事件
 self.delegate.customTriggered.connect(self.on_custom_triggered)
 
 def on_custom_triggered(self, index: QModelIndex, field_name: str):
 """自定义单元格编辑事件"""
 # print(f"on_custom_triggered: index={index}, field_name={field_name}")
 if field_name == "productno":
 # 弹出选择产品对话框
 dlg = FrmProductSelect(self)
 if dlg.exec() == QDialog.DialogCode.Accepted:
 info = dlg.select_product
 if info is not None:
 # print(f"选择的产品信息:{info}")
 row = index.row()
 self.sub_table_model.SetValueByKey(row, "productno", info.productno)
 
 # #同步更新产品名称/条形码/规格/型号/单位/颜色/尺寸等
 self.sub_table_model.SetValueByKey(row, "productname", info.productname)
 ....
 # 同步更新数量
 ....
 # 同步更新金额小结
 ....
 dlg.deleteLater()

通过对自定义委托类中的 信号对象 customTriggered 进行监听,我们就可以获得用户触发选择某个单元格的事件,并可以通过弹出自定义对话框获取列表选择,并把数据写回到对应单元格中即可。

而对于指定系统字典类型,作为下拉列表的操作,我们只需要设置字段类型为combo或者radio,并动态设置字典类型绑定即可。

 #表格单元格的编辑控件配置, 动态指定字典
 config = {
 "orderno": {"type": "text"},
 "quantity": {"type": "int", "min": 0, "max": 1000},
 "saleprice": {"type": "double", "min": 0, "decimals": 3, "step": 0.01},
 "subamout": {"type": "double", "min": 0, "decimals": 3, "step": 0.01},
 "expiredate": {"type": "date", "format": "yyyy-MM-dd"},
 "note": {"type": "multiline"},
 
 "productno": {"type": "custom"},
 "producttype": {"type": "combo"},
 "model": {"type": "combo"},
 "specification": {"type": "combo"},
 "unit": {"type": "combo"},
 "color": {"type": "combo"},
 "productsize": {"type": "radio"},
 }

在窗体的初始化函数中,指定字典类型即可。

 async def init_dict_items(self):
 """初始化字典数据"""
 await self.txtOrderStatus.bind_dictType("报价单状态")
 
 #设置单元格的下拉列表为指定字典类型
 await self.delegate.SetEditor_DictType("producttype", "产品类型")
 await self.delegate.SetEditor_DictType("model", "产品型号")
 await self.delegate.SetEditor_DictType("specification", "产品规格")
 await self.delegate.SetEditor_DictType("unit", "产品标准单位")
 await self.delegate.SetEditor_DictType("color", "产品颜色")
 await self.delegate.SetEditor_DictType("productsize", "产品尺寸") 

   

最终界面可以实现数据表格的直接编辑处理,可以再常规的表中进行编辑,也可以在主从表的编辑界面中进行数据的快速录入。

作者:伍华聪原文地址:https://www.cnblogs.com/wuhuacong/p/19156434

%s 个评论

要回复文章请先登录注册