<p>我通过使用3个组合框(让它们称为x、y和z)来实现</p>
<ol>
<li><p>x包含A、B,用于选择在另一个组合框中显示的选项。选择A显示组合框y,选择B显示组合框z</p></li>
<li><p>y包含1、2、3、4</p></li>
<li><p>Z包含a、b、c、d</p></li>
</ol>
<p>我用<code>QStackedLayout</code>代替第二个组合框,用两个组合框(y和z)加载它,并根据组合框x中选择的选项一次显示其中一个</p>
<p>方法<code>comboOptionChanged</code>处理此问题。
每当选择combobox x中的其他选项时,都会调用此方法。在</p>
<pre><code>import sys
from PyQt5.QtWidgets import QWidget, QComboBox, QApplication, QFormLayout, QLabel,QStackedLayout
class DynamicComboExample(QWidget):
def __init__(self):
super().__init__()
self.setupUI()
def setupUI(self):
self.setGeometry(300,300, 500, 300)
self.show()
combo_a = QComboBox(self)
combo_a.addItem('A')
combo_a.addItem('B')
# set slot for when option of combobox A is changed
combo_a.currentIndexChanged[int].connect(self.comboOptionChanged)
self.combo_b = QComboBox()
self.combo_b.addItem('1')
self.combo_b.addItem('2')
self.combo_b.addItem('3')
self.combo_b.addItem('4')
self.combo_c = QComboBox()
self.combo_c.addItem('a')
self.combo_c.addItem('b')
self.combo_c.addItem('c')
self.combo_c.addItem('d')
self.combo_c.addItem('e')
# use a stacked layout to view only one of two combo box at a time
self.combo_container_layout = QStackedLayout()
self.combo_container_layout.addWidget(self.combo_b)
self.combo_container_layout.addWidget(self.combo_c)
combo_container = QWidget()
combo_container.setLayout(self.combo_container_layout)
form_layout = QFormLayout()
form_layout.addRow(QLabel('A:\t'), combo_a)
# the stacked layout is placed in place of the (meant to be) second combobox
form_layout.addRow(QLabel('B:\t'), combo_container)
self.setLayout(form_layout)
def comboOptionChanged(self, idx):
''' gets called when option for combobox A is changed'''
# check which combobox_a option is selected ad show the appropriate combobox in stacked layout
self.combo_container_layout.setCurrentIndex(0 if idx == 0 else 1)
def main():
app= QApplication(sys.argv)
w = DynamicComboExample()
exit(app.exec_())
if __name__ == '__main__':
main()
</code></pre>