dissidia dissidia - 3 months ago 27
Python Question

Hiding rows in QTableWidget if 1 of the column does not have any values

I had wanted some opinions about a portion of code that I have written. My UI consists of a QTableWidget in which it has 2 columns, where one of the 2 columns are populated with QComboBox.

For the first column, it will fill in the cells with the list of character rigs (full path) it finds in the scene, while the second column will creates a combobox per cell and it populates in the color options as the option comes from a json file.

Right now I am trying to create some radio buttons that gives user the option to show all the results, or it will hides those rows if there are no color options within the combobox for that particular row.

As you can see in my code, I am populating the data per column, and so, when I tried to put in

if not len(new_sub_name) == 0:
while it does not put in any combobox with zero options, but how do I go about hiding such rows where there are no options in the combobox?

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)

# 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.character_table.setCellWidget(sub_index, 1, self.costume_color)


You can hide rows using setRowHidden. As for the rest of the code, I don't see much wrong with what you currently have, but FWIW I would write it something like this (completely untested, of course):

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()
            self.character_table.setCellWidget(index, 1, combo)
            self.character_table.setRowHidden(index, True)