自定義實現(xiàn) PyQt5 下拉復選框 ComboCheckBox的完整代碼
自定義實現(xiàn) PyQt5 下拉復選框 ComboCheckBox
一、前言
由于最近的項目需要具有復選功能,但過多的復選框會影響界面布局和美觀,因而想到把 PyQt5 的下拉列表和復選框結合起來,但在 PyQt5 中并沒有這樣的組件供我們使用,所以想要自己實現(xiàn)一個下拉復選框,主要就是繼承 QComboBox 類,然后將復選框 QCheckBox 加入其中,并實現(xiàn)相應的功能。
最終實現(xiàn)的下拉復選框效果如下:
二、代碼實現(xiàn)
1.主要方法
在 PyQt5 中,有幾個主要的方法需要了解一下,方法名稱和對應的含義如下:
QtWidgets.QComboBox.setView( itemView ) :設置 組合框彈出窗口中使用的視圖 , 組合框獲取視圖的所有權。
QtWidgets.QcomboBox.setLineEdit( QLineEdit ) : 設置組合框 使用 的行 ,而不是當前行編輯窗口小部件。
QtWidgets.QListWidget.setItemWidget(item, widget) : 設置 要在給定的 item 中的 widget 組件 。
2.具體代碼
實現(xiàn)下拉復選框的思路為用 setView() 方法將 QComboBox 下拉列表的視圖改為 QListWidget 組件,然后將 QCheckBox 復選框用在 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()
用于顯示被選中的內容,get_selected()
則用于獲取所有被選中的內容并返回。
3.增加全選
要增加全選功能,首先是要在最前面加一個全選的選擇框,然后為這個全選的選擇框綁定相應的方法,用于實現(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.修改樣式
由于默認的樣式并不美觀,所以我們可以對控件的樣式進行自定義,例如字體大小、字體粗細等等,例如:
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")
三、完整程序
完善后的下拉復選框的運行程序代碼如下:
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_())
總結
到此這篇關于自定義實現(xiàn) PyQt5 下拉復選框 ComboCheckBox的完整代碼的文章就介紹到這了,更多相關PyQt5 下拉復選框 ComboCheckBox內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
一文弄懂Pytorch的DataLoader, DataSet, Sampler之間的關系
這篇文章主要介紹了一文弄懂Pytorch的DataLoader, DataSet, Sampler之間的關系,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07基于python連接oracle導并出數(shù)據(jù)文件
這篇文章主要介紹了基于python連接oracle導并出數(shù)據(jù)文件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04數(shù)據(jù)驅動測試DDT之Selenium讀取Excel文件
這篇文章主要為大家介紹了數(shù)據(jù)驅動測試DDT之Selenium讀取Excel文件,2021-11-11python實現(xiàn)在sqlite動態(tài)創(chuàng)建表的方法
這篇文章主要介紹了python實現(xiàn)在sqlite動態(tài)創(chuàng)建表的方法,涉及Python操作SQLite數(shù)據(jù)庫創(chuàng)建數(shù)據(jù)表的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-05-05python?pandas處理excel表格數(shù)據(jù)的常用方法總結
在計算機編程中,pandas是Python編程語言的用于數(shù)據(jù)操縱和分析的軟件庫,下面這篇文章主要給大家介紹了關于python?pandas處理excel表格數(shù)據(jù)的常用方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07