Zac Diggum
Zac Diggum

Reputation: 113

How to emit a delayed signal in PyQt GUI?

In my PyQt GUI app, there are a lot of spinBoxes that trigger a lengthy setup routine when their values are changed.

My problem is this: When typing in (large) numbers the spinBox.valueChanged() signal is emitted every time I input a single digit. That leads to the setup function being called many more times than necessary.

Is there a way to delay the trigger until I'm done typing the number and then fire the signal only once? How do you usually take care of that issue in your GUIs?

I found this but I think it would involve creating an extra timer for every unique function I want to call upon emitting. That doesn't seem very elegant.

Upvotes: 2

Views: 1396

Answers (2)

Brendan Abel
Brendan Abel

Reputation: 37599

You can also connect to the editingFinished signal, which isn't emitted until the user presses enter or the spinbox loses focus. However, you'll get this signal even if the value doesn't change, so you may want to check it against the last value to prevent having to run the lengthy routine unnecessarily.

Upvotes: 3

titusjan
titusjan

Reputation: 5546

For spinboxes there is the keyboardTracking property.

If keyboard tracking is disabled, the spinbox doesn't emit the valueChanged() signal while typing. It emits the signal later, when the return key is pressed, when keyboard focus is lost, or when other spinbox functionality is used, e.g. pressing an arrow key.

I believe this solves your issue.

Upvotes: 3

Related Questions