diff --git a/qtpy/compat.py b/qtpy/compat.py index 45e11338..c1f1c54b 100644 --- a/qtpy/compat.py +++ b/qtpy/compat.py @@ -7,6 +7,14 @@ """ import sys +from . import ( + PYQT5, + PYQT6, + PYSIDE2, + PYSIDE6, + QtBindingsNotFoundError, +) + from .QtWidgets import QFileDialog @@ -129,3 +137,16 @@ def getsavefilename(parent=None, caption='', basedir='', filters='', caption=caption, basedir=basedir, filters=filters, selectedfilter=selectedfilter, options=options) + +# ============================================================================= +def isalive(object): + """Wrapper around sip.isdeleted and shiboken.isValid which tests whether + an object is currently alive.""" + if PYQT5 or PYQT6: + from . import sip + return sip.isdeleted(object) + elif PYSIDE2 or PYSIDE6: + from . import shiboken + return shiboken.isValid(object) + else: + raise QtBindingsNotFoundError() diff --git a/qtpy/tests/test_compat.py b/qtpy/tests/test_compat.py new file mode 100644 index 00000000..975774a6 --- /dev/null +++ b/qtpy/tests/test_compat.py @@ -0,0 +1,11 @@ +"""Test the compat module.""" + +from qtpy import compat, QtWidgets + +def test_isalive(qtbot): + """Test compat.isalive""" + test_widget = QtWidgets.QWidget() + assert compat.isalive(test_widget) == True + with qtbot.waitSignal(test_widget.destroyed): + test_widget.deleteLater() + assert compat.isalive(test_widget) == False