PyQt5 overrides QComboBox's mouse click event method

  • 2021-07-01 07:49:43
  • OfStack

I recently learned PyQt5, Want to be a serial debugging assistant to practice your hands, Serial port upper computer with punctual atoms was used before, I think it's great to click ComboBox to automatically detect serial port. I also want to add this function when writing serial debugging assistant with QT5 before. However, 1 straight failed, and then it went away. Now, after using PyQt, I think 1 must realize this function. After Baidu saw a lot of data, I didn't find a direct solution, but everyone emphasized rewriting the mouse click event, and then I finally wrote it slowly.

My development environment is PyCharm+Python3.6 +PyQt5.9. 2

1. I won't write anything about establishing an engineering creation interface. Baidu 1 has a lot of emphasis on rewriting the showPopup function in QComboBox. The interface file I generated is MainWidget.py.

2. I want to maximize the function of QTDesigner. I saw that one netizen rewrote it before laying it out. I think this is very troublesome, so my approach is to do a good job in the layout, convert it into py file, modify the code inside, and the layout will not be released. It is very dregs, and look at the code directly.


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtCore import pyqtSignal  # Import this module to create signals 

class MyComboBox(QComboBox):
  popupAboutToBeShown = pyqtSignal()   # Create 1 Signal 
  # def mousePressEvent(self, QMouseEvent):# This is the rewrite mouse click event 
  #   self.popupAboutToBeShown.emit()
  def showPopup(self):          # Rewrite showPopup Function 
    self.popupAboutToBeShown.emit()   # Send a signal 

In this way, the rewriting is completed. I thought about rewriting the mouse click event before, that is, rewriting the mouse click event in the above code, but after experiments, the drop-down list will not pop up after rewriting this function. Later, I studied 1. In QComboBox, it should be the mouse click event that triggered the drop-down function showPopup, so I rewrote showPopup instead.

You can see by looking at the QT help documentation


virtual void showPopup()

The virtual keyword is used to modify a method, property, indexer, or event declaration so that they can be overridden in a derived class.

3. The next step is to modify the code for establishing the interface. Only one line needs to be modified. The original code is as follows


self.ComBox = QtWidgets.QComboBox(self.splitter)

The original code is to call the QComboBox class, that is, the showPopup function has not been modified, and it is changed to


self.ComBox = MyComboBox(self.splitter)

This is a call to the rewritten class, and the other QComboBox controls will function normally without modification.

Finally bind the signal to the slot


self.ComBox.popupAboutToBeShown.connect(Form.refresh)

The refresh slot function is defined in the newly created py file


Related articles: