self.comboBox = QComboBox(self)
self.comboBox.resize(
100
,
30
)
self.comboBox.move(
100
,
100
)
self.test()
def
test
(
self
):
self.comboBox.addItem(
"xx1"
)
self.comboBox.addItem(
"xx2"
)
self.comboBox.addItem(QIcon(
"icon/view.png"
),
"xx3"
)
self.comboBox.addItems([
"xx1"
,
"xx2"
,
"xx3"
])
if
__name__ ==
'__main__'
:
app =QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
添加条目项
插入 (指定位置插入)
设置 (替换)
from
PyQt5.Qt
import
*
import
sys
class
Window
(
QWidget
):
def
__init__
(
self
):
super
().__init__()
self.setWindowTitle(
"QComboBox的学习"
)
self.resize(
400
,
400
)
self.set_ui()
def
set_ui
(
self
):
self.comboBox = QComboBox(self)
self.comboBox.resize(
100
,
30
)
self.comboBox.move(
100
,
100
)
self.test()
def
test
(
self
):
self.comboBox.addItems([
"xx1"
,
"xx2"
,
"xx3"
])
self.comboBox.insertItem(
1
,QIcon(
"icon/view.png"
),
"xxx4"
)
self.comboBox.setItemIcon(
2
,QIcon(
"icon/xiaoxio.png"
))
self.comboBox.setItemText(
3
,
"fsadjffajs"
)
self.comboBox = QComboBox(self)
self.comboBox.resize(
100
,
30
)
self.comboBox.move(
100
,
100
)
self.test()
def
test
(
self
):
self.comboBox.addItems([
"xx1"
,
"xx2"
,
"xx3"
])
self.comboBox.insertSeparator(
2
)
self.comboBox.setCurrentIndex(
3
)
self.comboBox.setEditable(
True
)
self.comboBox = QComboBox(self)
self.comboBox.resize(
200
,
30
)
self.comboBox.move(
100
,
100
)
self.test()
def
test
(
self
):
"""表示把下拉框设置成树型"""
qtv=QTreeView()
self.comboBox.setView(qtv)
model=qtv.model()
self.comboBox.setModel(model)
item1 = QStandardItem(
"item1"
)
item2 = QStandardItem(
"item2"
)
item22 = QStandardItem(
"item22"
)
item23 = QStandardItem(QIcon(
"icon/view.png"
),
"item23"
)
model.appendRow(item1)
model.appendRow(item2)
item2.appendRow(item22)
item2.appendRow(item23)
self.comboBox = QComboBox(self)
self.comboBox.resize(
200
,
30
)
self.comboBox.move(
100
,
100
)
self.test()
def
test
(
self
):
"""表示把下拉框设置成多选形式"""
qw=QListWidget()
self.comboBox.setView(qw)
self.comboBox.setModel(qw.model())
"""1设置第一个选项"""
qItem = QListWidgetItem(qw)
qb1 = QCheckBox()
qb1.setText(
"试试"
)
qw.setItemWidget(qItem, qb1)
"""2设置第二个选项"""
qItem2 = QListWidgetItem(qw)
qb2 = QCheckBox()
qb2.setText(
"再试试"
)
qw.setItemWidget(qItem2, qb2)
comboBox.count() 获取选项数
comboBox.currentIndex() 获取当前选中项索引
comboBox.currentText() 获取当前选中项内容
comboBox.itemIcon(int index) --> QIcon 下面的例子是获取到,然后放到按钮上
comboBox.itemText(4) - 获取指定索引选项
comboBox.itemData(4) 获取指定索引选项
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QComboBox的学习")
self.resize(400,400)
self.set_ui()
def set_ui(self):
self.comboBox = QComboBox(self)
self.comboBox.resize(100,30)
self.comboBox.move(100,100)
self.test()
btn = QPushButton(self)
btn.setText("按钮")
btn.move(0,300)
btn.clicked.connect(lambda :print(self.comboBox.itemText(4)))
btn.clicked.connect(lambda :print(self.comboBox.itemData(4)))
btn.clicked.connect(lambda _,idx = self.comboBox.count()-1:print(self.comboBox.itemIcon(idx)))
last_one=self.comboBox.count()-1
btn.clicked.connect(lambda :print(self.comboBox.itemIcon(last_one)))
def test(self):
self.comboBox.addItems(["xx1","xx2","xx3"])
self.comboBox.insertItem(1,QIcon("icon/view.png"),"xxx4")
self.comboBox.addItem(QIcon("icon/xiaoxio.png"),"fajsd",{"name":"tom"})
if __name__ == '__main__':
app =QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
六、选项限制
.setMaxCount(3)
.maxCount()
选项可见范围个数
.setMaxVisibleItems(10)
.maxVisibleItems()
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QComboBox的学习")
self.resize(400,400)
self.set_ui()
def set_ui(self):
self.comboBox = QComboBox(self)
self.comboBox.resize(100,30)
self.comboBox.move(100,100)
self.test()
btn = QPushButton(self)
btn.setText("按钮")
btn.move(0,300)
btn.clicked.connect(lambda :self.comboBox.addItem("xxx"))
def test(self):
self.comboBox.addItems(["xx1","xx2","xx3"])
self.comboBox.insertItem(1,QIcon("icon/view.png"),"xxx4")
self.comboBox.setMaxCount(3)
print(self.comboBox.maxCount())
self.comboBox.setMaxVisibleItems(10)
print(self.comboBox.maxVisibleItems())
if __name__ == '__main__':
app =QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
七、其他操作
设置为可编辑
允许编辑内容
设置为可重复
设置图标大小
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QComboBox的学习")
self.resize(400,400)
self.set_ui()
def set_ui(self):
self.comboBox = QComboBox(self)
self.comboBox.resize(100,30)
self.comboBox.move(100,100)
self.test()
def test(self):
self.comboBox.addItem(QIcon("icon/view.png"),"xx3")
self.comboBox.addItems(["xx1","xx2","xx3"])
self.comboBox.setEditable(True)
print("是否可编辑",self.comboBox.isEditable())
self.comboBox.setDuplicatesEnabled(True)
print("是否可编辑",self.comboBox.duplicatesEnabled())
print(self.comboBox.hasFrame())
self.comboBox.setIconSize(QSize(60, 60))
if __name__ == '__main__':
app =QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
弹开下拉框
它的用途是,自己自定义一个控制,然后通过点击自定义的也能弹出下拉框
comboBox.showPopup()
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QComboBox的学习")
self.resize(400, 400)
self.set_ui()
def set_ui(self):
self.comboBox = QComboBox(self)
self.comboBox.resize(100, 30)
self.comboBox.move(100, 100)
self.test()
btn = QPushButton(self)
btn.setText("按钮")
btn.move(0, 300)
btn.clicked.connect(self.slot_test)
def slot_test(self):
self.comboBox.showPopup()
def test(self):
self.comboBox.addItems(["xx1", "xx2", "xx3"])
self.comboBox.insertItem(1, QIcon("icon/view.png"), "xxx4")
self.comboBox = QComboBox(self)
self.comboBox.resize(100, 30)
self.comboBox.move(100, 100)
self.test()
btn = QPushButton(self)
btn.setText("清空所有项目")
btn.move(0, 300)
btn.clicked.connect(self.slot_test)
btn1 = QPushButton(self)
btn1.setText("清空编辑的文本")
btn1.move(100, 300)
btn1.clicked.connect(self.slot_test2)
def slot_test(self):
self.comboBox.clear()
def slot_test2(self):
self.comboBox.clearEditText()
def test(self):
self.comboBox.addItems(["xx1", "xx2", "xx3"])
self.comboBox.insertItem(1, QIcon("icon/view.png"), "xxx4")
self.comboBox.setSizeAdjustPolicy(QComboBox.AdjustToContents)
self.comboBox.setEditable(True)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
输入时支持搜索(完成器)
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QComboBox的学习")
self.resize(400,400)
self.set_ui()
def set_ui(self):
self.comboBox = QComboBox(self)
self.comboBox.resize(100,30)
self.comboBox.move(100,100)
self.test()
btn = QPushButton(self)
btn.setText("按钮")
btn.move(0,300)
btn.clicked.connect(self.slot_test)
def slot_test(self):
def test(self):
self.comboBox.addItems(["xx1","xx2","bb3","cc4"])
self.comboBox.setEditable(True)
self.comboBox.setCompleter(QCompleter(["xx1","xx2","bb3"]))
self.comboBox = QComboBox(self)
self.comboBox.resize(100,30)
self.comboBox.move(100,100)
self.test()
btn = QPushButton(self)
btn.setText("按钮")
btn.move(0,300)
btn.clicked.connect(self.slot_test)
def slot_test(self):
def test(self):
self.comboBox.addItems(["xx1","xx2","xx3"])
self.comboBox.setEditable(True)
"""被选中信号(手动选时)"""
self.comboBox.activated.connect(lambda val:print("选项被选中",val))
self.comboBox.activated[str].connect(lambda val:print("选项被选中",val))
"""选中选项发生改变(手动去改变,或代码改变时)"""
self.comboBox.currentIndexChanged.connect(lambda val:print(f"当前索引发生改变变成{val}"))
self.comboBox.currentIndexChanged[str].connect(lambda val:print(f"当前选项值发生改变变成{val}"))
"""当前的文本发生改变时(编辑)"""
self.comboBox.currentTextChanged.connect(lambda val:print("当前文本发生改变",val))
self.comboBox.editTextChanged.connect(lambda val:print("当前编辑文本发生改变",val))
"""高亮发生改变时(悬停时,就改变)"""
self.comboBox.highlighted.connect(lambda val:print("高亮发生改变",val))
if __name__ == '__main__':
app =QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
九、两级下拉选项案例
'版本1'
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QComboBox 的案例")
self.resize(400,400)
self.city_dict= {
"河南":{
"郑州":"001",
"开封":"002",
"洛阳":"003",
"南阳":"004",
"江西":{
"南昌":"005",
"福州":"006",
"乾州":"007",
"上海":"008",
"广东":{
"广州":"009",
"深圳":"010",
"湛江":"011",
"佛山":"012",
self.set_ui()
def set_ui(self):
pro = QComboBox(self)
city = QComboBox(self)
self.city = city
pro.move(100,100)
city.move(200,100)
pro.addItems(self.city_dict.keys())
self.pro_currentIndexChanged_slot(pro.currentText())
pro.currentIndexChanged[str].connect(self.pro_currentIndexChanged_slot)
city.currentIndexChanged[int].connect(self.city_currentIndexChanged_slot)
def pro_currentIndexChanged_slot(self,pro_name):
self.city.clear()
cities = self.city_dict[pro_name]
for key,val in cities.items():
self.city.addItem(key,val)
def city_currentIndexChanged_slot(self,idx):
userData = self.city.itemData(idx)
if userData != None:
print(userData)
if __name__ == '__main__':
app =QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
版本1以上代码存在点问题
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QComboBox 的案例")
self.resize(400,400)
self.city_dict = {
"河南": {
"郑州": "001",
"开封": "002",
"洛阳": "003",
"南阳": "004",
"江西": {
"南昌": "005",
"福州": "006",
"乾州": "007",
"上海": "008",
"广东": {
"广州": "009",
"深圳": "010",
"湛江": "011",
"佛山": "012",
self.set_ui()
def set_ui(self):
pro = QComboBox(self)
city = QComboBox(self)
self.city = city
pro.move(100,100)
city.move(200,100)
pro.currentIndexChanged[str].connect(self.pro_currentIndexChanged_slot)
city.currentIndexChanged[int].connect(self.city_currentIndexChanged_slot)
pro.addItems(self.city_dict.keys())
def pro_currentIndexChanged_slot(self,pro_name):
self.city.blockSignals(True)
self.city.clear()
self.city.blockSignals(False)
cities = self.city_dict[pro_name]
for key,val in cities.items():
self.city.addItem(key,val)
def city_currentIndexChanged_slot(self,idx):
userData = self.city.itemData(idx)
print(userData)
if __name__ == '__main__':
app =QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
十、QComboBox相关子类
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QFontComboBox 的学习")
self.resize(400,400)
self.set_ui()
def set_ui(self):
label = QLabel(self)
label.setText("我爱中国")
label.move(100,100)
fontComboBox = QFontComboBox(self)
fontComboBox.currentFontChanged.connect(lambda font:label.setFont(font))
fontComboBox.setEditable(False)
if __name__ == '__main__':
app =QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
十一、扩展封装多选组件
from PyQt5.QtWidgets import QComboBox, QLineEdit, QListWidget, QCheckBox, QListWidgetItem, QApplication
from PyQt5.QtCore import pyqtSignal
class ComboCheckBox(QComboBox):
1.将show函数改成show_items
2.增加changeitemlist函数
3.增加信号signa
signa = pyqtSignal(list)
def __init__(self):
super(ComboCheckBox, self).__init__()
self.items = ['全部']
self.row_num = len(self.items)
self.Selectedrow_num = 0
self.qCheckBox = []
self.qLineEdit = QLineEdit()
self.qLineEdit.setReadOnly(True)
self.qListWidget = QListWidget()
self.outputlist = []
self.selected_id = []
self.addQCheckBox(0)
self.qCheckBox[0].stateChanged.connect(self.select_all)
for i in range(1, self.row_num):
self.addQCheckBox(i)
self.qCheckBox[i].stateChanged.connect(self.show_selected)
self.setModel(self.qListWidget.model())
self.setView(self.qListWidget)
self.setLineEdit(self.qLineEdit)
self.setMaxVisibleItems(100)
def addQCheckBox(self, i):
self.qCheckBox.append(QCheckBox())
qItem = QListWidgetItem(self.qListWidget)
self.qCheckBox[i].setText(self.items[i])
self.qListWidget.setItemWidget(qItem, self.qCheckBox[i])
def Selectlist(self):
self.outputlist = []
self.selected_id = []
for i in range(1, self.row_num):
if self.qCheckBox[i].isChecked():
self.selected_id.append(i)
self.outputlist.append(self.qCheckBox[i].text())
self.Selectedrow_num = len(self.outputlist)
return self.outputlist
def show_selected(self):
show_items = ''
self.Selectlist()
self.signa.emit(self.outputlist)
self.qLineEdit.setReadOnly(False)
self.qLineEdit.clear()
for item in self.outputlist:
show_items += item + ';'
if self.Selectedrow_num == 0:
self.qCheckBox[0].setCheckState(0)
elif self.Selectedrow_num == self.row_num - 1:
self.qCheckBox[0].setCheckState(2)
else:
self.qCheckBox[0].setCheckState(1)
self.qLineEdit.setText(show_items[:-1])
self.qLineEdit.setReadOnly(True)
def select_all(self, status):
if status == 2:
for i in range(1, self.row_num):
self.qCheckBox[i].setChecked(True)
elif status == 1:
if self.Selectedrow_num == 0:
self.qCheckBox[0].setCheckState(2)
elif status == 0:
self.clear()
def clear(self):
for i in range(self.row_num):
self.qCheckBox[i].setChecked(False)
def change_item_list(self, itemlist):
self.items = itemlist
self.items.insert(0, '全部')
self.row_num = len(self.items)
self.Selectedrow_num = 0
self.qCheckBox = []
self.qLineEdit = QLineEdit()
self.qLineEdit.setReadOnly(True)
self.qListWidget = QListWidget()
self.addQCheckBox(0)
self.qCheckBox[0].stateChanged.connect(self.select_all)
for i in range(1, self.row_num):
self.addQCheckBox(i)
self.qCheckBox[i].stateChanged.connect(self.show_selected)
self.setModel(self.qListWidget.model())
self.setView(self.qListWidget)
self.setLineEdit(self.qLineEdit)
if __name__ == "__main__":
import sys, time
app = QApplication(sys.argv)
mainWindow = ComboCheckBox()
mainWindow.show()
mainWindow.change_item_list(['装置ID1', '传感器ID1', "采样时间1", '装置电源电压1', "信号强度1",'装置ID2', '传感器ID2', "采样时间2", '装置电源电压2', "信号强度3",'装置ID3', '传感器ID3', "采样时间3", '装置电源电压3', "信号强度4"])
sys.exit(app.exec_())
成都_陶木木
粉丝