使用pyqt5 實(shí)現(xiàn)ComboBox的鼠標(biāo)點(diǎn)擊觸發(fā)事件
一、自定義MyComboBox
# MyComboBox.py
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtCore import pyqtSignal
class MyComboBox(QComboBox):
clicked = pyqtSignal() #創(chuàng)建一個(gè)信號(hào)
def showPopup(self): #重寫(xiě)showPopup函數(shù)
self.clicked.emit() #發(fā)送信號(hào)
super(MyComboBox, self).showPopup() # 調(diào)用父類(lèi)的showPopup()
二、使用MyComboBox創(chuàng)建窗口空間
# test_ui.py
self.PrintersList = MyComboBox(self.groupBox) # 修改后
# self.PrintersList = QtWidgets.QComboBox(self.groupBox) # 修改前
三、main函數(shù)中對(duì)clicked 信號(hào)進(jìn)行綁定
# main_loop.py
self.PrintersList.clicked.connect(self.scan_printer_list_slot) # 信號(hào)與槽函數(shù)的綁定
# 槽函數(shù)的實(shí)現(xiàn)
def scan_printer_list_slot(self):
print("掃描打印機(jī)并刷新列表")
補(bǔ)充:PyQt5中QComboBox實(shí)現(xiàn)多選功能
網(wǎng)上大佬太多了,寫(xiě)的啥沒(méi)看懂,自己摸索著也寫(xiě)了個(gè)出來(lái),也勉強(qiáng)能用。
功能:
QComboBox實(shí)現(xiàn)多選功能
返回選中的文本列表
一鍵全選和取消全選功能
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): #這個(gè)函數(shù)是每次選擇項(xiàng)目時(shí)判斷狀態(tài)時(shí)自動(dòng)調(diào)用的,不用管(自動(dòng)調(diào)用)
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方法可以獲得選擇的項(xiàng)目列表,自動(dòng)調(diào)用。
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 #實(shí)例化的時(shí)候直接調(diào)用這個(gè)self.checkedItems就能獲取到選中的值,不需要調(diào)用這個(gè)方法,方法會(huì)在選擇選項(xiàng)的時(shí)候自動(dòng)被調(diào)用。
def get_all(self): #實(shí)現(xiàn)全選功能的函數(shù)(自動(dòng)調(diào)用)
all_item = self.model().item(0)
for index in range(1,self.count()): #判斷是否是全選的狀態(tài),如果不是,全選按鈕應(yīng)該處于未選中的狀態(tài)
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_())
總結(jié)(用法):
直接實(shí)例化一個(gè)Qcombox
使用ComboBox.addItem方法添加項(xiàng)目
調(diào)用ComboBox.checkedItems的屬性就能獲取到選中的文本列表
內(nèi)置函數(shù)基本都是自動(dòng)的,統(tǒng)統(tǒng)不用管
調(diào)用checkedItems屬性的時(shí)候最后寫(xiě)在ComboBox的槽函數(shù)里,這樣才能獲取到更改后的屬性,不然可能得到的會(huì)是空值。
補(bǔ)充:
定義一個(gè)槽函數(shù)self.get_checkedItems_slot用于獲取更改后的checkedItems屬性,下面三種ComboBox的信號(hào)槽選一種來(lái)用就行,推薦第一種。
ComboBox.activated.connect(self.get_checkedItems_slot) #推薦 ComboBox.highlighted.connect(self.get_checkedItems_slot) ComboBox.currentIndexChanged.connect(self.get_checkedItems_slot)
挺不容易的,網(wǎng)上資料有關(guān)Pyqt太少了,要么是Qt的,要么寫(xiě)得太復(fù)雜,要么沒(méi)講解的,大多是靠自己摸索出來(lái)的。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Django model.py表單設(shè)置默認(rèn)值允許為空的操作
這篇文章主要介紹了Django model.py表單設(shè)置默認(rèn)值允許為空的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python 網(wǎng)絡(luò)編程說(shuō)明
socket 是網(wǎng)絡(luò)連接端點(diǎn)。2009-08-08
Python3中簡(jiǎn)單的文件操作及兩個(gè)簡(jiǎn)單小實(shí)例分享
文件操作是我們?nèi)粘T谑褂胮ython的時(shí)候經(jīng)常會(huì)用到的,下面這篇文章主要給大家介紹了關(guān)于Python3中簡(jiǎn)單的文件操作及兩個(gè)簡(jiǎn)單小實(shí)例的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-06-06
Python使用unicodedata實(shí)現(xiàn)字符串標(biāo)準(zhǔn)化
這篇文章主要來(lái)和大家聊一聊 Python 的一個(gè)內(nèi)置模塊:unicodedata,它是專(zhuān)門(mén)用來(lái)處理 unicode 字符串的,下面就一起來(lái)看看它的用法吧2023-06-06
python搭建簡(jiǎn)易服務(wù)器分析與實(shí)現(xiàn)
本文將介紹python搭建簡(jiǎn)易服務(wù)器實(shí)現(xiàn)步驟,需要了解的朋友可以參考下2012-12-12
Pytest測(cè)試報(bào)告工具Allure的高級(jí)用法
這篇文章介紹了Pytest測(cè)試報(bào)告工具Allure的高級(jí)用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
Pytorch中關(guān)于BatchNorm2d的參數(shù)解釋
這篇文章主要介紹了Pytorch中關(guān)于BatchNorm2d的參數(shù)解釋?zhuān)哂泻芎玫膮⒖純r(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12

