Important: Please read the Qt Code of Conduct -

QTest.mouseClick does not close a previously opened QComboBox

  • Hi,
    I am trying to simulate mouse clicks using QTest.mouseClick() and this works for the most part but I came across the following issue: Suppose I have a QComboBox with some entries, when I use QTest.mouseClick() on this combo box it opens fine, but then a QTest.mouseClick() on the main window (so outside of the combo box or its entries) does not close the combo box. A normal click with my (hardware) mouse works just fine and closes the combo box.

    Does anybody have an idea why this is or how to fix it? Here is a minimal example that should reproduce this behavior:

    from PySide6.QtCore import Qt, QPoint, QTimer
    from PySide6.QtTest import QTest
    from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QComboBox, QWidget
    class MainWindow(QMainWindow):
        def __init__(self, **kwargs):
            self.setFixedSize(200, 200)
            self.layout = QVBoxLayout()
            self.combobox = QComboBox()
            self.combobox.addItems(["First", "Second", "Third"])
            dummy_widget = QWidget()
            QTimer.singleShot(1000, self._open_combobox)
            QTimer.singleShot(2000, self._try_to_close_combobox)
        def _open_combobox(self):
            QTest.mouseClick(self.combobox, Qt.LeftButton, Qt.NoModifier, QPoint(0, 0))
            print("Clicked on QComboBox")
        def _try_to_close_combobox(self):
            # Click in top left corner of MainWindow
            QTest.mouseClick(self, Qt.LeftButton, Qt.NoModifier, QPoint(0, 0))
            print("Clicked in MainWindow")
    def main():
        app = QApplication()
        main_window = MainWindow()
    if __name__ == '__main__':

    I am using PySide 6.1.3 on Manjaro Linux

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Since you want to do some testing, did you try to build a real unit test with pytest-qt ?

  • Hi,
    no I actually didn't but I am also not directly interested in unit testing. My use case is building a monkey tester that clicks through a Qt application by randomly sampling x and y coordinates and execute clicks there. So i don't want to specify unit tests but let the monkey tester interact with the software for a while. This interaction is also not done by separately calling pytest but in a normal execution of the software. Unfortunately the behavior I described, with not closing the combo box is a bit of a roadblock as this would not occur if I would click myself through the software.

    I also tried using QMouseEvent to do the clicks which again works for opening the combo box, but fails to close it, when the click is then done in the main window.

  • Lifetime Qt Champion

    So something like Squish would do ?

  • Thanks I did not know about this framework, I will take a look.

    But I would much rather use onboard Qt methods. And technically it works with QTest.mouseClick() except for this strange behavior. If somebody knows if this is intended or a bug would help me a lot :)

  • Lifetime Qt Champion

    I understand your motivation to test that way but I would not embed your tester code within your application. Have a separate class that knows the widgets you want to test for example so the monkey can freely do what it wants and you do not need to change your GUI in order to change your monkey behaviour.

    I don't think it's intended. Note that you would avoid clicking at the 0 0 coordinate as it's a corner edge. I would rather click in a zone that is within the widget.

  • The problem is that my whole program is built on that random clicking. The idea of the program is, that a random click is done, then the GUI changes accordingly, a screenshot is taken and then the next click is done and so on. So I am simply searching for a function, ideally in the Qt API, that can click programmatically at a specific coordinate and resembles a real click.

    You are right about the corner edge at 0, 0 but the same behavior is also observed at coordinates inside the widget.

    I also tried debugging why QTest.mouseClick() does not act like a real mouse click. For that I compiled Qt 6.1.3 from source with the command ./configure -debug followed by cmake --build . --parallel and then compiled PySide6 6.1.3 from source against my self compiled qmake.
    I found out that when a real click occurs, the function QT_DEFINE_QPA_EVENT_HANDLER(bool, handleMouseEvent ...) in qtbase/src/gui/kernel/qwindowsysteminterface.cpp (Line 402) is triggered, but for a QTest.mouseClick() that does not happen. Unfortunately I could not yet debug the QTest.mouseClick() function because my debugger in CLion says it cannot reach that code for some reason, but maybe this is a starting point.

    Thanks btw for your interest and respones to my problem :)

  • Lifetime Qt Champion

    @pdeubel said in QTest.mouseClick does not close a previously opened QComboBox:

    The problem is that my whole program is built on that random clicking. The idea of the program is, that a random click is done, then the GUI changes accordingly, a screenshot is taken and then the next click is done and so on.

    That's clear. I was just suggesting to have that part in a dedicated class rather that sprinkling it over the whole code base.

    It might be easier to test/debug with a C++ version of that part.

Log in to reply