1

我正在使用 pytest-qt 来自动化 PyQt GUI 的测试。对话框需要作为测试的一部分进行处理(不应模拟对话框)。

例如,必须处理单击按钮后出现的文件对话框。有2个问题

  1. 在按钮单击命令之后,程序控制转到事件处理程序而不是下一行,我可以尝试将鼠标单击/击键发送到对话框。

  2. 由于 QDialog 没有添加到主窗口小部件中,因此它没有列在主窗口小部件的子窗口中。那么如何获取QDialog的引用呢?

我尝试了多线程,但没有奏效,后来我发现 QObjects 不是线程安全的。

def test_filedialog(qtbot, window):
    qtbot.mouseClick(window.browseButton, QtCore.Qt.LeftButton, delay=1)
    print("After mouse click")
    #This is where I need to get the reference of QDialog and handle it
4

1 回答 1

0

可以使用QTimer.

def test_filedialog(qtbot, window):
    def handle_dialog():
        # get a reference to the dialog and handle it here
    QTimer.singleShot(500, handle_dialog)
    qtbot.mouseClick(window.browseButton, QtCore.Qt.LeftButton, delay=1)

请参阅此链接以获取更多详细信息

于 2019-03-29T16:27:27.980 回答