Important: Please read the Qt Code of Conduct -

Setting up QDialogButtonBox so that no button is pressed on Enter

  • Hi,

    I've been trying to set up a QDialog where buttons are added via QDialogButtonBox so no button is pressed on Enter. I've tried setting default and autoDefault to false, as in

    @ DialogButtonBox *buttons=new QDialogButtonBox(this);


    QPushButton *button=buttons->addButton(QDialogButtonBox::Ok);


    But it didn't really make any difference. Then I noticed the following in the QDialogButtonBox manual:

    bq. If you want a specific button to be default you need to call QPushButton::setDefault() on it yourself. However, if there is no default button set and to preserve which button is the default button across platforms when using the QPushButton::autoDefault property, the first push button with the accept role is made the default button when the QDialogButtonBox is shown,

    So, there is actually no way I can use ButtonBox with StandardButtons and/or a sensible roles setup if I don't want automatic button presses on Enter? Or am I missing something here?

    Just had to ask...

  • Have you tried setting the focus policy of the buttons to:


    Other than that, you could try to inherit your own class from QDialogButtonBox, overwrite the showEvent function and, in that function, use clearFocus() to clear the focus from all the buttons.

  • Setting policy to Qt::NoFocus does not seem to help. I guess overriding showEvent() will, although I haven't tried it yet. I also found that the following trick might work:
    @button=buttons->addButton("", QDialogButtonBox::ActionRole);

    • the idea is to "block" the automatic selection of default button on show.

    Alternatively, I guess I could just add my buttons via a "normal" HBox rather than using ButtonBox, or use ButtonBox but set up an OK button "by hand", and associate it with a different role from AcceptRole

    I don't particularly like any of these solutions.

Log in to reply