Exemplo n.º 1
0
class LessLeakyTests(TestCase):
    def setUp(self):
        self.qapp = QCoreApplication.instance() or QCoreApplication()
        self.test_qobj = QObject()
        self.addCleanup(self.release_qt_resources)

    def release_qt_resources(self):
        self.test_qobj.deleteLater()
        self.qapp.sendPostedEvents(event_type=QEvent.DeferredDelete)
        self.qapp.processEvents()

    def _single_shot(self, timeout_ms: int, func: Callable[[], Any]):
        timer = QTimer(parent=self.test_qobj)
        timer.setSingleShot(True)
        timer.setInterval(timeout_ms)
        timer.timeout.connect(func)
        timer.start()

    def test_one(self):
        def do_task():
            # This would be the main test logic, running in the qapp's event loop
            # Pretend that the test takes about 3 seconds and at the end of which quit() is called to signify a successful test
            self._single_shot(3000, self.qapp.quit)

        self._single_shot(0, do_task)
        self._fail_if_timeout()
        self.assertEqual(0, self.qapp.exec_())

    def test_two(self):
        def do_task():
            # This test takes about 3 seconds as well before succeeding
            self._single_shot(3000, self.qapp.quit)

        self._single_shot(0, do_task)
        self._fail_if_timeout()
        self.assertEqual(0, self.qapp.exec_())

    def _fail_if_timeout(self):
        self._single_shot(5000, lambda: self.qapp.exit(-1))
Exemplo n.º 2
0
 def testCase(self):
     o = QObject()
     o.deleteLater()
     del o
     QTimer.singleShot(100, self.app.quit)
     self.app.exec_()
Exemplo n.º 3
0
 def testCase(self):
     o = QObject()
     o.deleteLater()
     del o
     QTimer.singleShot(100, self.app.quit)
     self.app.exec_()
Exemplo n.º 4
0
def safeDelete(obj: QObject) -> None:
    """ Calls deleteLater() on a QObject, doing nothing if the object was already deleted """
    if obj and shiboken2.isValid(obj):
        obj.deleteLater()