Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Is there any ways to disable/enable accept() on a dialog without overriding the function itself?
So the story is, when user input an invalid value, I want to disable the accept() function on the dialog.
At first, I tried disable Ok button from the buttonbox, but Enter/Return key still works?
Is there any way to directly disable accept or disable Enter/Return keys?
Thank you in advance.
This can be done by setting setDefault() and setAutoDefault() on the buttons to how you want it to be. That way the Enter/Return key will not trigger anything as a result. See http://doc.qt.io/qt-5/qpushbutton.html#default-prop
JonB last edited by
Is there a good reason why you do not want to override
accept()? There is a reason why it's a
Doing it your way requires maintaining both the enablement of the button and the keyboard as per @AndyS's solution. You have to switch off & on what the default/auto-default button is as the user types. Is that what you want? It might be simpler to leave the interaction alone and validate the input in the
accept()slot regardless of how the code gets there.
@AndyS Thank you, these two function do stop Enter/Return keys from triggering anything.
@JonB The reason is that the dialog was created from Qt designer as a .ui file, I don't want to rewrite the whole dialog in codes.
My final solution is, over-write the validate() for the input spinbox I wanted to validate. I was using QValidator::Intermediate for the case of invalid input, which will lead to fixup() and tries to modify the input value to make it valid. Now it is changed to QValidator::Invalid, thus the invalid input cannot even be put into the box. And I do not need to worry about execute with a "preview" value user input.