自定義實(shí)現(xiàn) PyQt5 下拉復(fù)選框 ComboCheckBox的完整代碼
自定義實(shí)現(xiàn) PyQt5 下拉復(fù)選框 ComboCheckBox
一、前言
由于最近的項(xiàng)目需要具有復(fù)選功能,但過(guò)多的復(fù)選框會(huì)影響界面布局和美觀,因而想到把 PyQt5 的下拉列表和復(fù)選框結(jié)合起來(lái),但在 PyQt5 中并沒(méi)有這樣的組件供我們使用,所以想要自己實(shí)現(xiàn)一個(gè)下拉復(fù)選框,主要就是繼承 QComboBox 類(lèi),然后將復(fù)選框 QCheckBox 加入其中,并實(shí)現(xiàn)相應(yīng)的功能。
最終實(shí)現(xiàn)的下拉復(fù)選框效果如下:

二、代碼實(shí)現(xiàn)
1.主要方法
在 PyQt5 中,有幾個(gè)主要的方法需要了解一下,方法名稱(chēng)和對(duì)應(yīng)的含義如下:
QtWidgets.QComboBox.setView( itemView ) :設(shè)置 組合框彈出窗口中使用的視圖 , 組合框獲取視圖的所有權(quán)。
QtWidgets.QcomboBox.setLineEdit( QLineEdit ) : 設(shè)置組合框 使用 的行 ,而不是當(dāng)前行編輯窗口小部件。
QtWidgets.QListWidget.setItemWidget(item, widget) : 設(shè)置 要在給定的 item 中的 widget 組件 。
2.具體代碼
實(shí)現(xiàn)下拉復(fù)選框的思路為用 setView() 方法將 QComboBox 下拉列表的視圖改為 QListWidget 組件,然后將 QCheckBox 復(fù)選框用在 QListWiget 中,具體代碼如下:
class ComboCheckBox(QComboBox):
def __init__(self, items: list):
"""
initial function
:param items: the items of the list
"""
super(ComboCheckBox, self).__init__()
self.items = items # items list
self.box_list = [] # selected items
self.text = QLineEdit() # use to selected items
self.text.setReadOnly(True)
q = QListWidget()
for i in range(len(self.items)):
self.box_list.append(QCheckBox())
self.box_list[i].setText(self.items[i])
item = QListWidgetItem(q)
q.setItemWidget(item, self.box_list[i])
self.box_list[i].stateChanged.connect(self.show_selected)
self.setLineEdit(self.text)
self.setModel(q.model())
self.setView(q)
def get_selected(self) -> list:
"""
get selected items
:return:
"""
ret = []
for i in range(len(self.items)):
if self.box_list[i].isChecked():
ret.append(self.box_list[i].text())
return ret
def show_selected(self):
"""
show selected items
:return:
"""
self.text.clear()
ret = '; '.join(self.get_selected())
self.text.setText(ret)
其中 show_selected() 用于顯示被選中的內(nèi)容,get_selected() 則用于獲取所有被選中的內(nèi)容并返回。
3.增加全選
要增加全選功能,首先是要在最前面加一個(gè)全選的選擇框,然后為這個(gè)全選的選擇框綁定相應(yīng)的方法,用于實(shí)現(xiàn)全選功能和取消全選功能,具體代碼如下:
def all_selected(self):
"""
decide whether to check all
:return:
"""
# change state
if self.state == 0:
self.state = 1
for i in range(1, len(self.items)):
self.box_list[i].setChecked(True)
else:
self.state = 0
for i in range(1, len(self.items)):
self.box_list[i].setChecked(False)
self.show_selected()
4.修改樣式
由于默認(rèn)的樣式并不美觀,所以我們可以對(duì)控件的樣式進(jìn)行自定義,例如字體大小、字體粗細(xì)等等,例如:
q.setStyleSheet("font-size: 20px; font-weight: bold; height: 40px; margin-left: 5px") self.setStyleSheet("width: 300px; height: 50px; font-size: 21px; font-weight: bold")
三、完整程序
完善后的下拉復(fù)選框的運(yùn)行程序代碼如下:
from PyQt5.QtWidgets import QComboBox, QLineEdit, QListWidgetItem, QListWidget, QCheckBox, \
QApplication, QVBoxLayout, QWidget
import sys
class ComboCheckBox(QComboBox):
def __init__(self, items: list):
"""
initial function
:param items: the items of the list
"""
super(ComboCheckBox, self).__init__()
self.items = ["全選"] + items # items list
self.box_list = [] # selected items
self.text = QLineEdit() # use to selected items
self.state = 0 # use to record state
q = QListWidget()
for i in range(len(self.items)):
self.box_list.append(QCheckBox())
self.box_list[i].setText(self.items[i])
item = QListWidgetItem(q)
q.setItemWidget(item, self.box_list[i])
if i == 0:
self.box_list[i].stateChanged.connect(self.all_selected)
else:
self.box_list[i].stateChanged.connect(self.show_selected)
q.setStyleSheet("font-size: 20px; font-weight: bold; height: 40px; margin-left: 5px")
self.setStyleSheet("width: 300px; height: 50px; font-size: 21px; font-weight: bold")
self.text.setReadOnly(True)
self.setLineEdit(self.text)
self.setModel(q.model())
self.setView(q)
def all_selected(self):
"""
decide whether to check all
:return:
"""
# change state
if self.state == 0:
self.state = 1
for i in range(1, len(self.items)):
self.box_list[i].setChecked(True)
else:
self.state = 0
for i in range(1, len(self.items)):
self.box_list[i].setChecked(False)
self.show_selected()
def get_selected(self) -> list:
"""
get selected items
:return:
"""
ret = []
for i in range(1, len(self.items)):
if self.box_list[i].isChecked():
ret.append(self.box_list[i].text())
return ret
def show_selected(self):
"""
show selected items
:return:
"""
self.text.clear()
ret = '; '.join(self.get_selected())
self.text.setText(ret)
class UiMainWindow(QWidget):
def __init__(self):
super(UiMainWindow, self).__init__()
self.setWindowTitle('Test')
self.resize(600, 400)
combo = ComboCheckBox(["Python", "Java", "Go", "C++", "JavaScript", "PHP"])
layout = QVBoxLayout()
layout.addWidget(combo)
self.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = UiMainWindow()
ui.show()
sys.exit(app.exec_())
總結(jié)
到此這篇關(guān)于自定義實(shí)現(xiàn) PyQt5 下拉復(fù)選框 ComboCheckBox的完整代碼的文章就介紹到這了,更多相關(guān)PyQt5 下拉復(fù)選框 ComboCheckBox內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python爬蟲(chóng)beautiful?soup的使用方式
這篇文章主要介紹了python爬蟲(chóng)beautiful?soup的使用方式,Beautiful?Soup依據(jù)給定的解釋器來(lái)解析html文檔,其依據(jù)html中標(biāo)簽把html文檔在內(nèi)存中轉(zhuǎn)化為類(lèi)似于二叉樹(shù)的數(shù)據(jù)結(jié)構(gòu),并通過(guò)實(shí)現(xiàn)的查詢(xún)方法來(lái)查詢(xún)二叉樹(shù)以得到我們想要的爬蟲(chóng)數(shù)據(jù)2022-08-08
一文弄懂Pytorch的DataLoader, DataSet, Sampler之間的關(guān)系
這篇文章主要介紹了一文弄懂Pytorch的DataLoader, DataSet, Sampler之間的關(guān)系,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
基于python連接oracle導(dǎo)并出數(shù)據(jù)文件
這篇文章主要介紹了基于python連接oracle導(dǎo)并出數(shù)據(jù)文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
數(shù)據(jù)驅(qū)動(dòng)測(cè)試DDT之Selenium讀取Excel文件
這篇文章主要為大家介紹了數(shù)據(jù)驅(qū)動(dòng)測(cè)試DDT之Selenium讀取Excel文件,2021-11-11
python實(shí)現(xiàn)在sqlite動(dòng)態(tài)創(chuàng)建表的方法
這篇文章主要介紹了python實(shí)現(xiàn)在sqlite動(dòng)態(tài)創(chuàng)建表的方法,涉及Python操作SQLite數(shù)據(jù)庫(kù)創(chuàng)建數(shù)據(jù)表的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05
利用Python半自動(dòng)化生成Nessus報(bào)告的方法
這篇文章主要介紹了利用Python半自動(dòng)化生成Nessus報(bào)告的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
python?pandas處理excel表格數(shù)據(jù)的常用方法總結(jié)
在計(jì)算機(jī)編程中,pandas是Python編程語(yǔ)言的用于數(shù)據(jù)操縱和分析的軟件庫(kù),下面這篇文章主要給大家介紹了關(guān)于python?pandas處理excel表格數(shù)據(jù)的常用方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07

