【问题标题】:Hiding rows in QTableWidget if 1 of the column does not have any values如果列的 1 没有任何值,则隐藏 QTableWidget 中的行
【发布时间】:2017-02-08 09:26:38
【问题描述】:

我想对我编写的部分代码提出一些意见。我的 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)

【问题讨论】:

    标签: python pyqt maya qtablewidget qcombobox


    【解决方案1】:

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

    【讨论】:

    • 嘿,谢谢,但对我如何填充数据有任何见解吗?
    • @dissidia。不知道它对你有多大用处,但我已经稍微扩展了我的答案。
    • 我一定会去看看的,忘记带我的代码回家了。但即便如此,我能问为什么(sub_name, fullpath) 被放在一起吗?我想这只是优化和缩短我的代码?
    • @dissidia。元组扩展需要括号。我忘了把enumerate 放在那里,所以这可能不是很明显。
    猜你喜欢
    • 1970-01-01
    • 2013-03-17
    • 2021-11-11
    • 2013-07-01
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多