Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pressing enter in my lineedit also presses enter on a button
Forum Updated to NodeBB v4.3 + New Features

Pressing enter in my lineedit also presses enter on a button

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Moschops
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Moschops
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

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

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Moschops
              wrote on last edited by
              #6

              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).

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                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?

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Moschops
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved