gpt4 book ai didi

python - 如果其中 1 列没有任何值,则隐藏 QTableWidget 中的行

转载 作者:行者123 更新时间:2023-12-01 03:39:50 26 4
gpt4 key购买 nike

我想要一些关于我编写的部分代码的意见。我的 UI 由 QTableWidget 组成,其中有 2 列,其中 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 隐藏行。至于代码的其余部分,我认为您当前拥有的代码没有太大问题,但FWIW我会编写如下代码(当然,完全未经测试):

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()

关于python - 如果其中 1 列没有任何值,则隐藏 QTableWidget 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39780704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com