<tfoot id='uqICC'></tfoot>
    • <bdo id='uqICC'></bdo><ul id='uqICC'></ul>
    <i id='uqICC'><tr id='uqICC'><dt id='uqICC'><q id='uqICC'><span id='uqICC'><b id='uqICC'><form id='uqICC'><ins id='uqICC'></ins><ul id='uqICC'></ul><sub id='uqICC'></sub></form><legend id='uqICC'></legend><bdo id='uqICC'><pre id='uqICC'><center id='uqICC'></center></pre></bdo></b><th id='uqICC'></th></span></q></dt></tr></i><div id='uqICC'><tfoot id='uqICC'></tfoot><dl id='uqICC'><fieldset id='uqICC'></fieldset></dl></div>
      <legend id='uqICC'><style id='uqICC'><dir id='uqICC'><q id='uqICC'></q></dir></style></legend>

      1. <small id='uqICC'></small><noframes id='uqICC'>

      2. 如何使用 Qt 聚焦 menuBar()

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

            <i id='E6Pd5'><tr id='E6Pd5'><dt id='E6Pd5'><q id='E6Pd5'><span id='E6Pd5'><b id='E6Pd5'><form id='E6Pd5'><ins id='E6Pd5'></ins><ul id='E6Pd5'></ul><sub id='E6Pd5'></sub></form><legend id='E6Pd5'></legend><bdo id='E6Pd5'><pre id='E6Pd5'><center id='E6Pd5'></center></pre></bdo></b><th id='E6Pd5'></th></span></q></dt></tr></i><div id='E6Pd5'><tfoot id='E6Pd5'></tfoot><dl id='E6Pd5'><fieldset id='E6Pd5'></fieldset></dl></div>
          1. <tfoot id='E6Pd5'></tfoot>
              <bdo id='E6Pd5'></bdo><ul id='E6Pd5'></ul>
                <tbody id='E6Pd5'></tbody>

              • <small id='E6Pd5'></small><noframes id='E6Pd5'>

                <legend id='E6Pd5'><style id='E6Pd5'><dir id='E6Pd5'><q id='E6Pd5'></q></dir></style></legend>

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

                  问题描述

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

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

                  如你所见,我还添加了一个 QMessageBox 来查看 menuBar 何时获得焦点.而这个框只出现一半的时间.它是这样的:

                  1. 应用启动,菜单栏隐藏
                  2. 我按下 ALT,显示菜单栏,没有消息框,没有焦点
                  3. 我按下 ALT,隐藏菜单栏
                  4. 我按下 ALT,显示菜单栏,消息框,焦点
                  5. 我按下 ALT,隐藏菜单栏
                  6. 我按下 ALT,显示菜单栏,没有消息框,没有焦点
                  7. 我按下 ALT,隐藏菜单栏
                  8. 我按下 ALT,显示菜单栏,消息框,焦点

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

                  解决方案

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

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

                  针对 Qt 5.9.4 进行测试.

                  因为它包含很多其他垃圾,一个最小的例子:

                  class AutoHidingMenuBar : public QMenuBar {Q_OBJECT上市:AutoHidingMenuBar() : QMenuBar() {设置最大高度(0);连接(qApp, &QApplication::focusChanged, this, &AutoHidingMenuBar::focusChanged);}私人插槽:void focusChanged(QWidget *from, QWidget *to) {bool inFocus = hasFocus() ||isAncestorOf(focus) ||hasFocusedChild();if (inFocus &&maximumHeight() == 0) {自动动作 = activeAction();设置最大高度(100);如果(动作){//XXX 这是一个小技巧.我们可以做//QCoreApplication::processEvents();//setActiveAction(action);//效果几乎相同,但随后我们*打开*了单次按下 alt 时的第一个菜单...auto evt = new QMouseEvent(QEvent::MouseMove, actionGeometry(action).center(), Qt::NoButton, Qt::NoButton, Qt::NoModifier);QCoreApplication::postEvent(this, evt);}} else if (!inFocus &&maximumHeight() != 0)) {设置最大高度(0);}}私人的:bool hasFocusedChild() {QObjectList 队列{children()};而 (!queue.empty()) {auto child = queue.takeFirst();自动小部件 = dynamic_cast<QWidget *>(child);if (widget && widget->hasFocus())返回真;queue.append(child->children());}返回假;}};

                  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 ?

                  解决方案

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

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                    <tbody id='aE1Pg'></tbody>
                    <bdo id='aE1Pg'></bdo><ul id='aE1Pg'></ul>

                      • <tfoot id='aE1Pg'></tfoot>

                        <small id='aE1Pg'></small><noframes id='aE1Pg'>

                        <legend id='aE1Pg'><style id='aE1Pg'><dir id='aE1Pg'><q id='aE1Pg'></q></dir></style></legend>

                      • <i id='aE1Pg'><tr id='aE1Pg'><dt id='aE1Pg'><q id='aE1Pg'><span id='aE1Pg'><b id='aE1Pg'><form id='aE1Pg'><ins id='aE1Pg'></ins><ul id='aE1Pg'></ul><sub id='aE1Pg'></sub></form><legend id='aE1Pg'></legend><bdo id='aE1Pg'><pre id='aE1Pg'><center id='aE1Pg'></center></pre></bdo></b><th id='aE1Pg'></th></span></q></dt></tr></i><div id='aE1Pg'><tfoot id='aE1Pg'></tfoot><dl id='aE1Pg'><fieldset id='aE1Pg'></fieldset></dl></div>