pyqt5的QComboBox 使用模板

Posted xjt2018

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyqt5的QComboBox 使用模板相关的知识,希望对你有一定的参考价值。

技术分享图片

技术分享图片

  1 import sys
  2 from PyQt5.QtWidgets import *
  3 from PyQt5.QtGui import *
  4 from PyQt5.QtCore import *
  5 ################################################
  6 items_list=["C","C++","Java","Python","javascript","C#","Swift","go","Ruby","Lua","php"]
  7 datas_list=[1972,1983,1995,1991,1992,2000,2014,2009,1995,1993,1995]
  8 
  9 class Widget(QWidget):
 10     def __init__(self, *args, **kwargs):
 11         super(Widget, self).__init__(*args, **kwargs)
 12         layout = QVBoxLayout(self)
 13         self.combobox1 = QComboBox(self, minimumWidth=200)
 14         self.combobox2 = QComboBox(self, minimumWidth=200)
 15         self.combobox3 = QComboBox(self, minimumWidth=200)
 16         self.combobox4 = QComboBox(self, minimumWidth=200)
 17 
 18         layout.addWidget(QLabel("增加单项,不带数据", self))
 19         layout.addWidget(self.combobox1)
 20         layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
 21 
 22         layout.addWidget(QLabel("增加单项,附带数据", self))
 23         layout.addWidget(self.combobox2)
 24         layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
 25 
 26         layout.addWidget(QLabel("增加多项,不带数据", self))
 27         layout.addWidget(self.combobox3)
 28         layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
 29 
 30         layout.addWidget(QLabel("设置模型,不带数据", self))
 31         layout.addWidget(self.combobox4)
 32 
 33         #初始化combobox
 34         self.init_combobox1()
 35         self.init_combobox2()
 36         self.init_combobox3()
 37         self.init_combobox4()
 38 
 39         #增加选中事件
 40         self.combobox1.activated.connect(self.on_combobox1_Activate)
 41         self.combobox2.activated.connect(self.on_combobox2_Activate)
 42         self.combobox3.activated.connect(self.on_combobox3_Activate)
 43         self.combobox4.activated.connect(self.on_combobox4_Activate)
 44 
 45     ####### addItem()  增加单项元素,不带数据  #########
 46     def init_combobox1(self):
 47         for i in range(len(items_list)):
 48             self.combobox1.addItem(items_list[i])
 49         self.combobox1.setCurrentIndex(-1)
 50 
 51     def on_combobox1_Activate(self, index):
 52         print(self.combobox1.count())
 53         print(self.combobox1.currentIndex())
 54         print(self.combobox1.currentText())
 55         print(self.combobox1.currentData())
 56         print(self.combobox1.itemData(self.combobox1.currentIndex()))
 57         print(self.combobox1.itemText(self.combobox1.currentIndex()))
 58         print(self.combobox1.itemText(index))
 59 
 60     ####### addItem()  增加单项元素,附带数据  #########
 61     def init_combobox2(self):
 62         for i in range(len(items_list)):
 63             self.combobox2.addItem(items_list[i],datas_list[i])
 64         self.combobox2.setCurrentIndex(-1)
 65 
 66     def on_combobox2_Activate(self, index):
 67         print(self.combobox2.count())
 68         print(self.combobox2.currentIndex())
 69         print(self.combobox2.currentText())
 70         print(self.combobox2.currentData())
 71         print(self.combobox2.itemData(self.combobox2.currentIndex()))
 72         print(self.combobox2.itemText(self.combobox2.currentIndex()))
 73         print(self.combobox2.itemText(index))
 74 
 75 
 76     ####### addItems()  增加多项元素,不带数据  #########
 77     def init_combobox3(self):
 78         self.combobox3.addItems(items_list)
 79         self.combobox3.setCurrentIndex(-1)
 80 
 81     def on_combobox3_Activate(self, index):
 82         print(self.combobox3.count())
 83         print(self.combobox3.currentIndex())
 84         print(self.combobox3.currentText())
 85         print(self.combobox3.currentData())
 86         print(self.combobox3.itemData(self.combobox3.currentIndex()))
 87         print(self.combobox3.itemText(self.combobox3.currentIndex()))
 88         print(self.combobox3.itemText(index))
 89 
 90 
 91 
 92     ####### setModel()  设置数据模型,不带数据  #########
 93     def init_combobox4(self):
 94         self.tablemodel = QStringListModel(items_list)
 95         self.combobox4.setModel(self.tablemodel)
 96         self.combobox4.setCurrentIndex(-1)
 97 
 98     def on_combobox4_Activate(self, index):
 99         print(self.combobox4.count())
100         print(self.combobox4.currentIndex())
101         print(self.combobox4.currentText())
102         print(self.combobox4.currentData())
103         print(self.combobox4.itemData(self.combobox4.currentIndex()))
104         print(self.combobox4.itemText(self.combobox4.currentIndex()))
105         print(self.combobox4.itemText(index))
106 
107 
108 if __name__ == "__main__":
109     app = QApplication(sys.argv)
110     w = Widget()
111     w.show()
112     sys.exit(app.exec_())

 

以上是关于pyqt5的QComboBox 使用模板的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5 组件之QComboBox

单独设置 QComboBox 菜单项的样式 PyQt5

如何从 QComboBox 中获取所选项目以显示在 PyQt5 的 QTableWidget 中? (QComboBox 有复选框来选择项目)

Pyqt5_QComboBox

Pyqt5 组合框允许搜索但阻止添加

pyqt5可复用代码