我使用pytest-qt自动测试PyQt GUI。对话框需要作为测试的一部分进行处理(不应该模拟对话框)。
例如,必须处理按钮单击后出现的文件对话框。有两个问题
我尝试了多线程,但是没有用,后来我发现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
它可以使用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)
有关更多详细信息,请参阅此link