qt5focuskeypressosx-snow-leopardqmessagebox

Qt5 QMessageBox cannot tab between buttons


I have some code to bring up a QMessageBox, but when it prompts, I am unable to tab between the buttons. It is stuck on the default button.

Keys like Escape and Enter work just fine, but the Tab, Space and Left and Right keys do nothing. I would like to have it so pressing those keys will change focus from one button to another or if Space, will act as Enter.


I figured this would be default behaviour or simple to implement, but I'm not seeing it. I'd prefer not to a) build my own ui form, or b) build out a complex keyPressEvent function, but maybe that's what I'll be stuck with.

Using Qt 5.3.2, qt-creator 3.1.2 and 3.0.1 used, Mac OS X 10.6.8.

    QMessageBox msgBox;
    msgBox.setWindowTitle("This is a prompt");
    msgBox.setIcon(QMessageBox::Question);
    msgBox.layout()->setSizeConstraint(QLayout::SetMaximumSize);
    msgBox.setText(tr("This is some text"));
    msgBox.setInformativeText(tr("Here is some great information for you.\n"));
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    msgBox.setBaseSize(QSize(400, 160));
    msgBox.setFocusPolicy(Qt::StrongFocus);
    int ret = msgBox.exec();

I've tried using setFocusPolicy(Qt::StrongFocus);, but that has not helped.

I was able to get the desired effect from Qt 5.14.1 + qtcreator 4.10.1 on Linux, so I will do more testing. It may be specific to my platform.


Solution

  • Thanks to @VladimirBershov and the answer at https://stackoverflow.com/a/32378759/4149835 for helping me get through this.

    Previously I had called setFocusPolicy(Qt::StrongFocus) on the message box object itself, but instead I do this:

    QMessageBox msgBox;
    msgBox.setWindowTitle("This is a prompt");
    msgBox.setIcon(QMessageBox::Question);
    msgBox.layout()->setSizeConstraint(QLayout::SetMaximumSize);
    msgBox.setText(tr("This is some text"));
    msgBox.setInformativeText(tr("Here is some great information for you.\n"));
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    msgBox.setBaseSize(QSize(400, 160));
    QList<QAbstractButton *> bList = msgBox.buttons();
    for (int i=0; i<bList.count(); i++)
    {
        bList.at(i)->setFocusPolicy(Qt::StrongFocus);
    }
    int ret = msgBox.exec();
    

    And that fixes it! Mac OS X 10.6.8 requires strong focus policies on the buttons themselves to get this functionality.

    EDIT

    Setting focus policy to strong focus does work, but it's not the way that Mac OS X is intended to be used. I read a blog a while back that mentioned you need to go into:

    System Settings -> Keyboard -> Keyboard Shortcuts tab
    

    Then at the bottom, there should be a Full Keyboard Access section that gives you the ability to move keyboard tab focus between "Text boxes and lists only" (default), or "All controls". Setting to All Control means QT can use its default focus policy with OS X correctly.