Skip to content

Waiting for a signal with certain arguments #118

@The-Compiler

Description

@The-Compiler

I have some scenarios where I'd like to do something like:

with qtbot.waitSignal(got_line, args=['foo']):
    ...

And qtbot would wait until the signal is emitted with exactly those args.

I also have another case where I do some more sophisticated checking:

    spy = QSignalSpy(self.new_data)
    elapsed_timer = QElapsedTimer()
    elapsed_timer.start()

    while True:
        got_signal = spy.wait(timeout)
        if not got_signal or elapsed_timer.hasExpired(timeout):
            raise WaitForTimeout("Timed out after {}ms waiting for "
                                 "{!r}.".format(timeout, kwargs))

        for args in spy:
            assert len(args) == 1
            line = args[0]

            matches = []

            for key, expected in kwargs.items():
                value = getattr(line, key)
                matches.append(self._match_data(value, expected))

            if all(matches):
                return line

I'm thinking if it makes sense or not to do something like:

def check_args(*args):
    return args == ['foo', 2342]

with qtbot.waitSignal(got_line, callback=check_args):
    ...

Then qtbot will only stop waiting when the callback returned something true.

Maybe args isn't even necessary then, as you could do:

with qtbot.waitSignal(got_line, callback=lambda *args: args == ['foo']):
    ...

What do you think? Neither? Only args? args and callback? Both?

(edited to rename arg_callback to callback as it could be used to check other stuff as well.

Metadata

Metadata

Assignees

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