PyQt5 控件学习(一个一个学习之QComboBox)
QComboBox的继承图:
QComboBox的描述:
通过下拉框,选择更多的预置选项 !
QComboBox的继承:
它直接继承自 QWidget
QComboBox的功能作用:
QComboBox的功能作用之构造函数:
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): comboBox = QComboBox(self) comboBox.resize(100,30) comboBox.move(100,100) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
QComboBox的功能作用之数据操作:
API之 添加,插入,设置,删除条目项:
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("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.insertItems() ############################设置条目############################### self.comboBox.setItemIcon(2,QIcon("icon/view_off.png")) self.comboBox.setItemText(2,"fsadjffajs") ############################设置条目############################### ############################删除条目项############################### # self.comboBox.removeItem(2) ############################删除条目项############################### ############################插入条目项############################### if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
Api之 插分割线:
设置当前编辑文本:
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.insertSeparator(2) ############################设置分割线############################### ############################设置 当前选项############################### self.comboBox.setCurrentIndex(3) # self.comboBox.setCurrentText() ############################设置 当前选项############################### ############################设置 编辑文本############################### self.comboBox.setEditable(True) self.comboBox.setEditText("zzzzz") #设置文本 ############################设置 编辑文本############################### 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") ############################改变下拉列表的样式 成为树形结构############################### # print(QAbstractItemModel.__subclasses__()) model = QStandardItemModel() ############################QStandardItemModel 的设置############################### item1= QStandardItem("item1") item2= QStandardItem("item2") item22= QStandardItem("item22") item2.appendRow(item22) model.appendRow(item1) model.appendRow(item2) #此时展示不出来,要换个视图才可以 self.comboBox.setView(QTreeView(self.comboBox)) ############################QStandardItemModel 的设置############################### self.comboBox.setModel(model) ############################改变下拉列表的样式############################### 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("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.count())) # btn.clicked.connect(lambda :print(self.comboBox.currentIndex())) # self.comboBox.addItem(QIcon("icon/view.png"),"fajsd",{"name":"tom"}) # btn.clicked.connect(lambda :print(self.comboBox.currentData())) # btn.clicked.connect(lambda :print(self.comboBox.currentText())) # btn.clicked.connect(lambda :print(self.comboBox.itemIcon(self.comboBox.currentIndex()))) # btn.clicked.connect(lambda :btn.setIcon(self.comboBox.itemIcon(self.comboBox.currentIndex()))) #获取最后一个条目的 图标,文本还有对应的数据 btn.clicked.connect(lambda _,idx = self.comboBox.count()-1:print(self.comboBox.itemIcon(idx),self.comboBox.itemText(idx),self.comboBox.itemData(idx))) def test(self): self.comboBox.addItems(["xx1","xx2","xx3"]) self.comboBox.insertItem(1,QIcon("icon/view.png"),"xxx4") ############################常用的数据获取############################### ############################常用的数据获取############################### 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("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) self.comboBox.setMaxVisibleItems(2) #一屏可以显示的个数 ############################数据的限制############################### 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("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.setEditable(True) self.comboBox.setDuplicatesEnabled(True) # 设置可重复 self.comboBox.setFrame(False) #将框架去掉 self.comboBox.setIconSize(QSize(60,60)) ############################尺寸调整策略############################### self.comboBox.setSizeAdjustPolicy(QComboBox.AdjustToContents) #参照最长的长度 ############################尺寸调整策略############################### # self.comboBox.clear() #清空所有项目 # self.comboBox.clearEditText() #清空编辑的文本 ############################常规操作############################### 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): pass def test(self): self.comboBox.addItems(["xx1","xx2","xx3"]) self.comboBox.setEditable(True) self.comboBox.setCompleter(QCompleter(["xx1","xx2","xx3"])) #注意:完成器中的内容一般要和 下拉框中的条目一致 ,这样可以达到快速的匹配 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("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): pass def test(self): self.comboBox.addItems(["xx1","xx2","xx3"]) self.comboBox.setEditable(True) ############################信号############################### #1 self.comboBox.activated.connect(lambda val:print("条目被激活",val)) # self.comboBox.activated.connect(lambda val:print("条目被激活",self.comboBox.itemText(val))) #2 self.comboBox.activated[str].connect(lambda val:print("条目被激活",val)) # 以上两个信号仅仅是和用户交互的时候发射的信号,如果此时用代码改变,它不会发射信号 # 如果也想检测到用代码改变的事件,用下面: #3 self.comboBox.currentIndexChanged.connect(lambda val:print("当前索引发生改变",val)) self.comboBox.currentIndexChanged[str].connect(lambda val:print("当前索引发生改变",val)) #4 # self.comboBox.currentTextChanged.connect(lambda val:print("当前文本发生改变",val)) #编辑的时候改变 #5 # self.comboBox.editTextChanged.connect(lambda val:print("当前编辑文本发生改变",val)) # 它和 4 差不多 #6 高亮发生改变 高亮指的是条目的高亮 (选中哪个条目) self.comboBox.highlighted.connect(lambda val:print("高亮发生改变",val)) ############################信号############################### 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.city_dict= { "HeName":{ "zhengzhou":"001", "kaifeng":"002", "luoyang":"003", "nanyang":"004", }, "JiangXi":{ "nanchang":"005", "fuzhou":"006", "ganzhou":"007", "xinyu":"008", }, "GuangDong":{ "guangzhou":"009", "shenzhen":"010", "zhanjiang":"011", "foshan":"012", } } self.set_ui() def set_ui(self): #1创建两个下拉框控件 pro = QComboBox(self) city = QComboBox(self) self.city = city pro.move(100,100) city.move(200,100) # 2,展示数据到第一个下拉框 pro.addItems(self.city_dict.keys()) #3 监听第一个下拉列表里面的当前值 发生改变的信号 pro.currentIndexChanged[str].connect(self.pro_currentIndexChanged_slot) #5 监听第二个列表中发生改变的信号 city.currentIndexChanged[int].connect(self.city_currentIndexChanged_slot) self.pro_currentIndexChanged_slot(pro.currentText()) def pro_currentIndexChanged_slot(self,pro_name): # print(pro_name) #4,根据第一个的名称来获取对应城市列表 self.city.clear() cities = self.city_dict[pro_name] # self.city.addItems(cities) for key,val in cities.items(): #这种有自己的Userdata 它可用索引获取data self.city.addItem(key,val) def city_currentIndexChanged_slot(self,idx): # print("第二个列表发生改变的时候",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_())
from PyQt5.Qt import * #刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QComboBox 的案例") self.resize(400,400) self.city_dict= { "HeName":{ "zhengzhou":"001", "kaifeng":"002", "luoyang":"003", "nanyang":"004", }, "JiangXi":{ "nanchang":"005", "fuzhou":"006", "ganzhou":"007", "xinyu":"008", }, "GuangDong":{ "guangzhou":"009", "shenzhen":"010", "zhanjiang":"011", "foshan":"012", } } self.set_ui() def set_ui(self): #1创建两个下拉框控件 pro = QComboBox(self) city = QComboBox(self) self.city = city pro.move(100,100) city.move(200,100) #3 监听第一个下拉列表里面的当前值 发生改变的信号 pro.currentIndexChanged[str].connect(self.pro_currentIndexChanged_slot) #5 监听第二个列表中发生改变的信号 city.currentIndexChanged[int].connect(self.city_currentIndexChanged_slot) # 2,展示数据到第一个下拉框 #一定要注意要先连接信号,然后再去添加数据,不然第一个就无法触发 pro.addItems(self.city_dict.keys()) def pro_currentIndexChanged_slot(self,pro_name): # print(pro_name) #4,根据第一个的名称来获取对应城市列表 self.city.blockSignals(True) self.city.clear() # 第二种解决None 产生的方法,当clear 时 ,将信号临时阻断 self.city.blockSignals(False) cities = self.city_dict[pro_name] # self.city.addItems(cities) for key,val in cities.items(): #这种有自己的Userdata 它可用索引获取data self.city.addItem(key,val) def city_currentIndexChanged_slot(self,idx): # print("第二个列表发生改变的时候",idx) userData = self.city.itemData(idx) # if userData != None: #第一种解决None 的方法 # print(userData) 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:print(font)) 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_())
总结:
以上就是输入控件QComboBox 控件,
下面看另一个输入控件的基类:滑块控件 QAbstractSlider (鼠标 ):https://www.cnblogs.com/zach0812/p/11390309.html