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