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. [Solved]How to prevent dialog close when press enter?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 5.4k Views
  • 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 Offline
    W Offline
    wcren2000
    wrote on last edited by
    #1

    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
    0
    • G Offline
      G Offline
      guziemic
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wcren2000
        wrote on last edited by
        #3

        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
        0
        • R Offline
          R Offline
          Rolias
          wrote on last edited by
          #4

          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
          0
          • R Offline
            R Offline
            Rolias
            wrote on last edited by
            #5

            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
            0
            • W Offline
              W Offline
              wcren2000
              wrote on last edited by
              #6

              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
              0
              • R Offline
                R Offline
                Rolias
                wrote on last edited by
                #7

                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
                1

                • Login

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