一、项目地址
https://github.com/LinFeng-BingYi/DailyAccountBook
二、新增
1. 在表格中设置选项列表,让用户更快地编辑动账记录
1.1 功能详述
为表格中以下字段设置选项列表:
1. 需求强度(由"基本需求"更名)
温饱:基本维持生存且不铺张浪费的消费行为
小康:在温饱的基础上,可以使生活变得比较舒适的消费行为
奢华:可有可无的,或超出自身消费水平的消费行为
该属性意在于让用户更好地明白基本消费与超额消费占比,以便遏制不必要的消费行为
2. 类别(支出表、收入表)
计划在将来支持用户自定义支出或收入类别。目前写死在代码中,可以在CommonFiles/ConstArgs.py文件中修改后使用
3. 支出账户、收入账户、关联账户
计划在将来支持用户自定义账户列表。目前写死在代码中,可以在CommonFiles/ConstArgs.py文件中修改后使用文章来源:https://www.toymoban.com/news/detail-711673.html
1.2 代码实现
思路分析
支出、收入、转移表格中的列名有大量重复,因此将所有列的初始化集中到一个函数中,根据列名执行对应初始化方式。同时,还要区分已存在记录的行和空白行。综上,实现了三个函数:文章来源地址https://www.toymoban.com/news/detail-711673.html
函数名 | 作用 |
---|---|
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控件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!