Pyqt5 - Identify who pressed F10 on a Qlineedit

Asked

Viewed 71 times

-3

How do I open a function by pressing the F10 key on a Qlineedit?

Searching found the function "returnPressed" but only works for the "Enter"

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(612, 430)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.lin1 = QtWidgets.QLineEdit(self.centralwidget)
    self.lin1.setGeometry(QtCore.QRect(20, 130, 113, 20))
    self.lin1.setObjectName("lin1")
    self.lin2 = QtWidgets.QLineEdit(self.centralwidget)
    self.lin2.setGeometry(QtCore.QRect(150, 130, 113, 20))
    self.lin2.setObjectName("lin2")
    self.lin3 = QtWidgets.QLineEdit(self.centralwidget)
    self.lin3.setGeometry(QtCore.QRect(290, 130, 113, 20))
    self.lin3.setObjectName("lin3")
    self.lin4 = QtWidgets.QLineEdit(self.centralwidget)
    self.lin4.setGeometry(QtCore.QRect(430, 130, 113, 20))
    self.lin4.setObjectName("lin4")
    MainWindow.setCentralWidget(self.centralwidget)

    # -----------------------------------------------------------------
    self.lin1.returnPressed.connect(self.teste)
    # -----------------------------------------------------------------

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def teste(self):
    print('Tecla ENTER pressionada')

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  • Hello Rafael. Do not use the field "Your answer" to thank, you "thank" marking the answer as correct by clicking on the icon that is next to my answer. If you have any questions please follow the tips by: https://pt.meta.stackoverflow.com/a/1079/3635

1 answer

0

Use the PyQt5.QtWidgets.QShortcut with PyQt5.QtGui.QKeySequence('F10') or with the shortcut PyQt5.QtCore.Qt.Key_F10

Note that to run the shortcut only in the specific widget you need to inform the context, the event will run anywhere in the program (context WindowShortcut).

To specify the context as itself QLineEdit define in attribute:

context=QtCore.Qt.WidgetShortcut

Thus:

PyQt5.QtWidgets.QShortcut(<atalho>, <widget>, <evento>, context=QtCore.Qt.WidgetShortcut)

A very simple example with two text fields with different functions:

from sys import argv, exit
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QMessageBox, QShortcut, QVBoxLayout, QWidget
from PyQt5.QtGui import QKeySequence


class Examplo(QWidget):
    def __init__(self):
        super(Examplo, self).__init__()

        self.textEdit1 = QLineEdit()
        self.textEdit2 = QLineEdit()

        QShortcut(Qt.Key_F10, self.textEdit1, self.eventoFoo, context=Qt.WidgetShortcut)
        QShortcut(Qt.Key_F10, self.textEdit2, self.eventoBar, context=Qt.WidgetShortcut)

        mainLayout = QVBoxLayout()

        mainLayout.addWidget(QLabel("Primeiro label F10"))
        mainLayout.addWidget(self.textEdit1)

        mainLayout.addWidget(QLabel("Segundo label F10"))
        mainLayout.addWidget(self.textEdit2)

        self.setLayout(mainLayout)

        self.setWindowTitle("Exemplo")


    def eventoFoo(self):
        msg = QMessageBox(self)
        msg.setText("#1 eventoFoo() executado")
        msg.show()


    def eventoBar(self):
        msg = QMessageBox(self)
        msg.setText("#2 eventoBar() executado")
        msg.show()


app = QApplication(argv)
exemplo = Examplo()
exemplo.show()
exit(app.exec_())

Browser other questions tagged

You are not signed in. Login or sign up in order to post.