Example #1
0
 def get_active_modal_widget():
     return QApplication.activeModalWidget()
Example #2
0
class ModalTester(object):
    """
    Helper class for testing modal widgets (dialogs).
    """
    def __init__(self, creator, tester):
        """
        Initialise ModalTester.
        :param creator: Function without arguments that creates a modal widget. Can be a class name.
            A modal widget must have exec_() method.
        :param tester: A function taking a widget as its argument that does testing.
        """
        self.app = QApplication.instance()
        if self.app is None:
            self.app = QApplication([''])
        self.creator = creator
        self.tester = tester
        self.widget = None
        self.timer = None
        self.passed = False

    def __call__(self):
        """
        Initialise testing.
        """
        try:
            self.widget = self.creator()
        except:
            traceback.print_exc()
            self.app.exit(0)
        if self.widget is not None:
            self.widget.exec_()

    def _idle(self):
        """
        This function runs every time in QApplication's event loop.
        Call the testing function.
        """
        modal_widget = self.app.activeModalWidget()
        if modal_widget is not None:
            if self.widget is modal_widget:
                try:
                    self.tester(self.widget)
                    self.passed = True
                except:
                    traceback.print_exc()
            if modal_widget.isVisible():
                modal_widget.close()

    def start(self):
        """
        Start this tester.
        """
        self.timer = QTimer()
        # Connecting self.idle to a QTimer with 0 time delay
        # makes it called when there are no events to process
        self.timer.timeout.connect(self._idle)
        self.timer.start()
        # This calls __call__() method
        QTimer.singleShot(0, self)
        # Start the event loop
        self.app.exec_()
Example #3
0
 def get_active_modal_widget():
     return QApplication.activeModalWidget()