Beyond accept() reject()

  • I have a dialog that includes an array of buttons that I want to trigger various actions. How can the program that calls the dialog determine exactly which button was pressed? Ideally, I'd like any button press to close the dialog and then drop into something similar to a switch/case : button1, do this, button2, do that, button3 do the other... Right now one button is linked to accept() and another to reject(), which is kind of limiting.

  • Moderators

    Accept is an indication that "some" action is to be taken, while reject means the user wants to cancel or "escape" from the dialog/decision entirely.
    Having said that all the buttons that "do something" should accept and set some flag to indicate which action was chosen.
    You could then check if the dialog was accepted, and if so, enter a switch/case to test the flag and decide which action to take.
    Either that or emit custom signals from the buttons clicks.

  • Maybe you can make use of "finished()": instead of accepted()/rejected(). Just connect your buttons clicked()-signals to a short lambda (or a normal slot, should you want to avoid that) and you should be fine. A custom enum might be be a good idea, though.

  • Got it, thanks. Each button when clicked assigns a value to my return code; each button except for my cancel is connected to the accept() slot. Seems to work fine :)

  • Lifetime Qt Champion


    You might also be interested by the "QDialogButtonBox": class

  • Also note that you can still access your dialog class after the user has made his choice. That means that you can also provide API on it to read any value you need.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.