一、自定義MyComboBox
1
2
3
4
5
6
7
8
|
# MyComboBox.py from PyQt5.QtWidgets import QComboBox from PyQt5.QtCore import pyqtSignal class MyComboBox(QComboBox): clicked = pyqtSignal() #創建一個信號 def showPopup( self ): #重寫showPopup函數 self .clicked.emit() #發送信號 super (MyComboBox, self ).showPopup() # 調用父類的showPopup() |
二、使用MyComboBox創建窗口空間
1
2
3
|
# test_ui.py self .PrintersList = MyComboBox( self .groupBox) # 修改后 # self.PrintersList = QtWidgets.QComboBox(self.groupBox) # 修改前 |
三、main函數中對clicked 信號進行綁定
1
2
3
4
5
|
# main_loop.py self .PrintersList.clicked.connect( self .scan_printer_list_slot) # 信號與槽函數的綁定 # 槽函數的實現 def scan_printer_list_slot( self ): print ( "掃描打印機并刷新列表" ) |
補充:PyQt5中QComboBox實現多選功能
網上大佬太多了,寫的啥沒看懂,自己摸索著也寫了個出來,也勉強能用。
功能:
QComboBox實現多選功能
返回選中的文本列表
一鍵全選和取消全選功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
from PyQt5 import QtCore, QtGui, QtWidgets import sys class CheckableComboBox(QtWidgets.QComboBox): def __init__( self , parent = None ): super (CheckableComboBox, self ).__init__(parent) self .setModel(QtGui.QStandardItemModel( self )) self .view().pressed.connect( self .handleItemPressed) self .checkedItems = [] self .view().pressed.connect( self .get_all) self .view().pressed.connect( self .getCheckItem) self .status = 0 def handleItemPressed( self , index): #這個函數是每次選擇項目時判斷狀態時自動調用的,不用管(自動調用) item = self .model().itemFromIndex(index) if item.checkState() = = QtCore.Qt.Checked: item.setCheckState(QtCore.Qt.Unchecked) else : item.setCheckState(QtCore.Qt.Checked) def getCheckItem( self ): # getCheckItem方法可以獲得選擇的項目列表,自動調用。 for index in range ( 1 , self .count()): item = self .model().item(index) if item.checkState() = = QtCore.Qt.Checked: if item.text() not in self .checkedItems: self .checkedItems.append(item.text()) else : if item.text() in self .checkedItems: self .checkedItems.remove(item.text()) print ( "self.checkedItems為:" , self .checkedItems) return self .checkedItems #實例化的時候直接調用這個self.checkedItems就能獲取到選中的值,不需要調用這個方法,方法會在選擇選項的時候自動被調用。 def get_all( self ): #實現全選功能的函數(自動調用) all_item = self .model().item( 0 ) for index in range ( 1 , self .count()): #判斷是否是全選的狀態,如果不是,全選按鈕應該處于未選中的狀態 if self .status = = 1 : if self .model().item(index).checkState() = = QtCore.Qt.Unchecked: all_item.setCheckState(QtCore.Qt.Unchecked) self .status = 0 break if all_item.checkState() = = QtCore.Qt.Checked: if self .status = = 0 : for index in range ( self .count()): self .model().item(index).setCheckState(QtCore.Qt.Checked) self .status = 1 elif all_item.checkState() = = QtCore.Qt.Unchecked: for index in range ( self .count()): if self .status = = 1 : self .model().item(index).setCheckState(QtCore.Qt.Unchecked) self .status = 0 if __name__ = = "__main__" : app = QtWidgets.QApplication(sys.argv) dialog = QtWidgets.QMainWindow() mainWidget = QtWidgets.QWidget() dialog.setCentralWidget(mainWidget) ComboBox = CheckableComboBox(mainWidget) ComboBox.addItem( "全選" ) for i in range ( 6 ): ComboBox.addItem( "Combobox Item " + str (i)) dialog.show() sys.exit(app.exec_()) |
總結(用法):
直接實例化一個Qcombox
使用ComboBox.addItem方法添加項目
調用ComboBox.checkedItems的屬性就能獲取到選中的文本列表
內置函數基本都是自動的,統統不用管
調用checkedItems屬性的時候最后寫在ComboBox的槽函數里,這樣才能獲取到更改后的屬性,不然可能得到的會是空值。
補充:
定義一個槽函數self.get_checkedItems_slot用于獲取更改后的checkedItems屬性,下面三種ComboBox的信號槽選一種來用就行,推薦第一種。
1
2
3
|
ComboBox.activated.connect( self .get_checkedItems_slot) #推薦 ComboBox.highlighted.connect( self .get_checkedItems_slot) ComboBox.currentIndexChanged.connect( self .get_checkedItems_slot) |
挺不容易的,網上資料有關Pyqt太少了,要么是Qt的,要么寫得太復雜,要么沒講解的,大多是靠自己摸索出來的。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/mynameisJW/article/details/109396226