abhishekgarg
abhishekgarg

Reputation: 1473

Can't type in QlineEdit if the keyPressEvent is on

I am having a problem with the QLineEdit, if i enable the keyPressEvent in the code then i am not able to type anything in the QLineEdit.

class SearchBox(gui.QLineEdit):
    def __init__(self, parent=None):
        super(SearchBox, self).__init__(parent)

        self.setWindowTitle("Explorer")
        self.setGeometry(500,500,400,40)
        font = gui.QFont()
        font.setPointSize(15)
        self.setFont(font)

    # if i disable this function then it works..
    def keyPressEvent(self, event):
        if event.key() == core.Qt.Key_Escape:
            self.close()
        if event.key() == core.Qt.Key_Enter:
            print self.text()
            self.close()

can someone please help me with this.. thanks.

Upvotes: 1

Views: 2390

Answers (1)

Bakuriu
Bakuriu

Reputation: 101909

If you don't call the keyPressEvent of the base class how is it supposed to know that some key was pressed?

def keyPressEvent(self, event):
    if event.key() == core.Qt.Key_Escape:
        self.close()
    if event.key() == core.Qt.Key_Enter:
        print self.text()
        self.close()
    else:
        super(SearchBox, self).keyPressEvent(event)

This is also mentioned in the documentation:

If you reimplement this handler, it is very important that you call the base class implementation if you do not act upon the key.

Upvotes: 2

Related Questions