Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [Solved]How to prevent dialog close when press enter?

    General and Desktop
    3
    7
    4505
    Loading More Posts
    • 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.
    • W
      wcren2000 last edited by

      In my qml app,I have a textfield inside dialog.Default I input something in textfield and press enter, the dialog close by itself. How to prevent dialog close automatically?my code like this:

      @Dialog{
      id:myDialog
      TextField{
      id:
      }
      }@

      1 Reply Last reply Reply Quote 0
      • G
        guziemic last edited by

        Maybe you should try to catch keyboard event (enter) and add empty implementation for it?

        1 Reply Last reply Reply Quote 0
        • W
          wcren2000 last edited by

          Thanks guziemic.I add the onAccepted in Dialog and actually it was called when press enter then I only insert console.debug() but still dialog closed.

          [quote author="guziemic" date="1411463103"]Maybe you should try to catch keyboard event (enter) and add empty implementation for it?[/quote]

          1 Reply Last reply Reply Quote 0
          • R
            Rolias last edited by

            I think there's a bug (which I'll report shortly, if it's not already there).
            You should be able to do something like this:
            @ Dialog{
            id:myDialogId
            TextField{
            id: myTextFieldId
            focus : true
            text: "This is some text"
            Keys.onPressed: {print ("GOT Ya!"); event.accepted = true}
            }
            }@

            Note this is a bit over-aggressive, but done on purpose for testing purposes. It should use @Keys.onReturnPressed@

            to satisfy your requirement.

            Check out my third course in the trilogy on Qt
            "Integrating Qt Quick with C++"
            http://bit.ly/qtquickcpp
            published by Pluralsight

            1 Reply Last reply Reply Quote 0
            • R
              Rolias last edited by

              After further testing in 5.3.2 , it appears to be related to relying on the default property contentItem. Here's a minimal example explicitly using the contentItem property and it behaves the way you want:
              @ Dialog{
              id:myDialogId
              contentItem:
              TextField{
              id: myTextFieldId
              text: "This is some text"
              Keys.onReturnPressed: event.accepted = true
              }
              }@

              Check out my third course in the trilogy on Qt
              "Integrating Qt Quick with C++"
              http://bit.ly/qtquickcpp
              published by Pluralsight

              1 Reply Last reply Reply Quote 0
              • W
                wcren2000 last edited by

                Thanks Rolias.
                The code works.
                Actually my textedit is under three level of recatagnle in the dialog.I will try above in my code.

                1 Reply Last reply Reply Quote 0
                • R
                  Rolias last edited by

                  I filed a bug report and got a very detailed and helpful response from Shawn Rutledge. In case you or anyone else with a similar issue finds this thread here it is:

                  bq. Dialog has buttons, so the intended use case is that the return key presses the OK button and the Escape key presses the Cancel if there is one. We wanted that to be reliable default behavior, so there are multiple ways for those keys to be detected and handled.
                  Otherwise you just need to ensure that the text field has focus; one way is
                  @Component.onCompleted: myTextFieldId.forceActiveFocus()@
                  p<.
                  But maybe you didn't want the buttons... then you can indeed override the contentItem to ensure that only your content exists, and there will be no buttons trying to grab focus.
                  You could set
                  @standardButtons: StandardButton.NoButton@
                  p<.
                  to get rid of the buttons, but it still doesn't give focus to the text field.
                  I didn't anticipate how many people would use Dialog for a generic pop-up window instead of wanting typical dialog features, so in retrospect I'm thinking ButtonBox could have been something separate that you could add to the dialog if you want it. But now we have defined that it is there by default, so we'll have to find other ways to make it easier to remove it. For now, setting contentItem is probably the only way.

                  Check out my third course in the trilogy on Qt
                  "Integrating Qt Quick with C++"
                  http://bit.ly/qtquickcpp
                  published by Pluralsight

                  1 Reply Last reply Reply Quote 1
                  • First post
                    Last post