我正在一个名为Osdag的项目上的输入码头的GUI部分,使用 pyqt5 模块编写了整个GUI部分。我的任务是完成输入码头上的连接成员属性,这恰好是单个QTableWidget
上的一组QComboBox
小部件。我已经成功地写下了大部分部分,而表小部件中有一个QComboBox
取决于另一个QComboBox
。
经过长时间的研究,我能够创建一个显示所需功能的螺柱。这是存根的以下代码段。
# ---------------
# More Code above
# ---------------
# Table Widget Creation
table_widget = QtWidgets.QTableWidget(self.dockWidgetContents)
table_widget.setObjectName(option[0])
table_widget.setRowCount(8)
table_widget.setColumnCount(6)
# Attributes dictionary
selectable_options = {
'Section profile': ['Angle', 'Star Angles', 'Back to Back Angles', 'Channel','Back to Back Channels'],
'Connection Location': {
'Angle': ['Long leg connected', 'Short leg connected'],
'Channel': ['Web-connected']
},
'Section Size': ['20 x 20 x 3', '20 x 20 x 4', '25 x 25 x 3', '25 x 25 x 4', ...],
'Material': [ 'E165', 'E250 (Fe 410W) A', 'E250 (Fe 410W) B', 'E250 (Fe 410W) C', ....]
}
# Setting Head Labels to the table widget
table_widget.setHorizontalHeaderLabels(
list(selectable_options.keys()) + ["Angle with x-axis (Degrees)", "Factored Load (KN)"]
)
# Creating two indivdual QComboBox for testing out the dependency functionality
combo_box_secpro = QtWidgets.QComboBox()
combo_box_connloc = QtWidgets.QComboBox()
# Adding values to the "Section Profile" QComboBox
for item in selectable_options['Section profile']:
value = None
if item in ['Angle', 'Star Angles', 'Back to Back Angles']:
value = selectable_options['Connection Location']['Angle']
elif item in ['Channel', 'Back to Back Channels']:
value = selectable_options['Connection Location']['Channel']
combo_box_secpro.addItem(item, value)
# Connecting "Section Profile" QComboBox to "Connection Location" QComboBox
combo_box_secpro.activated.connect(
lambda: combo_box_connloc.clear() or combo_box_connloc.addItems(
selectable_options['Connection Location'][ 'Angle' if combo_box_secpro.currentText() in ['Angle', 'Star Angles', 'Back to Back Angles'] else 'Channel']
)
)
# Placing both of those the QComboBoxes into table Widget
table_widget.setCellWidget(0,0,combo_box_secpro)
table_widget.setCellWidget(0,1,combo_box_connloc)
# ---------------
# More code below
# ---------------
上面的代码片段工作正常,但是当我尝试循环相同的逻辑...它无法正常工作...循环逻辑的代码段:
# ---------------------
# More other code above
# ---------------------
# Note that this is just a looped version of the above logic,
# so all the other declarations of the dictionary mentioned above are present here..
combo_box_dictionary = dict() # Just in case, if we need the combo_box object for each option
for col, (key, value) in enumerate(selectable_options.items()):
# Skipping 'Connection location' as I have handled it in 'Section profile'
if key == 'Connection Location':
continue
# Creating 'Connection location' QComboBox that depend on 'Section profile' QComboBox
elif key == 'Section profile':
combo_box_section_profile_list = []
combo_box_connection_location_list = []
# Looping for 8 times for each row in table widget
for row in range(8):
# Creating QComboBox Object for both 'Section profile' & 'Connection location'
combo_box_section_profile = QtWidgets.QComboBox()
combo_box_connection_location = QtWidgets.QComboBox()
# Adding items into 'Section profile' QComboBox
for item in selectable_options['Section profile']:
value = None
if item in ['Angle', 'Star Angles', 'Back to Back Angles']:
value = selectable_options['Connection Location']['Angle']
elif item in ['Channel', 'Back to Back Channels']:
value = selectable_options['Connection Location']['Channel']
combo_box_section_profile.addItem(item, value)
# Connecting 'Section profile' dependent functionality to 'Connection Location'
combo_box_section_profile.activated.connect(
lambda: combo_box_connection_location.clear() or combo_box_connection_location.addItems(
selectable_options['Connection Location']['Angle' if combo_box_section_profile.currentText() in ['Angle', 'Star Angles', 'Back to Back Angles'] else 'Channel']
)
)
# Added Default Items into 'Connection Location'
combo_box_connection_location.addItems(selectable_options['Connection Location']['Angle'])
# Setting the QComboBoxes into their respective places
table_widget.setCellWidget(row, col, combo_box_section_profile)
table_widget.setCellWidget(row, col+1, combo_box_connection_location)
# Storing their Object References per each row
combo_box_section_profile_list.append(combo_box_section_profile)
combo_box_connection_location_list.append(combo_box_connection_location)
# Collectively storing Object References for both of them for all the rows
combo_box_dictionary['Section profile'] = combo_box_section_profile_list
combo_box_dictionary['Connection Location'] = combo_box_connection_location_list
else:
# Logic for adding simple QComboBoxes to the table widget
combo_box_list = []
for row in range(8):
combo_box = QtWidgets.QComboBox()
combo_box.addItems(value)
table_widget.setCellWidget(row, col, combo_box)
combo_box_list.append(combo_box)
combo_box_dictionary[key] = combo_box_list
# ---------------------
# More other code below
# ---------------------
注意:
- 如果您想查看整个代码,可以在我的truss-connection-bolted-gui的github中找到它。
- 如果您想在系统内设置项目,请确保以下内容;
- 使用 ubuntu 20.04 LTS版本通过您的VM。
- 遵循README.md仪器安装项目环境。
- 建议使用Pycharm IDE。
我花了很多时间搜索一些答案,但是可以找到一个答案。有方法可以解决此问题吗?
除了我的原始实施外,我尝试搜索连接方法的替代方案,但找不到一种...
然后,我尝试了不同的方法,例如存储对象引用,然后分配连接方法将所需功能链接到相应的Qcomboboxes。但是,这也没有解决...
for col, (key, value) in enumerate(selectable_options.items()):
combo_box_list = []
for row in range(8):
combo_box = QtWidgets.QComboBox()
if key == 'Connection Location':
value = selectable_options['Connection Location']['Angle']
combo_box.addItems(value)
table_widget.setCellWidget(row, col, combo_box)
combo_box_list.append(combo_box)
combo_box_dictionary[key] = combo_box_list
for index, secpro_QComboBox in enumerate(combo_box_dictionary['Section profile']):
connloc_QComboBox = combo_box_dictionary['Connection Location'][index]
secpro_QComboBox.activated.connect(
connloc_QComboBox.clear() or
connloc_QComboBox.addItems(
selectable_options['Connection Location']['Angle' if secpro_QComboBox.currentText() in ['Angle', 'Star Angles', 'Back to Back Angles'] else 'Channel']
)
)
我真的对结果感到困惑,因为在上面显示的原始实现中使用currentText()
之前没有TypeError
。我不知道该怎么办...