Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Setting up QDialogButtonBox so that no button is pressed on Enter

    General and Desktop
    2
    3
    3344
    Loading More Posts
    • 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.
    • T
      toralf last edited by

      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);

      layout_->addWidget(buttons);

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

      button=buttons->addButton(QDialogButtonBox::Cancel);
      button->setAutoDefault(false);
      button->setDefault(false);@

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

      1 Reply Last reply Reply Quote 0
      • M
        MuldeR last edited by

        Have you tried setting the focus policy of the buttons to:
        @Qt::NoFocus@

        __

        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.

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply Reply Quote 0
        • T
          toralf last edited by

          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);
          button->setDefault(true);
          button->hide();@

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

          1 Reply Last reply Reply Quote 0
          • First post
            Last post