我想知道一些关于我所写的一部分代码的意见。我的UI由一个QTableWidget
组成,其中有2列,其中一列用QComboBox
填充。
对于第一列,它将用它在场景中找到的角色装备列表(完整路径)填充单元格,而第二列将为每个单元格创建Qcombobox
,并填充颜色选项,因为选项来自json文件。
现在我正试图创建一些单选按钮,为用户提供显示所有结果的选项,或者它将隐藏这些行,如果Qcombobox
中没有特定行的颜色选项。
正如您在我的代码中看到的,我正在填充每列的数据,因此,当我试图放入if not len(new_sub_name) == 0:
时,它没有放入任何零选项的Qcombobox
,但是我如何去隐藏这样的行,在Qcombobox
中没有选项?
def populate_table_data(self):
self.sub_names, self.fullpaths = get_chars_info()
# Output Results
# self.sub_names : ['/character/nicholas/generic', '/character/mary/default']
# self.fullpaths : ['|Group|character|nicholas_generic_001', '|Group|character|mary_default_001']
# Insert fullpath into column 1
for fullpath_index, fullpath_item in enumerate(self.fullpaths):
new_path = QtGui.QTableWidgetItem(fullpath_item)
self.character_table.setItem(fullpath_index, 0, new_path)
self.character_table.resizeColumnsToContents()
# Insert colors using itempath into column 2
for sub_index, sub_name in enumerate(self.sub_names):
new_sub_name = read_json(sub_name)
if not len(new_sub_name) == 0:
self.costume_color = QtGui.QComboBox()
self.costume_color.addItems(list(sorted(new_sub_name)))
self.character_table.setCellWidget(sub_index, 1, self.costume_color)
可以使用setRowHidden隐藏行。至于其余的代码,我看不出你现在的代码有什么问题,但是我想把它写成这样(当然,完全未经测试):
def populate_table_data(self):
self.sub_names, self.fullpaths = get_chars_info()
items = zip(self.sub_names, self.fullpaths)
for index, (sub_name, fullpath) in enumerate(items):
new_path = QtGui.QTableWidgetItem(fullpath)
self.character_table.setItem(index, 0, new_path)
new_sub_name = read_json(sub_name)
if len(new_sub_name):
combo = QtGui.QComboBox()
combo.addItems(sorted(new_sub_name))
self.character_table.setCellWidget(index, 1, combo)
else:
self.character_table.setRowHidden(index, True)
self.character_table.resizeColumnsToContents()