Skip to content

behaviour with QMessageBox.question and other modal dialogs #18

Closed
@baudren

Description

@baudren

I do not understand how to successfully test the behaviour of modal dialogs, and did not find an example in the documentation -- or anywhere else, for that matter.

Question

How to prevent modal dialogs from poping up in testing, and how do I simulate clicking on their buttons with qtbot?

Sample file

from PySide.QtGui import QMessageBox, QFrame

class Simple(QFrame):
    def query(self):
        self.question = QMessageBox.question(
            self, 'Message', 'Are you sure?',
            QMessageBox.Yes | QMessageBox.No,
            QMessageBox.No)
        if self.question == QMessageBox.Yes:
            self.answer = True
        else:
            self.answer = False

def test_Qt(qtbot):
    simple = Simple()
    qtbot.addWidget(simple)

    simple.query()

What I tried

Because self.question is a QMessageBox.StandardButton, i.e. an enum, it has no method to allow clicking on it. I tried to set directly self.question to QMessageBox.Yes, for instance, but obviously, this does not close the window.

Also, adding self.question to qtbot with addWidget does not work, as it is no real widget.

Could this be due to a wrong design choice on my side? Should the opening of the question be in a separate function, and the rest of the logic would simply take as an input the result of the question. Or am I testing this the wrong way?

Additional but (maybe) related issues

It is seems related to me, but when calling an exec_ method on a customized QtGui.QDialog object, the same problem appears. I can not use the method qtbot.mouseClick on the object before clicking on it manually. Because the object pops out, and interrups the flow of the program.

Of course, this is the whole point of such an object in the main application, but while testing, it looks like it messes things up. Feel free to ignore this part if you deem it too unrelated (I can also open another ticket).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions