目录
7)信号——当前选中的索引发生改变currentIndexChanged()
QcomboBox控件就是一个下拉列表,由一个个条目组成,常用方法属性
1、添加条目:
1)逐一添加
控件名.addItem(“python”)
2)批量添加
控件名.addItems(["C","C++","Java"])
3)获取当前选中的索引
控件名.curentIndex()
4)获取当前选中文本
控件名.currentText()
5)根据索引获取文本
控件名.itemText(index)
6)统计条目总数
self.cb.count()7)信号——当前选中的索引发生改变currentIndexChanged()
self.cb.currentIndexChanged.connect(self.select) # 默认会将选中的索引返回 def select(self,i): print(i)
2、完整代码案例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/5/25 16:37
# @Author : @linlianqin
# @Site :
# @File : QcomboBox_learn.py
# @Software: PyCharm
# @description:
from PyQt5.QtWidgets import *
import sys
class QcomboBoxDemo(QWidget):
def __init__(self):
super(QcomboBoxDemo, self).__init__()
self.InitUI()
def InitUI(self):
self.resize(300,30)
self.cb = QComboBox()
self.label = QLabel("请选择编程语言:")
self.cb.addItem("Python")
self.cb.addItem("Java")
self.cb.addItems(["HTML","VB","C","C++"])
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.cb)
self.setLayout(layout)
self.cb.currentIndexChanged.connect(self.select) # 默认会将选中的索引返回
def select(self,i):
for count in range(self.cb.count()):
print(self.cb.itemText(count),":")
print("已选中:",i,",",self.cb.currentText())
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWIn = QcomboBoxDemo()
mainWIn.show()
sys.exit(app.exec_())