Skip to main content
 首页 » 编程设计

python之检测/确认打开 QInputDialog

2024年11月01日10softidea

在对应用程序进行 GUI 测试的测试环境中,会出现这种情况(取决于正在测试的当前配置),提示用户通过 QInputDialog 进行选择。

问题是,这会阻止正在运行的测试,直到有人手动单击“确定”按钮(无需进一步操作)。但是,我不知道如何以编程方式检测此对话框。实际上我不确定是否可行,因为主循环将保留在打开对话框的函数中,直到获得用户确认。

对话框

measure, ok = QtGui.QInputDialog.getItem(self.centralWidget, \ 
              "Select the measure to apply", "Measure:", items, 0, False) 

测试函数

print('--- Button click') 
 
# Invokes some GUI action that in some cases includes a user input prompt via  
# the QInputDialog above 
self.p.TabValid.buttonRun.click()  
 
# I tried these ones, but the program won't reach here 
# because it is stuck as soon as the dialog opens 
print('Widget with focus:', self.app.activeModalWidget()) 
print('Widget with focus:', self.app.focusWidget()) 

请您参考如下方法:

只要事件循环在运行,您的代码就始终处于控制之中。只需在定期计时器上执行一些代码,检查事件小部件是否是 QInputDialog,然后强制接受该输入。使用 qApp.activeWindow 或 qApp.activeModalWidget。