官方文档:https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QComboBox.html
使用前导入该组件:
from PySide6.QtWidgets import QComboBox
构造方法
# 创建下拉列表框
QComboBox([_parent=None_])
下拉列表框
常用api
# 添加文字
addItem(text:str)
# 添加图标和文字
addItem(icon:QIcon, text:str)
# 添加多项
addItems(texts:list)
# 清除添加的内容
clear()
# 获得某一行下标的文字内容
itemText(index:int)
# 返回当前选中的行下标
currentIndex()
常用信号
# 当选中的内容变化的时候就会触发
currentIndexChanged(index:int)
例子
from PySide6.QtWidgets import QComboBox,QLabel, QVBoxLayout, QWidget, QApplication
from PySide6.QtGui import QIcon
class Example(QWidget):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout()
self.selected = QLabel()
# 创建combobox
self.choices = QComboBox()
self.choices.addItem("西瓜")
self.choices.addItem("香蕉")
self.choices.addItem(QIcon("../images/python.svg"),"python")
self.choices.addItems(["苹果", "葡萄"])
self.layout.addWidget(self.choices)
self.layout.addWidget(self.selected)
self.setLayout(self.layout)
# 绑定槽函数
self.choices.currentIndexChanged.connect(self.showSelected)
def showSelected(self,index):
self.selected.setText(f"当前选中的行下标为{index},内容为{self.choices.currentText()}")
if __name__ == "__main__":
app = QApplication([])
btnTest = Example()
btnTest.show()
app.exec()