我在一个大型QT/Python应用程序中有大量对话框和向导,但无法确定哪个子类触发父级的exec_方法。有没有使用mock或任何其他库来实现这一点?当然,我可以通过调试找到它,但我想要一种程序化的方式
import mock
from PySide2 import QtWidgets
class CustomDialog(QtWidgets.QDialog):
pass
class AnotherCustomDialog(QtWidgets.QDialog) :
pass
def launch_custom_dialog() :
dlg = CustomDialog()
dlg.exec_()
with mock.patch.object(QtWidgets.QDialog, 'exec_') as mock_dialog:
test_which_calls_launch_custom_dialog()
if mock_dialog.called:
# How do I find the name of the child? I.e. CustomDialog
如果您自己调用它:
def launch_custom_dialog() :
dlg = CustomDialog() # or could be any of your custom dialogs
dlg.exec_()
return dlg
然后你可以创建自己的中间基类:
class CustomDialogBase(QtWidgets.QDialog):
def exec_(self, *args, **kwargs):
self.note_caller = self
return super().exec_(*args, **kwargs)
class CustomDialog(CustomDialogBase):
pass
class AnotherCustomDialog(CustomDialogBase) :
pass
现在您可以检查它是哪个子类:
dlg = launch_custom_dialog()
print(dlg.note_caller)