python-3.xeventspyqtsignals-slotsqlineedit

pyqt keypress event in lineedit


i have created a untitled ui where exists one lineedit. now i want to get the value of whatever i type in lineedit immediately i figured out it could be done using keypressevent but i exactly didn't understand how to use it in lineedit now

from untitled import *
from PyQt4 import QtGui # Import the PyQt4 module we'll need
import sys # We need sys so that we can pass argv to QApplication
import os

from PyQt4.QtGui import *
from PyQt4.QtCore import *


class MainWindow(QMainWindow, Ui_Dialog):
    def event(self, event):
        if type(event) == QtGui.QKeyEvent:
             print (event.key())

    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)
        #here i want to get what is keypressed in my lineEdit 

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

this is my untitled.py code that i have imported

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(662, 207)
        self.lineEdit = QtGui.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(50, 30, 113, 27))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

Solution

  • If you want to get the whole text in the line-edit as it is entered, you can use the textEdited signal. However, if you just want to get the current key that was pressed, you can use an event-filter.

    Here is how to use both of these approaches:

    class MainWindow(QMainWindow, Ui_Dialog):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.setupUi(self)
            self.lineEdit.installEventFilter(self)
            self.lineEdit.textEdited.connect(self.showCurrentText)
    
        def eventFilter(self, source, event):
            if (event.type() == QEvent.KeyPress and
                source is self.lineEdit):
                print('key press:', (event.key(), event.text()))
            return super(MainWindow, self).eventFilter(source, event)
    
        def showCurrentText(self, text):
            print('current-text:', text)