Question

Using PyQt 4.8 and Python 3.3

I'm using a modified version of this example: whereas this example emits a signal on tab press and adds arbitrary text to the second QLineEdit, I want my script to emit a signal on any keypress, add arbitrary signal text to the 2nd QLineEdit, and add the typed character to the 1st QLineEdit (assuming it's a valid ASCII character).

Whenever I try to use any keypress as a signal, I can no longer grab that text to input into QLineEdit. Here's what I have so far and where I'm stuck:

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

#################################################################### 
def main(): 
    app = QApplication(sys.argv) 
    w = MyWindow() 
    w.show() 
    sys.exit(app.exec_()) 

####################################################################
class MyWindow(QWidget): 
    def __init__(self, *args): 
        QWidget.__init__(self, *args)

        # create objects
        self.la = QLabel("Type in this box:")
        self.le = MyLineEdit()
        self.la2 = QLabel("\nLook here:")
        self.le2 = QLineEdit()
        self.char = MyLineEdit.char # HOW CAN I GET THIS WORKING?

        # layout
        layout = QVBoxLayout()
        layout.addWidget(self.la)
        layout.addWidget(self.le)
        layout.addWidget(self.la2)
        layout.addWidget(self.le2)
        self.setLayout(layout)

        # connections
        self.connect(self.le, SIGNAL("keyPressed"),
                     self.update)

    def update(self):
        newtext1 = self.le.text() + self.char
        newtext2 = self.le2.text() + "kP "
        self.le.setText(newtext1)
        self.le2.setText(newtext2)

####################################################################

class MyLineEdit(QLineEdit):
    def __init__(self, *args):
        QLineEdit.__init__(self, *args)

    def event(self, event):
        if (event.type() == QEvent.KeyPress):
            self.emit(SIGNAL("keyPressed"))
            self.char = "%c" % (event.key())
            return True

        return QLineEdit.event(self, event)

####################################################################
if __name__ == "__main__": 
    main()

Any and all help is greatly appreciated. Is there something within PyQt4 that allows me to use a keypress as both a signal and input text, or is my Python off?

Was it helpful?

Solution

Problem1: you are emitting the signal before setting self.char:

class MyLineEdit(QLineEdit):
    def __init__(self, *args):
        QLineEdit.__init__(self, *args)
        self.char = ""

    def event(self, event):
        if (event.type() == QEvent.KeyPress):
            self.char = "%c" % (event.key()) #this line above the next
            self.emit(SIGNAL("keyPressed"))
            return True

        return QLineEdit.event(self, event)

Problem 2: use the char value in your MyLineEdit object:

    def update(self):
        newtext1 = self.le.text() + self.le.char
        newtext2 = self.le2.text() + "kP "
        self.le.setText(newtext1)
        self.le2.setText(newtext2)

Finally you don't need self.char on MyWindow

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top