QComboBox 类
- setItemText()
- removeItem()
- clear()
- currentItem()
- setCurrentIndex()
- count()
- setMaxCount()
- setEditable()
- addItem()
- addItems()
- itemText()
- currentIndex()
QcomboBox类可发射的信号:
- currentIndexChanged():当组合框的索引更改时发出,即用户在组合框中选择一些新项
- activated():当用户更改索引时发出
- highlighted():当用户突出显示组合框中的项目时发出
- editTextChanged():当可编辑组合框的文本更改时发出
实例
- 创建Dialog without Buttons模板窗口
- 拖出两个Label部件和一个Combo Box部件
- 设定第一个Label部件的text为Select your account type, 第二个Label的text置空
- 设定Combo Box部件的objectName为comboBoxAccountType
- 设定第二个Label部件的objectName为labelAccountType
- 右击ComboBox部件选择编辑项目
- 添加项目
- 保存为demoComboBox.ui文件
- 使用pyuic生成demoComboBox.py文件
- 创建 callComboBox.py文件,代码如下
import sys
from PyQt5.QtWidgets import QDialog,QApplication
from cookbook_200425.demoComboBox import *
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui=Ui_Dialog()
self.ui.setupUi(self)
self.ui.comboBoxAccountType.currentIndexChanged.connect(self.dispAccountType)
self.show()
def dispAccountType(self):
self.ui.labelAccountType.setText("You have selected "+self.ui.comboBoxAccountType.itemText(self.ui.comboBoxAccountType.currentIndex()))
if __name__=="__main__":
app=QApplication(sys.argv)
w=MyForm()
w.show()
sys.exit(app.exec())