DEV Community

Facing issues while creating multiple instances of Dependent QComboBox

Shamith Nakka on July 18, 2023

I'm working on the GUI part of the input dock on the truss_connection_bolted module on a project called Osdag and the whole GUI part is written usi...
Collapse
 
iamwatchdogs profile image
Shamith Nakka

Even though, My question is marked as duplicate and has been closed *( I've applied for review that even though it was a similar case, it's no way near my case )*I have resolved the issue with the advice given by @musicamante in Stackoverflow. Even though it was abstract, I got the idea to implement the same logic differently.

Stackoverflow Comment

In the comment, he suggested that I use an explicit function instead of complex code for creating the dependent QComboBox. So, I have taken the same logic which I initially created and placed it into a method and used it within the current method where the whole logic was supposed to be executed. This method only takes the required data dictionary and returns a pair of dependent QComboBoxes.

Method implementation:

def create_dependent_QComboBox(self, selectable_options):
  combo_box_section_profile = QtWidgets.QComboBox()
  combo_box_connection_location = QtWidgets.QComboBox()

  for item in selectable_options['Section profile']:
    value = None
    if item in ['Angles', 'Star Angles', 'Back to Back Angles']:
      value = selectable_options['Connection Location']['Angle']
    elif item in ['Channels', 'Back to Back Channels']:
      value = selectable_options['Connection Location']['Channel']
    else:
      value = ['Invalid Option']
  combo_box_section_profile.addItem(item, value)

  combo_box_section_profile.currentIndexChanged.connect(
    lambda: combo_box_connection_location.clear() or
                  combo_box_connection_location.addItems(
                      combo_box_section_profile.currentData()
                   )
    )

  combo_box_connection_location.addItems(
      selectable_options['Connection Location']['Angle']
  )

  return combo_box_section_profile, combo_box_connection_location

Enter fullscreen mode Exit fullscreen mode

Updated logic:

table_widget = QtWidgets.QTableWidget(self.dockWidgetContents)
table_widget.setObjectName(option[0])
table_widget.setRowCount(8)
table_widget.setColumnCount(6)

combo_box_dictionary = dict()  # Just in case, if we need the combo_box object for each option

table_widget.setHorizontalHeaderLabels(list(option[3].keys()))

for col, (key, value) in enumerate(option[3].items()):
  if key == 'Connection Location' or value is None:
    continue
  elif key == 'Section profile':
    combo_box_section_profile_list = []
    combo_box_connection_location_list = []
    for row in range(8):
      combo_box_section_profile, combo_box_connection_location = self.create_dependent_QComboBox(option[3])
      table_widget.setCellWidget(row, col, combo_box_section_profile)
      table_widget.setCellWidget(row, col+1, combo_box_connection_location)

      combo_box_section_profile_list.append(combo_box_section_profile)
      combo_box_connection_location_list.append(combo_box_connection_location)

      combo_box_dictionary['Section profile'] = combo_box_section_profile_list
      combo_box_dictionary['Connection Location'] = combo_box_connection_location_list

  else:
    combo_box_list = []
    for row in range(8):
      combo_box = QtWidgets.QComboBox()
      for item in value:
        combo_box.addItem(item, value)
        table_widget.setCellWidget(row, col, combo_box)
        combo_box_list.append(combo_box)
      combo_box_dictionary[key] = combo_box_list

table_widget.resizeRowsToContents()
table_widget.resizeColumnsToContents()
Enter fullscreen mode Exit fullscreen mode

Source link:

Pull Request: PR#333
Commit: a3ac7e8