pyqt5的QComboBox 使用模板的具體方法
QComboBox 的常規(guī)使用方法,在這個使用模板里,基本都有了。
QComboBox小部件是一個組合的按鈕和彈出列表。
QComboBox提供了一種向用戶呈現(xiàn)選項列表的方式,其占用最小量的屏幕空間。
組合框是一個顯示當前項目的選擇小部件,可以彈出可選項目列表。組合框可以是可編輯的,允許用戶修改列表中的每個項目。
組合框可以包含圖像以及字符串; 當然insertItem()和setItemText()函數(shù)需要適當重載。對于可編輯組合框,提供了函數(shù)clearEditText(),以清除顯示的字符串而不更改組合框的內(nèi)容。
如果組合框的當前項目發(fā)生更改,則會發(fā)出兩個信號currentIndexChanged()和activated()。無論以編程方式或通過用戶交互完成更改,currentIndexChanged()總是被發(fā)射,而只有當更改是由用戶交互引起時才activated() 。highlighted()信號在用戶突出顯示組合框彈出列表中的項目時發(fā)出。所有三個信號都有兩個版本,一個帶有str參數(shù),另一個帶有int參數(shù)。如果用戶選擇或突出顯示一個圖像,則只會發(fā)出int信號。每當可編輯組合框的文本發(fā)生改變時,editTextChanged()信號就會發(fā)出。
當用戶在可編輯的組合框中輸入一個新的字符串時,該小部件可能會插入它,也可能不會插入它,并且可以將它插入到多個位置。默認策略是InsertAtBottom,但您可以使用setInsertPolicy()更改它。
可以使用QValidator將輸入約束為可編輯的組合框;請參閱setValidator()。默認情況下,接受任何輸入。
例如,可以使用插入函數(shù)insertItem()和insertItems()來填充組合框??梢允褂胹etItemText()更改項目。一個項目可以使用removeItem()來移除,所有項目都可以使用clear()來移除。當前項目的文本由currentText()返回,項目的文本編號使用text()返回。當前項目可以使用setCurrentIndex()來設(shè)置。 count()返回組合框中的項目數(shù);可以用setMaxCount()設(shè)置項目的最大數(shù)量。您可以允許使用setEditable()進行編輯。對于可編輯組合框,您可以使用setCompleter()設(shè)置自動完成,并且用戶是否可以添加重復(fù)項由setDuplicatesEnabled()進行設(shè)置。
QComboBox為其彈出列表使用模型/視圖框架并存儲其項目。默認情況下,QStandardItemModel存儲項目,QListView子類顯示彈出列表。您可以直接訪問模型和視圖(使用model()和view()),但QComboBox還提供了設(shè)置和獲取項目數(shù)據(jù)的函數(shù)(例如,setItemData()和itemText())。您還可以設(shè)置新的模型和視圖(使用setModel()和setView())。對于組合框標簽中的文本和圖標,將使用具有Qt.DisplayRole和Qt.DecorationRole的模型中的數(shù)據(jù)。請注意,您不能通過使用setSelectionMode()來更改view()的SelectionMode。
類歸屬
PyQt5->QtWidgets->QComboBox
繼承關(guān)系
PyQt5->QObject and QPaintDevice->QWidget->QFontComboBox->QComboBox
熟悉一下代碼,直接就可以用了。

【如下代碼,完全復(fù)制,直接運行,即可使用】
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的鼠標點擊事件方法
- python GUI庫圖形界面開發(fā)之PyQt5訪問系統(tǒng)剪切板QClipboard類詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5控件數(shù)據(jù)拖曳Drag與Drop詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5圖片顯示控件QPixmap詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5打開保存對話框QFileDialog詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5輸入對話框QInputDialog詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5單行文本框控件QLineEdit詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5窗口布局控件QStackedWidget詳細使用方法
- python GUI庫圖形界面開發(fā)之PyQt5 UI主線程與耗時線程分離詳細方法實例
- python GUI庫圖形界面開發(fā)之PyQt5中QWebEngineView內(nèi)嵌網(wǎng)頁與Python的數(shù)據(jù)交互傳參詳細方法實例
- python GUI庫圖形界面開發(fā)之PyQt5時間控件QTimer詳細使用方法與實例
- python GUI庫圖形界面開發(fā)之PyQt5窗口控件QWidget詳細使用方法
- python GUI庫圖形界面開發(fā)之PyQt5窗口類QMainWindow詳細使用方法
- python GUI庫圖形界面開發(fā)之PyQt5中QMainWindow, QWidget以及QDialog的區(qū)別和選擇
- python GUI庫圖形界面開發(fā)之PyQt5 Qt Designer工具(Qt設(shè)計師)詳細使用方法及Designer ui文件轉(zhuǎn)py文件方法
- python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細使用方法與實例
相關(guān)文章
使用Anaconda創(chuàng)建Pytorch虛擬環(huán)境的排坑詳細教程
PyTorch是一個開源的Python機器學(xué)習(xí)庫,基于Torch,用于自然語言處理等應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于使用Anaconda創(chuàng)建Pytorch虛擬環(huán)境的相關(guān)資料,需要的朋友可以參考下2022-12-12
Python functools.lru_cache裝飾器性能提升利器深入探究
本文將詳細介紹functools.lru_cache裝飾器的原理、用法以及適當?shù)膱鼍?以幫助你更好地利用這一功能,它可以用來緩存函數(shù)的輸出,以避免重復(fù)計算,從而顯著提高程序的執(zhí)行速度2024-01-01
Python+Selenium實現(xiàn)短視頻自動上傳與發(fā)布的實踐
本文主要介紹了Python+Selenium實現(xiàn)短視頻自動上傳與發(fā)布的實踐,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Python使用pandas對數(shù)據(jù)進行差分運算的方法
今天小編就為大家分享一篇Python使用pandas對數(shù)據(jù)進行差分運算的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Windows10+anacond+GPU+pytorch安裝詳細過程
這篇文章主要介紹了Windows10+anacond+GPU+pytorch安裝詳細過程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

