为了账号安全,请及时绑定邮箱和手机立即绑定

pyqt5的QComboBox 使用模板

标签:
ZBrush

QComboBox 的常规使用方法,在这个使用模板里,基本都有了。
熟悉一下代码,直接就可以用了。

webp

【如下代码,完全复制,直接运行,即可使用】

import sysfrom PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore import *################################################items_list=["C","C++","Java","Python","JavaScript","C#","Swift","go","Ruby","Lua","PHP"]

datas_list=[1972,1983,1995,1991,1992,2000,2014,2009,1995,1993,1995]################################################class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        super(Widget, self).__init__(*args, **kwargs)
        layout = QVBoxLayout(self)
        self.combobox1 = QComboBox(self, minimumWidth=200)
        self.combobox2 = QComboBox(self, minimumWidth=200)
        self.combobox3 = QComboBox(self, minimumWidth=200)
        self.combobox4 = QComboBox(self, minimumWidth=200)

        layout.addWidget(QLabel("增加单项,不带数据", self))
        layout.addWidget(self.combobox1)
        layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))

        layout.addWidget(QLabel("增加单项,附带数据", self))
        layout.addWidget(self.combobox2)
        layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))

        layout.addWidget(QLabel("增加多项,不带数据", self))
        layout.addWidget(self.combobox3)
        layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))

        layout.addWidget(QLabel("设置模型,不带数据", self))
        layout.addWidget(self.combobox4)        #初始化combobox
        self.init_combobox1()
        self.init_combobox2()
        self.init_combobox3()
        self.init_combobox4()        #增加选中事件
        self.combobox1.activated.connect(self.on_combobox1_Activate)
        self.combobox2.activated.connect(self.on_combobox2_Activate)
        self.combobox3.activated.connect(self.on_combobox3_Activate)
        self.combobox4.activated.connect(self.on_combobox4_Activate)    ####### addItem()  增加单项元素,不带数据  #########
    def init_combobox1(self):
        for i in range(len(items_list)):
            self.combobox1.addItem(items_list[i])
        self.combobox1.setCurrentIndex(-1)    def on_combobox1_Activate(self, index):
        print(self.combobox1.count())
        print(self.combobox1.currentIndex())
        print(self.combobox1.currentText())
        print(self.combobox1.currentData())
        print(self.combobox1.itemData(self.combobox1.currentIndex()))
        print(self.combobox1.itemText(self.combobox1.currentIndex()))
        print(self.combobox1.itemText(index))    ####### addItem()  增加单项元素,附带数据  #########
    def init_combobox2(self):
        for i in range(len(items_list)):
            self.combobox2.addItem(items_list[i],datas_list[i])
        self.combobox2.setCurrentIndex(-1)    def on_combobox2_Activate(self, index):
        print(self.combobox2.count())
        print(self.combobox2.currentIndex())
        print(self.combobox2.currentText())
        print(self.combobox2.currentData())
        print(self.combobox2.itemData(self.combobox2.currentIndex()))
        print(self.combobox2.itemText(self.combobox2.currentIndex()))
        print(self.combobox2.itemText(index))    ####### addItems()  增加多项元素,不带数据  #########
    def init_combobox3(self):
        self.combobox3.addItems(items_list)
        self.combobox3.setCurrentIndex(-1)    def on_combobox3_Activate(self, index):
        print(self.combobox3.count())
        print(self.combobox3.currentIndex())
        print(self.combobox3.currentText())
        print(self.combobox3.currentData())
        print(self.combobox3.itemData(self.combobox3.currentIndex()))
        print(self.combobox3.itemText(self.combobox3.currentIndex()))
        print(self.combobox3.itemText(index))    ####### setModel()  设置数据模型,不带数据  #########
    def init_combobox4(self):
        self.tablemodel = QStringListModel(items_list)
        self.combobox4.setModel(self.tablemodel)
        self.combobox4.setCurrentIndex(-1)    def on_combobox4_Activate(self, index):
        print(self.combobox4.count())
        print(self.combobox4.currentIndex())
        print(self.combobox4.currentText())
        print(self.combobox4.currentData())
        print(self.combobox4.itemData(self.combobox4.currentIndex()))
        print(self.combobox4.itemText(self.combobox4.currentIndex()))
        print(self.combobox4.itemText(index))if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())



作者:一心狮
链接:https://www.jianshu.com/p/c0a3af18e12a


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消