如何使用Qt聚焦menuBar() [英] How to focus menuBar() with Qt

查看:604
本文介绍了如何使用Qt聚焦menuBar()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效的应用程序.我向带有某些菜单的主窗口添加了menuBar().然后,我将其隐藏以释放屏幕空间.我在下面编写了代码,以便当用户按ALT键时,菜单栏在隐藏时会出现,在显示时会隐藏.

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 !");
        }
    }
}

如您所见,我还添加了一个QMessageBox来查看menuBar何时具有焦点.并且此框仅出现一半的时间.它是这样的:

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. 应用程序已启动,菜单栏已隐藏
  2. 我按ALT,显示菜单栏,没有消息框,没有焦点
  3. 我按ALT键,菜单栏隐藏了
  4. 我按ALT,显示菜单栏,消息框,焦点
  5. 我按ALT键,菜单栏隐藏了
  6. 我按ALT,显示菜单栏,没有消息框,没有焦点
  7. 我按ALT键,菜单栏隐藏了
  8. 我按ALT,显示菜单栏,消息框,焦点

如何确保显示menuBar时始终具有焦点?

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

推荐答案

我想做同样的事情.我的解决方案,完整的示例,作为要点:

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

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

针对Qt 5.9.4进行了测试.

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

这篇关于如何使用Qt聚焦menuBar()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆