Assign keyboard shortcut to QLineEdit from Qt Designer Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Assign keyboard shortcut to QLineEdit from Qt Designer without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a form designed in Qt Designer (with PyQt5). I use a line-edit there for entering some input. What I want is to add the ability to use a keyboard combo (something like Ctrl+B or one of the function keys) in the line-edit. Once that combo or function key is pressed, I am going to run a function/method that will paste the clipboard contents in a “special” way (meaning it will filter and adjust the value from the clipboard).

It seems all easy, and a lot of people must have been doing this kind of stuff before. But I wasn’t able to find a single, simple, understandable way of doing what I want. Here is the kind of solution I am looking for. The following is under a class that holds the MainForm code:

self.ui.my_lineedit.keyboard_key_pressed.connect(self.my_func, ctrl+b)

def my_func(self):
    pass

I made up the keyboard_key_pressed part (and others) for you to understand what I’m going for. Is there a straightforward way of doing what I want like the example above?

Answer

The simplest approach would be to use a QShortcut:

self.shortcut = QtWidgets.QShortcut(
    QtCore.QKeySequence('Ctrl+B'), self.ui.my_lineedit, self.my_func)

This will only be activated when the widget has the keyboard focus.

You can also use an event-filter to achieve the same thing. This provides much greater flexibilty, allowing you to listen in on all the events of a given widget and modify the default behaviour (if any) in whatever way you want. This approach can be very useful when working with widgets that you can’t (or would prefer not to) sub-class.

The code would look something like this:

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        ...
        self.ui.my_lineedit.installEventFilter(self)

    def eventFilter(self, source, event):
        if (event.type() == QtCore.QEvent.KeyPress and
            source is self.ui.my_lineedit):
            if (event.modifiers() & QtCore.Qt.ControlModifier and
                event.key() == QtCore.Qt.Key_B):            
                self.my_func()
                # return True here to stop further processing
        return super(MainWindow, self).eventFilter(source, event)

The event here is a QKeyEvent, which (amongst other things) provides information about the specific modifiers and key being pressed.

We are here to answer your question about Assign keyboard shortcut to QLineEdit from Qt Designer - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji