How to focus menuBar() with Qt

2019-02-26 05:53发布

I have a working application. I added a menuBar() to the main window with some menus. Then, I hid it to free screen space. I wrote the code below so that when user presses ALT key, the menu bar appears if it's hidden, and it hides if it's displayed.

void MainWindow::keyPressEvent( QKeyEvent *k ) {
    if(k->modifiers() & Qt::AltModifier) {
        menuBar()->setHidden(!menuBar()->isHidden());
        if(menuBar()->hasFocus()) {
            QMessageBox::information(this, "Info", "Focus !");
        }
    }
}

As you can see, I also added a QMessageBox to see when the menuBar has the focus. And this box appears only half of the time. It goes like this :

  1. Application launched, menubar hidden
  2. I press ALT, menubar displayed, no message box, no focus
  3. I press ALT, menubar hidden
  4. I press ALT, menubar displayed, message box, focus
  5. I press ALT, menubar hidden
  6. I press ALT, menubar displayed, no message box, no focus
  7. I press ALT, menubar hidden
  8. I press ALT, menubar displayed, message box, focus
  9. etc.

How to make sure when the menuBar is displayed, it always has focus ?

2条回答
smile是对你的礼貌
2楼-- · 2019-02-26 06:11

I wanted to do the same thing. My solution, complete example, as a gist:

https://gist.github.com/xim/ee56564f425151ea2fa70f730d644873

Tested against Qt 5.9.4.

As it contains a lot of other junk, a minimal example:

class AutoHidingMenuBar : public QMenuBar {
    Q_OBJECT

public:
    AutoHidingMenuBar() : QMenuBar() {
        setMaximumHeight(0);
        connect(qApp, &QApplication::focusChanged, this, &AutoHidingMenuBar::focusChanged);
    }

private slots:
    void focusChanged(QWidget *from, QWidget *to) {
        bool inFocus = hasFocus() || isAncestorOf(focus) || hasFocusedChild();
        if (inFocus && maximumHeight() == 0) {
            auto action = activeAction();
            setMaximumHeight(100);
            if (action) {
                // XXX This is a bit of a hack. We could do
                //   QCoreApplication::processEvents();
                //   setActiveAction(action);
                // with almost the same effect, but then we *open* the first menu on single alt press...
                auto evt = new QMouseEvent(QEvent::MouseMove, actionGeometry(action).center(), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
                QCoreApplication::postEvent(this, evt);
            }
        } else if (!inFocus && maximumHeight() != 0)) {
            setMaximumHeight(0);
        }
    }

private:
    bool hasFocusedChild() {
        QObjectList queue{children()};
        while (!queue.empty()) {
            auto child = queue.takeFirst();
            auto widget = dynamic_cast<QWidget *>(child);
            if (widget && widget->hasFocus())
                return true;

            queue.append(child->children());
        }
        return false;
    }
};
查看更多
Root(大扎)
3楼-- · 2019-02-26 06:24

Have you tried just adding the setFocus command?

void MainWindow::keyPressEvent( QKeyEvent *k ) {
    if(k->modifiers() & Qt::AltModifier) {
        menuBar()->setHidden(!menuBar()->isHidden());
        menuBar()->setFocus(Qt::MenuBarFocusReason);
        if(menuBar()->hasFocus()) {
            QMessageBox::information(this, "Info", "Focus !");
        }
    }
}
查看更多
登录 后发表回答