0

I need to take what number is typed into a QLineEdit object and assign it to a variable x when the enter key is pressed. How can I do this with the new signals and slots mechanism that was implemented in Qt 4.5 ?

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):
        data1 = QtGui.QLineEdit('', self)
        data1.move(10, 13)

        self.setGeometry(300, 300, 250, 45)
        self.setWindowTitle('Absolute')    
        self.show()

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
Elazar
  • 20,415
  • 4
  • 46
  • 67
Jkallus
  • 431
  • 1
  • 4
  • 16

2 Answers2

2

You can do it using EventFilter:

Example:

class eventFilter(QtCore.QObject):

    def eventFilter(self,  obj,  event):
        if event.type() == QtCore.QEvent.KeyPress:
            print 'Key: %s' % event.key()

        return obj.eventFilter(obj, event);

and install it to lineedit

filter = eventFilter(data1)
data1.installEventFilter(filter)

NOTE:

don't forget import QtCore module

0

Look QLineEdit::returnPressed() signal.

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61