2017-09-30 10 views
1

私は無題のuiを作成しました。ここには1つの改行があります。 は今、私はそれがkeypresseventを使用して行うことができるが、私はまさに今LINEEDITにpyqt keypress event in lineedit

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_()) 

これをそれを使用する方法を理解していなかったLINEEDIT私は考え出し、直ちに に入力するものは何でもの値を取得したいです私はあなたがそれが入力されるように、ライン編集で全体テキストを取得したい場合は、あなたがtextEdited信号を使用することができます

# -*- 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_()) 

答えて

1

を輸入してきた私のuntitled.pyコードです。ただし、押された現在のキーを取得するだけの場合は、event-filterを使用できます。 );たくさん、私は実際に私はそれがどのように動作するかを知るようになった今textEdited信号を望んでいたおかげで

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) 
+0

完璧に動作し、あなたは右の私の質問を得た:ここ

は、これらのアプローチの両方を使用する方法です –