【日常收支账本】【Day04】优化编辑动账记录的操作——QTableWidget单元格设置QComboBox控件
一、项目地址
https://github.com/LinFeng-BingYi/DailyAccountBook
二、新增
1. 在表格中设置选项列表,让用户更快地编辑动账记录
1.1 功能详述
为表格中以下字段设置选项列表:
1. 需求强度(由"基本需求"更名)
温饱:基本维持生存且不铺张浪费的消费行为
小康:在温饱的基础上,可以使生活变得比较舒适的消费行为
奢华:可有可无的,或超出自身消费水平的消费行为

该属性意在于让用户更好地明白基本消费与超额消费占比,以便遏制不必要的消费行为
2. 类别(支出表、收入表)

计划在将来支持用户自定义支出或收入类别。目前写死在代码中,可以在CommonFiles/ConstArgs.py文件中修改后使用
3. 支出账户、收入账户、关联账户

计划在将来支持用户自定义账户列表。目前写死在代码中,可以在CommonFiles/ConstArgs.py文件中修改后使用
1.2 代码实现
思路分析
支出、收入、转移表格中的列名有大量重复,因此将所有列的初始化集中到一个函数中,根据列名执行对应初始化方式。同时,还要区分已存在记录的行和空白行。综上,实现了三个函数:
| 函数名 | 作用 |
|---|---|
| setExistTableCell | 为已存在记录的表格行设置单元格 |
| setBlankTableCell | 为表格的空白行设置单元格 |
| getExistTableCell | 获取某行记录的数据,组装成字典 |
代码
def setExistTableCell(self, tableWidget, current_row, value_dict: dict, const_class):
"""
Describe: 为已存在记录的表格行设置单元格。根据列名设置对应类型(格式)的单元格
Args:
tableWidget: QTableWidget
涉及的表格
current_row: int
本条记录所在行号
value_dict: dict
记录字典
const_class: Union[ExpenseConst, IncomeConst, MovementConst]
记录类型对应的常量类
"""
keys_list = list(value_dict.keys())
for key, value in value_dict.items():
if key == 'necessity':
comboBox = ComboBoxInTableWidget(NECESSITY, value)
tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
elif key == 'value':
tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(str(value)))
elif key == 'category':
comboBox = ComboBoxInTableWidget(const_class.CATEGORY, value)
tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
elif key == 'detail':
tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(value))
elif key == 'describe':
tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(value))
elif key == 'from':
comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, value)
tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
elif key == 'to':
comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, value)
tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
elif key == 'associatedFund':
comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, value)
tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
else:
print("未知的记录属性!!")
return
tableWidget.setCellWidget(current_row, len(keys_list), self.buttonsForExistRow(tableWidget))
def setBlankTableCell(self, tableWidget, current_row, const_class):
"""
Describe: 为表格的空白行设置单元格。根据列名设置对应类型(格式)的单元格
Args:
tableWidget: QTableWidget
涉及的表格
current_row: int
本条记录所在行号
const_class: Union[ExpenseConst, IncomeConst, MovementConst]
记录类型对应的常量类
"""
keys_list = [const_class.TABLEWIDGET_COLUMN_HEAD[tableWidget.horizontalHeaderItem(i).text()] for i in range(tableWidget.columnCount()-1)]
for key in keys_list:
if key == 'necessity':
comboBox = ComboBoxInTableWidget(NECESSITY, 'True')
tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
elif key == 'value':
tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(''))
elif key == 'category':
comboBox = ComboBoxInTableWidget(const_class.CATEGORY, 0)
tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
elif key == 'detail':
tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(''))
elif key == 'describe':
tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(' '))
elif key == 'from':
comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, 0)
tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
elif key == 'to':
comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, 0)
tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
elif key == 'associatedFund':
comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, None)
tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
else:
print("未知的记录属性!!: {}", format(key))
return
tableWidget.setCellWidget(current_row, len(keys_list), self.buttonsForNewRow(tableWidget))
def getExistTableCell(self, tableWidget, current_row, const_class):
new_data_dict = OrderedDict()
for i in range(tableWidget.columnCount() - 1):
key = const_class.TABLEWIDGET_COLUMN_HEAD[tableWidget.horizontalHeaderItem(i).text()]
if key == 'necessity':
comboBox: ComboBoxInTableWidget = tableWidget.cellWidget(current_row, i)
new_data_dict[key] = str(comboBox.getKeyByCurrentText())
elif key == 'value':
new_data_dict[key] = tableWidget.item(current_row, i).text()
elif key == 'category':
comboBox: ComboBoxInTableWidget = tableWidget.cellWidget(current_row, i)
new_data_dict[key] = str(comboBox.getKeyByCurrentText())
elif key == 'detail':
new_data_dict[key] = tableWidget.item(current_row, i).text()
elif key == 'describe':
new_data_dict[key] = tableWidget.item(current_row, i).text()
elif key == 'from':
comboBox: ComboBoxInTableWidget = tableWidget.cellWidget(current_row, i)
new_data_dict[key] = str(comboBox.getKeyByCurrentText())
elif key == 'to':
comboBox: ComboBoxInTableWidget = tableWidget.cellWidget(current_row, i)
new_data_dict[key] = str(comboBox.getKeyByCurrentText())
elif key == 'associatedFund':
comboBox: ComboBoxInTableWidget = tableWidget.cellWidget(current_row, i)
new_data_dict[key] = str(comboBox.getKeyByCurrentText())
else:
print("未知的记录属性!!: {}", format(key))
return None
return new_data_dict
【日常收支账本】【Day04】优化编辑动账记录的操作——QTableWidget单元格设置QComboBox控件的更多相关文章
- 在网页中编辑报表的报表设计器Stimulsoft Reports Designer.Web报表控件
Stimulsoft Reports Designer.Web报表控件是一款网页报表设计器.您想在网页中编辑您的报表吗?现在是可能的! Stimulsoft Reports Designer.Web ...
- ABAP 将单元格设置编辑状态 FORM
FORM set_style USING fieldname style TYPE string CHANGING ...
- html5--移动端视频video的android兼容,去除播放控件、全屏等
html5 中的video 在手机浏览器中的总结所有页面播放时, 如果选择全屏播放, 播放画面将浮动到屏幕的最上层 IOS 手机 自动播放 播放界面浮动文字 播放时是否自动全屏 能否嵌入在页面中播 ...
- QRowTable表格控件(三)-效率优化之-合理使用QStandardItem
目录 一.开心一刻 二.概述 三.效果展示 四.QStandardItem 1.QStandardItem是什么鬼 2.性能分析 3.QStandardItem使用上的坑 五.相关文章 原文链接:QR ...
- easyui datagrid动态设置行、列、单元格不允许编辑
Easyui datagrid 行编辑.列编辑.单元格编辑设置 功能: 动态对datagrid 进行行.列.单元格编辑进行设置不允许编辑. 禁用行编辑: 在编辑方法调用前,对选择的行进行判断,如果不允 ...
- QRowTable表格控件(四)-效率优化之-优化数据源
目录 一.开心一刻 二.问题分析 三.重写数据源 1.自己存储数据 2.重写data接口 四.比较 五.相关文章 原文链接:QRowTable表格控件(四)-效率优化之-优化数据源 一.开心一刻 一程 ...
- [C1] 优化 C1FlexGrid 单元格边框
一 优化理由 如下图所示,如果按照 C1FlexGrid 自带的单元格边框设置,即对每个单元格的 CellStyle 的 BorderThickness 进行设置,会得到如下图的效果: 其中,明显可 ...
- 基于MVC4+EasyUI的Web开发框架经验总结(5)--使用HTML编辑控件CKEditor和CKFinder
Web开发上有很多HTML的编辑控件,如CKEditor.kindeditor等等,很多都做的很好,本文主要介绍在MVC界面里面,CKEditor的配置和使用.CKEditor的前身是FCKEdito ...
- Cxgrid获取选中行列,排序规则,当前正在编辑的单元格内的值
Delphi Cxgrid获取选中行列,排序规则,当前正在编辑的单元格内的值 cxGrid1DBTableView1.Controller.FocusedRowIndex 当前行号 cxGrid1DB ...
- MFC List Control 控件添加单元格编辑,实现可编辑重写
在实现随机生成四则运算的个人项目中,目前已经完成基本功能,想要把程序变成一个Windows界面的程序.原本以为学习过MFC,应该很快就能完成.但是由于以前用的都是VC6.0,这次用了VS2010,稍微 ...
随机推荐
- 河南省第十四届icpc大学生程序设计竞赛-clk
这次比赛赛程比较长,520出发,521,回学校,出发的那一天有点热,感觉不是很好,而且那一天感觉有点生病,应该只是普通感冒,热身赛的时候被oier吊打,省实验真厉害,晚上回酒店后,我喊队友,补了前年的 ...
- linux 软件包:UnixBench 性能测试工具、跑分神器
目录 安装 使用 结果示例 测试项说明 UnixBench是一个类unix系(Unix,BSD,Linux)统下的性能测试工具,一个开源工具,被广泛用与测试linux系统主机的性能.Unixbench ...
- 信创啊,信创。Solon 的 war 包,现在同时支持 jakarta.servlet(及 javax.servlet)容器了!
Solon 是个神奇的项目,不是基于 Servlet 的.但是又很支持 Servlet,尤其是 war 包.打起来还挺方便的. 如果你是做信创的(听说,很多信创项目是用 war 部署到 tomcat ...
- 【博客索引】Welcome!!
欢迎来到 Daniel_yzy 的博客园 个人简介 初二,男,就读于长沙市一中双语实验学校. 爱好 OI,一生讨厌文化课. 当然,也是唯物主义无神论者. 已有 npy,要问是谁的话可以私下问. 博客索 ...
- 代码随想录算法训练营第三天| LeetCode 242.有效的字母异位词 349. 两个数组的交集 1. 两数之和
242.有效的字母异位词 卡哥建议: 这道题目,大家可以感受到数组用来做哈希表给我们带来的遍历之处. 题目链接/文章讲解/视频讲解: https://programmercarl.com ...
- [db2]数据备份与还原
前言 备份还原db2数据库一般有两种方式,一种是使用db2 backup + db2 restore,另一种是db2move + db2look.前者备份的数据库文件不能使用后者的方式进行还原. 实例 ...
- auto-GPT部署
Auto-GPT 是一个实验性开源应用程序,其作者在3月31日将其发布在Github上.它以GPT-4 作为驱动,可以自主做出决定以实现目标,无需用户干预.AutoGPT的地址:https://git ...
- 数据可视化【原创】vue+arcgis+threejs 实现流光立体墙效果
本文适合对vue,arcgis4.x,threejs,ES6较熟悉的人群食用. 效果图: 素材: 主要思路: 先用arcgis externalRenderers封装了一个ExternalRender ...
- stencilJs学习之构建 Drawer 组件
前言 在之前的学习中,我们已经掌握了 stencilJs 中的一些核心概念和基础知识,如装饰器 Prop.State.Event.Listen.Method.Component 以及生命周期方法.这些 ...
- 深入探究API接口
作为程序员,我们经常会遇到需要获取外部数据或调用外部服务的情况.而API(Application Programming Interface,应用程序编程接口)接口就是这样的一种机制,它允许我们的应用 ...