Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QTest.mouseClick does not close a previously opened QComboBox
Forum Updated to NodeBB v4.3 + New Features

QTest.mouseClick does not close a previously opened QComboBox

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 2 Posters 977 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pdeubel
    wrote on last edited by
    #1

    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):
            super().__init__(**kwargs)
    
            self.setFixedSize(200, 200)
            self.layout = QVBoxLayout()
    
            self.combobox = QComboBox()
            self.combobox.addItems(["First", "Second", "Third"])
    
            self.layout.addWidget(self.combobox)
    
            dummy_widget = QWidget()
            dummy_widget.setLayout(self.layout)
            self.setCentralWidget(dummy_widget)
    
            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()
        main_window.show()
        app.exec()
    
    
    if __name__ == '__main__':
        main()
    

    I am using PySide 6.1.3 on Manjaro Linux

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pdeubel
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So something like Squish would do ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pdeubel
            wrote on last edited by
            #5

            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 :)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pdeubel
                wrote on last edited by pdeubel
                #7

                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 :)

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved