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. Is there any ways to disable/enable accept() on a dialog without overriding the function itself?
Forum Updated to NodeBB v4.3 + New Features

Is there any ways to disable/enable accept() on a dialog without overriding the function itself?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.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.
  • F Offline
    F Offline
    FunghiApe
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • AndySA Offline
      AndySA Offline
      AndyS
      Moderators
      wrote on last edited by
      #2

      Hi @FunghiApe,

      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

      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      2
      • F FunghiApe

        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.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @FunghiApe
        Is there a good reason why you do not want to override accept()? There is a reason why it's a virtual slot.

        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.

        F 1 Reply Last reply
        1
        • AndySA AndyS

          Hi @FunghiApe,

          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

          F Offline
          F Offline
          FunghiApe
          wrote on last edited by
          #4

          @AndyS Thank you, these two function do stop Enter/Return keys from triggering anything.

          1 Reply Last reply
          0
          • JonBJ JonB

            @FunghiApe
            Is there a good reason why you do not want to override accept()? There is a reason why it's a virtual slot.

            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.

            F Offline
            F Offline
            FunghiApe
            wrote on last edited by
            #5

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

            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