Pyside6-第十一篇-QComboBox下拉列表

简介: Pyside6-第十一篇-QComboBox下拉列表

本篇Pyside6的第十一篇,依旧是开启新功能。下拉列表。

QComboBox

class QComboBox(QWidget):
    """ QComboBox(self, parent: Optional[PySide6.QtWidgets.QWidget] = None) -> None """

显而易见,一种创建方式

from PySide6.QtWidgets import QApplication, QWidget, QComboBox
class Box(QWidget):
    def __init__(self):
        super(Box, self).__init__()
        self.setWindowTitle("QComboBox")
        self.setGeometry(300, 300, 700, 400)
        self.UI()
    def UI(self):
        self.com = QComboBox(self)
        self.setGeometry(100, 100, 120, 30)
        self.show()
if __name__ == '__main__':
    app = QApplication([])
    box = Box()
    app.exec()

内容时空的,如何添加内容?

给下拉框添加内容

.addItem()    添加单个内容
.addItems()   添加多个内容
.insertItem()   在指定位置插入单个内容
.insertItems()  在指定位置插入多个内容
from PySide6.QtWidgets import QApplication, QWidget, QComboBox
class Box(QWidget):
    def __init__(self):
        super(Box, self).__init__()
        self.setWindowTitle("QComboBox")
        self.setGeometry(300, 300, 700, 400)
        self.UI()
    def UI(self):
        list_value = ["清安","V: qing_an_an"]
        otherName = ["张三", "李四"]
        self.com = QComboBox(self)
        self.com.setGeometry(100, 100, 120, 30)
        # self.com.addItem("清安")
        self.com.addItems(list_value) 
        self.com.insertItem(3, "王五")    # 在指定位置插入
        self.com.insertItems(1, otherName)  # 在指定位置插入
        self.show()
if __name__ == '__main__':
    app = QApplication([])
    box = Box()
    app.exec()

给下拉内容添加图标

.setIconSize  设置图标宽大小
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QApplication, QWidget, QComboBox
class Box(QWidget):
    def __init__(self):
        super(Box, self).__init__()
        self.setWindowTitle("QComboBox")
        self.setGeometry(300, 300, 700, 400)
        self.UI()
    def UI(self):
        open_close = ["关闭","打开"]
        images = ["close.png","open.png"]
        self.comOpenClose = QComboBox(self)
        self.comOpenClose.setGeometry(150, 150, 120, 30)
        self.comOpenClose.setIconSize(QSize(30,30))
        for k,v in zip(open_close, images):
            print(k,v)
            self.comOpenClose.addItem(QIcon(v),k)
        self.show()
if __name__ == '__main__':
    app = QApplication([])
    box = Box()
    app.exec()

数据获取

.currentText 获取文本
.currentIndex  获取索引位置
.currentData  获取item的userData值
from PySide6.QtWidgets import QApplication, QWidget, QComboBox, \
    QLabel
class Box(QWidget):
    def __init__(self):
        super(Box, self).__init__()
        self.setWindowTitle("QComboBox")
        self.setGeometry(300, 300, 700, 400)
        self.UI()
    def UI(self):
        list_value = ["清安", "V: qing_an_an", "关闭", "打开"]
        self.label = QLabel(self)
        self.label.setGeometry(50, 50, 80, 80)
        self.com = QComboBox(self)
        self.com.setGeometry(100, 100, 120, 30)
        self.com.addItem("姓名","123")
        self.com.addItems(list_value)
        self.com.currentIndex() # 获取索引值
        self.label.setText(self.com.currentText())  # 获取文本并写入
        print(self.com.currentIndex())      # 获取索引位置
        print(self.com.currentData())       # 获取item的userData值
        self.show()
if __name__ == '__main__':
    app = QApplication([])
    box = Box()
    app.exec()

信号与槽

.activated(index: int)
    当用户在组合下拉框中选中一个条目时发射此信号,索引作为参数传递。即使选中项未改变也会发射
.currentIndexChanged(index: int)
    当前索引由用户交互或编程方式改变时发射此信号,索引作为参数传递。
    若combobox为空或当前索引已重置,则传递条目的 index 或 -1
.currentTextChanged(text: str)
    当前文本发生改变时发射此信号,新的值作为参数传递
.editTextChanged(text: str)
    当启用了可编辑模式,且编辑器中的文本发生改变时发射此信号,新的文本作为参数传递
.highlighted(index: int)
    当用户高亮(光标移入或键盘选择)了弹出菜单中的某一条目时发射此信号
    索引值作为参数传递
.textActivated(text: str)
    当用户选择了条目之一时,发射此信号并将文本作为参数传递
    即使选择未发生改变也会发射此信号
.textHighlighted(text: str)
    当用户高亮(光标移入或键盘选择)了弹出菜单中的某一条目时发射此信号
    文本作为参数传递
from PySide6.QtWidgets import QApplication, QWidget, QComboBox, \
    QLabel
class Box(QWidget):
    def __init__(self):
        super(Box, self).__init__()
        self.setWindowTitle("QComboBox")
        self.setGeometry(300, 300, 700, 400)
        self.UI()
    def UI(self):
        self.label = QLabel(self)
        self.label.setGeometry(50, 50, 80, 80)
        list_value = ["清安", "V: qing_an_an", "关闭", "打开"]
        self.com = QComboBox(self)
        self.com.setGeometry(100, 100, 120, 30)
        self.com.addItems(list_value)
        self.com.currentIndexChanged.connect(self.getText)  # 索引变化时触发
        # self.com.currentTextChanged.connect(self.getText)  # 文本变化时触发
        # self.com.highlighted.connect(self.getText)  # 文本变化时触发
        # 当用户高亮(光标移入或键盘选择)了弹出菜单中的某一条目时发射此信号
        # self.com.highlighted.connect(self.getText)
        # 当用户选择了条目之一时,发射此信号并将文本作为参数传递
        # self.com.textActivated.connect(self.getText)
        self.show()
    def getText(self):
        text = self.com.currentText()
        self.label.setText(text)
if __name__ == '__main__':
    app = QApplication([])
    box = Box()
    app.exec()

最后代码地址:

https://gitee.com/qinganan_admin/Pyside6_Information/tree/master/%E7%AC%AC%E4%B8%83%E7%AB%A0%EF%BC%8CQComboBox


目录
相关文章
|
9月前
Pyside6-第四篇-QCheckBox复选框
Pyside6-第四篇-QCheckBox复选框
359 0
Pyside6-第四篇-QCheckBox复选框
|
9月前
Pyside6-第八篇-QLabel文本标签
Pyside6-第八篇-QLabel文本标签
230 0
|
9月前
Pyside6-第七篇-QLineEdit文本行编辑(内设案例)
Pyside6-第七篇-QLineEdit文本行编辑(内设案例)
450 0
|
9月前
Pyside6-第二篇-QPushButton一个普通按钮
Pyside6-第二篇-QPushButton一个普通按钮
163 0
|
9月前
Pyside6-第六篇-各按钮的信号与槽
Pyside6-第六篇-各按钮的信号与槽
147 0
|
9月前
Pyside6-第三篇-QToolButton一个的按钮
Pyside6-第三篇-QToolButton一个的按钮
154 0
|
6月前
PyQt5-QLabel控件是什么?方法有哪些?具体如何使用?
PyQt5-QLabel控件是什么?方法有哪些?具体如何使用?
39 0
|
9月前
Pyside6-第五篇-单选按钮QRadioButton
Pyside6-第五篇-单选按钮QRadioButton
204 0
Qt-网易云音乐界面实现-5 收藏列表,播放列表实现 QListWidget QTableWidget
先上目前完成的内容吧,发现后面越写越多。在看看点击量,心凉凉。
316 0
Qt-网易云音乐界面实现-5 收藏列表,播放列表实现 QListWidget QTableWidget