Pressing enter in my lineedit also presses enter on a button



  • I have (inherited from someone else) a dialog with a lineedit, and also a button.

    When I type in the lineedit, and press enter, it seems that the enter keypress also goes to the button. I don't want this to happen. How can I stop it happening?

    It seems to be default behaviour that pressing enter presses this button, but thre are some scary custom parts of the dialog that one can type into (and press enter) and the button does not get pressed.



  • Hi,
    This is not possible to fix IYAM. It's in the signal/slots of the dialog you got. Only if it's event handler is virtual you might be able to overwrite it, but normally it isn't. Get the other person on the phone and fix it there. When a line edit is edited it is very normal to just press enter and only signals from that lineEdit should happen.



  • This is just a guess:
    Could it be necessary that the linedit needs to accept() the keypress. It does not "eat up" the event, so it gets passed to other elements that happily process it again.



  • lineedit doesn't seemto have an accept() function, so that's not going to do it, I think.

    I will see if I can track down the signal/slot that handles the user pressing enter on this dialog.


  • Moderators

    did you define shortcuts in your application for the enter key?



  • I think I'm getting an understanding of this. Maybe. What follows is what I think happens; I would be grateful for corrections.

    When someone presses enter in the lineedit box, usually after typing some words, this causes an event to be created and passed through QApplication::notify. The event is whatever event indicates that the enter key has been pressed.

    This event gets passed to a chain of widgets, and the first widget to deal with it effectively kills it. I think one of the widgets is the lineedit itself, but as I understand it, a standard issue lineedit widget doesn't "kill" the event, and it gets passed upwards.

    So I need to make the lineedit box kill this event. I think I can do this by installing an event filter in the QLineEdit object, using code much like this:
    "http://www.java2s.com/Code/Cpp/Qt/InstalleventfilterforQLineEdit.htm":http://www.java2s.com/Code/Cpp/Qt/InstalleventfilterforQLineEdit.htm
    and have my eventFilter function return true if the key is the ENTER key. By my understanding, this will in effect stop the keypress being bounced any higher, and it will never get as far as pressing the button (which is, I think, the default for pressing enter in thye dialog).


  • Moderators

    just for basic clarification:
    When ENTER key is pressed an event is generated and sent to Qt. Qt then forwards it to the widget which currently has input focus. First the event filters get notified about the event before the actual widgets gets it delivered.
    To prevent event propagation to the parent widget you must "accept":http://qt-project.org/doc/qt-5.0/qtcore/qevent.html#accept the event. The default base-class (QWidget) implementation does nothing, meaning it keeps ignored, thus propagated up to the parent widget.

    You said you have a subclass. Did you reimplement the keyPressEvent handler and forgot to call the base class implementation? So it gets accepted and thus prevented to be further processed?



  • No no no, so far I have no subclass. It's a standard issue, off-the-shelf QLineEdit. When I said at the start that I inherited it, I meant that I inherited the codebase and now it's mine, all mine!

    Presumably what I'm getting at the moment is the default behaviour from a QLineEdit that has ENTER (or actually RETURN) pressed in it; I think I'm getting the returnPressed signal, and I can only surmise the event is also propagated upwards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.