PyQt 中的 Qcombobox,如何根据其他组合框的选择在一个组合框中设置选项?
Qcombobox in PyQt, How could I set the options in one combo box which depends on the choice of other combo box?
比如第一个组合框有选项A和选项B。
如果我在第一个组合框中 select 选项 A,则第二个组合框包含选项 1、2、3、4、5。
第二个组合框包含选项 a、b、c、d、e 如果我 select 第一个组合框中的选项 B。
我怎么能在 PyQt5 中做到这一点?我搜索了Qcomboboxclass,有2个classactivated
和currentIndexChanged
,但不知道我需要使用哪个以及如何在 PyQt5 中使用它。
我通过使用 3 个组合框(我们称它们为 x、y 和 z)来实现它
x 包含 A、B,用于选择在另一个组合框中显示的选项。选择 A 显示组合框 y,选择 B 显示组合框 z
y 包含 1,2,3,4
Z 包含 a,b,c,d
我用 QStackedLayout
代替第二个组合框,用两个组合框(y 和 z)加载它,并根据在组合框 x 中选择的选项一次显示其中一个。
方法 comboOptionChanged
处理这个问题。
只要在组合框 x 中选择了不同的选项,就会调用此方法。
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()
我会这样做:
import sys
from PyQt5 import QtWidgets
class ComboWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ComboWidget, self).__init__(parent)
self.setGeometry(50, 50, 200, 200)
layout = QtWidgets.QVBoxLayout(self)
self.comboA = QtWidgets.QComboBox()
# The second addItem parameter is itemData, which we will retrieve later
self.comboA.addItem('A', ['1', '2', '3', '4'])
self.comboA.addItem('B', ['a', 'b', 'c', 'd', 'e'])
self.comboA.currentIndexChanged.connect(self.indexChanged)
layout.addWidget(self.comboA)
self.comboB = QtWidgets.QComboBox()
# We could have added the 1,2,3,4 items directly, but the following
# is a bit more generic in case the combobox starts with a different
# index for some reason:
self.indexChanged(self.comboA.currentIndex())
layout.addWidget(self.comboB)
self.show()
def indexChanged(self, index):
self.comboB.clear()
data = self.comboA.itemData(index)
if data is not None:
self.comboB.addItems(data)
def main():
app = QtWidgets.QApplication(sys.argv)
w = ComboWidget()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
这样一来,您的代码就更有前瞻性了,因为如果您想在某个时候添加/修改/删除任何下拉菜单中的项目,它可以很容易地添加/修改/删除项目。
比如第一个组合框有选项A和选项B。 如果我在第一个组合框中 select 选项 A,则第二个组合框包含选项 1、2、3、4、5。 第二个组合框包含选项 a、b、c、d、e 如果我 select 第一个组合框中的选项 B。
我怎么能在 PyQt5 中做到这一点?我搜索了Qcomboboxclass,有2个classactivated
和currentIndexChanged
,但不知道我需要使用哪个以及如何在 PyQt5 中使用它。
我通过使用 3 个组合框(我们称它们为 x、y 和 z)来实现它
x 包含 A、B,用于选择在另一个组合框中显示的选项。选择 A 显示组合框 y,选择 B 显示组合框 z
y 包含 1,2,3,4
Z 包含 a,b,c,d
我用 QStackedLayout
代替第二个组合框,用两个组合框(y 和 z)加载它,并根据在组合框 x 中选择的选项一次显示其中一个。
方法 comboOptionChanged
处理这个问题。
只要在组合框 x 中选择了不同的选项,就会调用此方法。
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()
我会这样做:
import sys
from PyQt5 import QtWidgets
class ComboWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ComboWidget, self).__init__(parent)
self.setGeometry(50, 50, 200, 200)
layout = QtWidgets.QVBoxLayout(self)
self.comboA = QtWidgets.QComboBox()
# The second addItem parameter is itemData, which we will retrieve later
self.comboA.addItem('A', ['1', '2', '3', '4'])
self.comboA.addItem('B', ['a', 'b', 'c', 'd', 'e'])
self.comboA.currentIndexChanged.connect(self.indexChanged)
layout.addWidget(self.comboA)
self.comboB = QtWidgets.QComboBox()
# We could have added the 1,2,3,4 items directly, but the following
# is a bit more generic in case the combobox starts with a different
# index for some reason:
self.indexChanged(self.comboA.currentIndex())
layout.addWidget(self.comboB)
self.show()
def indexChanged(self, index):
self.comboB.clear()
data = self.comboA.itemData(index)
if data is not None:
self.comboB.addItems(data)
def main():
app = QtWidgets.QApplication(sys.argv)
w = ComboWidget()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
这样一来,您的代码就更有前瞻性了,因为如果您想在某个时候添加/修改/删除任何下拉菜单中的项目,它可以很容易地添加/修改/删除项目。