pyqt5的QComboBox 使用模板的具體方法
QComboBox 的常規(guī)使用方法,在這個使用模板里,基本都有了。
QComboBox小部件是一個組合的按鈕和彈出列表。
QComboBox提供了一種向用戶呈現(xiàn)選項列表的方式,其占用最小量的屏幕空間。
組合框是一個顯示當(dāng)前項目的選擇小部件,可以彈出可選項目列表。組合框可以是可編輯的,允許用戶修改列表中的每個項目。
組合框可以包含圖像以及字符串; 當(dāng)然insertItem()和setItemText()函數(shù)需要適當(dāng)重載。對于可編輯組合框,提供了函數(shù)clearEditText(),以清除顯示的字符串而不更改組合框的內(nèi)容。
如果組合框的當(dāng)前項目發(fā)生更改,則會發(fā)出兩個信號currentIndexChanged()和activated()。無論以編程方式或通過用戶交互完成更改,currentIndexChanged()總是被發(fā)射,而只有當(dāng)更改是由用戶交互引起時才activated() 。highlighted()信號在用戶突出顯示組合框彈出列表中的項目時發(fā)出。所有三個信號都有兩個版本,一個帶有str參數(shù),另一個帶有int參數(shù)。如果用戶選擇或突出顯示一個圖像,則只會發(fā)出int信號。每當(dāng)可編輯組合框的文本發(fā)生改變時,editTextChanged()信號就會發(fā)出。
當(dāng)用戶在可編輯的組合框中輸入一個新的字符串時,該小部件可能會插入它,也可能不會插入它,并且可以將它插入到多個位置。默認(rèn)策略是InsertAtBottom,但您可以使用setInsertPolicy()更改它。
可以使用QValidator將輸入約束為可編輯的組合框;請參閱setValidator()。默認(rèn)情況下,接受任何輸入。
例如,可以使用插入函數(shù)insertItem()和insertItems()來填充組合框。可以使用setItemText()更改項目。一個項目可以使用removeItem()來移除,所有項目都可以使用clear()來移除。當(dāng)前項目的文本由currentText()返回,項目的文本編號使用text()返回。當(dāng)前項目可以使用setCurrentIndex()來設(shè)置。 count()返回組合框中的項目數(shù);可以用setMaxCount()設(shè)置項目的最大數(shù)量。您可以允許使用setEditable()進(jìn)行編輯。對于可編輯組合框,您可以使用setCompleter()設(shè)置自動完成,并且用戶是否可以添加重復(fù)項由setDuplicatesEnabled()進(jìn)行設(shè)置。
QComboBox為其彈出列表使用模型/視圖框架并存儲其項目。默認(rèn)情況下,QStandardItemModel存儲項目,QListView子類顯示彈出列表。您可以直接訪問模型和視圖(使用model()和view()),但QComboBox還提供了設(shè)置和獲取項目數(shù)據(jù)的函數(shù)(例如,setItemData()和itemText())。您還可以設(shè)置新的模型和視圖(使用setModel()和setView())。對于組合框標(biāo)簽中的文本和圖標(biāo),將使用具有Qt.DisplayRole和Qt.DecorationRole的模型中的數(shù)據(jù)。請注意,您不能通過使用setSelectionMode()來更改view()的SelectionMode。
類歸屬
PyQt5->QtWidgets->QComboBox
繼承關(guān)系
PyQt5->QObject and QPaintDevice->QWidget->QFontComboBox->QComboBox
熟悉一下代碼,直接就可以用了。
【如下代碼,完全復(fù)制,直接運(yùn)行,即可使用】
import sys from 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("增加單項,不帶數(shù)據(jù)", self)) layout.addWidget(self.combobox1) layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) layout.addWidget(QLabel("增加單項,附帶數(shù)據(jù)", self)) layout.addWidget(self.combobox2) layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) layout.addWidget(QLabel("增加多項,不帶數(shù)據(jù)", self)) layout.addWidget(self.combobox3) layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) layout.addWidget(QLabel("設(shè)置模型,不帶數(shù)據(jù)", 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() 增加單項元素,不帶數(shù)據(jù) ######### 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() 增加單項元素,附帶數(shù)據(jù) ######### 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() 增加多項元素,不帶數(shù)據(jù) ######### 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() 設(shè)置數(shù)據(jù)模型,不帶數(shù)據(jù) ######### 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_())
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PyQt5重寫QComboBox的鼠標(biāo)點擊事件方法
- python GUI庫圖形界面開發(fā)之PyQt5訪問系統(tǒng)剪切板QClipboard類詳細(xì)使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5控件數(shù)據(jù)拖曳Drag與Drop詳細(xì)使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5圖片顯示控件QPixmap詳細(xì)使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5打開保存對話框QFileDialog詳細(xì)使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5輸入對話框QInputDialog詳細(xì)使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5單行文本框控件QLineEdit詳細(xì)使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5窗口布局控件QStackedWidget詳細(xì)使用方法
- python GUI庫圖形界面開發(fā)之PyQt5 UI主線程與耗時線程分離詳細(xì)方法實例
- python GUI庫圖形界面開發(fā)之PyQt5中QWebEngineView內(nèi)嵌網(wǎng)頁與Python的數(shù)據(jù)交互傳參詳細(xì)方法實例
- python GUI庫圖形界面開發(fā)之PyQt5時間控件QTimer詳細(xì)使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5窗口控件QWidget詳細(xì)使用方法
- python GUI庫圖形界面開發(fā)之PyQt5窗口類QMainWindow詳細(xì)使用方法
- python GUI庫圖形界面開發(fā)之PyQt5中QMainWindow, QWidget以及QDialog的區(qū)別和選擇
- python GUI庫圖形界面開發(fā)之PyQt5 Qt Designer工具(Qt設(shè)計師)詳細(xì)使用方法及Designer ui文件轉(zhuǎn)py文件方法
- python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細(xì)使用方法與實例
相關(guān)文章
python算法學(xué)習(xí)雙曲嵌入論文代碼實現(xiàn)數(shù)據(jù)集介紹
由于雙曲嵌入相關(guān)的文章已經(jīng)有了一系列的代碼。本篇博客主要目的實現(xiàn)最開始的雙曲嵌入論文,將論文中有些直接寫出來的內(nèi)容進(jìn)行了細(xì)節(jié)的推導(dǎo),同時實現(xiàn)對應(yīng)的代碼2021-11-11穩(wěn)扎穩(wěn)打?qū)WPython之容器 可迭代對象 迭代器 生成器專題講解
在剛開始學(xué)Python的時候,是不是經(jīng)常會聽到大佬們在講容器、可迭代對象、迭代器、生成器、列表/集合/字典推導(dǎo)式等等眾多概念,其實這不是大佬們沒事就擱那扯專業(yè)術(shù)語來裝B,而是這些東西都得要明白的,光知道字符串、列表等基礎(chǔ)還是不夠的,尤其是在Python的數(shù)據(jù)結(jié)構(gòu)方面2021-10-10python實現(xiàn)txt文件格式轉(zhuǎn)換為arff格式
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)txt文件格式轉(zhuǎn)換為arff格式的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05python如何實現(xiàn)不可變字典inmutabledict
這篇文章主要介紹了python如何實現(xiàn)不可變字典inmutabledict,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01pandas combine_first函數(shù)處理兩個數(shù)據(jù)集重疊和缺失
combine_first是pandas中的一個函數(shù),它可以將兩個DataFrame對象按照索引進(jìn)行合并,用一個對象中的非空值填充另一個對象中的空值,這個函數(shù)非常適合處理兩個數(shù)據(jù)集有部分重疊和缺失的情況,可以實現(xiàn)數(shù)據(jù)的補(bǔ)全和更新,本文介紹combine_first函數(shù)的語法及一些案例應(yīng)用2024-01-01python十進(jìn)制轉(zhuǎn)二進(jìn)制的詳解
在本篇文章里小編給大家整理了關(guān)于python十進(jìn)制轉(zhuǎn)二進(jìn)制的相關(guān)知識點內(nèi)容,需要的朋友們可以參考學(xué)習(xí)下。2020-02-02