UmSenhorQualquer/pyforms

How to set focus to specific widget?

Closed this issue · 1 comments

Hi!

Love what you've done with pyforms, it has made it very quick and easy to throw together a functional GUI for a small application I'm working on.

I want to make a usability improvement to the user interface of my application, which would require the application to automatically set focus to a specific widget when a button is pressed. I tried looking around in both the pyforms and Qt documentation but couldn't find anything helpful. Is there a way to do this?
Even better would be the possibility to have the main application window listen to keypresses globally, and trigger a function when e.g. the TAB key is pressed (i.e. overriding/skipping the usual tab key functionality in a GUI).

The application is scanning barcodes using a USB barcode scanner, and the scanner sends a TAB key press after each successful scan, upon which I want to call a function to do a quick database search for the scanned barcode, and then reset the text field and set focus to it again so the scanner can be used again, without having to interact with the keyboard.

Never mind, I worked around it.

Here's the code if anyone is interested:
https://github.com/ctmrbio/list_scanner/blob/master/scan_lists.py#L87-L95
and
https://github.com/ctmrbio/list_scanner/blob/master/scan_lists.py#L114-L131

It bypasses the issue by creating an even handler for Qt keyPressEvents, and disabling the Tab-key functionality (focusNextPrevChild). This means that if the users places focus in a specific widget and then uses Enter/Return or Tab to submit the data, the widget never loses focus.