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

2024-10-02 08:20:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想对我写的代码的一部分有一些看法。我的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)

Tags: 代码nameselfnewindexnames选项table
1条回答
网友
1楼 · 发布于 2024-10-02 08:20:23

可以使用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()

相关问题 更多 >

    热门问题