Using pyqt5 to realize the mouse click trigger event of ComboBox

  • 2021-10-11 19:06:16
  • OfStack

1. Customize MyComboBox


# MyComboBox.py
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtCore import pyqtSignal       
class MyComboBox(QComboBox):
  clicked = pyqtSignal()     # Create 1 Signal 
  def showPopup(self):      # Rewrite showPopup Function 
    self.clicked.emit()     # Send a signal 
    super(MyComboBox, self).showPopup()   #  Call the parent class's showPopup()

2. Create window space using MyComboBox


# test_ui.py
    self.PrintersList = MyComboBox(self.groupBox) #  After modification 
    # self.PrintersList = QtWidgets.QComboBox(self.groupBox) #  Before modification 

3. Binding the clicked signal in the main function


# main_loop.py
  self.PrintersList.clicked.connect(self.scan_printer_list_slot)    #  Binding between signal and slot function 
 #  Realization of slot function 
  def scan_printer_list_slot(self):
   print(" Scan the printer and refresh the list ")

Supplement: QComboBox in PyQt5 realizes multi-selection function

There are too many big bosses on the Internet, and I don't understand what I wrote. I groped for it and wrote it, which can barely be used.

Functions:

QComboBox realizes multi-selection function

Returns a list of selected text

1 key to select all and cancel all functions


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):              # This function is automatically called every time the project is selected to determine the status, don't worry (automatic call) 
    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 Method can get a list of selected items and automatically call. 
    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 Is: ",self.checkedItems)
    return self.checkedItems          # Call this directly when instantiating self.checkedItems You can get the selected value without calling this method, which is automatically called when the option is selected. 
 
  def get_all(self):              # Functions that implement the all-select function (automatically called) 
    all_item = self.model().item(0)
 
    for index in range(1,self.count()):    # Determine whether the All button is selected. If not, the All button should be in the unselected state 
      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(" All selection ")
  for i in range(6):
    ComboBox.addItem("Combobox Item " + str(i))
  dialog.show()
  sys.exit(app.exec_())

Summary (usage):

Instantiate 1 Qcombox directly

Add a project using the ComboBox. addItem method

Call the properties of ComboBox. checkedItems to get the selected text list

Built-in functions are basically automatic, so don't worry about them all

When calling checkedItems attribute, it is finally written in the slot function of ComboBox, so as to get the changed attribute, otherwise it may get a null value.

Additional:

Define a slot function self. get_checkedItems_slot is used to obtain the changed checkedItems attribute. Choose one of the following three signal slots of ComboBox, and recommend the first one.


ComboBox.activated.connect(self.get_checkedItems_slot) # Recommend 
ComboBox.highlighted.connect(self.get_checkedItems_slot)
ComboBox.currentIndexChanged.connect(self.get_checkedItems_slot)

It's not easy. There are too few online materials about Pyqt, either Qt, or it is too complicated to write, or it is not explained, and most of them are groped out by themselves.


Related articles: