Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarification about methods like keyEvent, etc.? #549

Open
Mrodent opened this issue Apr 2, 2024 · 1 comment
Open

Clarification about methods like keyEvent, etc.? #549

Mrodent opened this issue Apr 2, 2024 · 1 comment

Comments

@Mrodent
Copy link

Mrodent commented Apr 2, 2024

First, thanks for this test package, it's a life-saver.

I've been using it for some time now. I just want to simulate pressing of "Enter" on a QLineEdit (for example). There don't seem to be many examples of this kind of thing out there.

In the documentation here we see that something like keyPress or keyEvent is said to be static. (By the way that page appears to list keyPress twice, for no particular reason).

I am currently experimenting with this by going qtbot.keyEvent(...). This appears to work OK. But if that method is static, shouldn't I be using the class instead? If I try qtbot.__class__.keyEvent(...) I get this error:

AttributeError: type object 'MagicMock' has no attribute 'keyEvent'

Also, as a general rule, which should I be using, keyPress or keyEvent? Or doesn't it matter.

@Mrodent Mrodent closed this as not planned Won't fix, can't repro, duplicate, stale Apr 2, 2024
@The-Compiler
Copy link
Member

Looks like they have been marked as such ever since they were introduced some 11 years ago in de0b795.

While they are originally static in Qt (on the QTest object), that doesn't really translate over to pytest-qt. There, qtbot.keyEvent(...) is indeed the proper way to use them. Maybe the signatures were just copied 1:1 from PyQt/PySide at the time, and this was an oversight?

As an aside, if you get AttributeError: type object 'MagicMock' has no attribute 'keyEvent', you're dealing with something from unittest.mock and not the qtbot object. It sounds like you might be using the @unittest.mock.patch decorator incorrectly, and would recommend you use pytest-mock instead, and use its mocker fixture, to avoid this kind of subtle issue.

As for keyPress / keyEvent: keyPress(...) is just a shorthand to say keyEvent(QTest.KeyAction.Press, ...).

(Reopening this as I think the doc bug is legit)

@The-Compiler The-Compiler reopened this Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants