Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMenuBar, QAction - setVisible slot
Forum Update on Monday, May 27th 2025

QMenuBar, QAction - setVisible slot

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.2k Views
  • 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.
  • Romain CR Offline
    Romain CR Offline
    Romain C
    wrote on last edited by
    #1

    Hi everyone!
    I came with a new problem! I'm working on Qt 5.5 and I get a problem with QMenubar and QAction.
    I got in my software a QMenubar with several QAction, each action containing an icon. I want to dynamically set visible or hide on of this actions.

    So I connect QAction setVisible slot to one of my Signals and it work not as expected:

    • The icon disapear... (That's great)
    • .... But the QMenubar don't resize and I get a gap.

    Thanks for your answers
    Romain

    1 Reply Last reply
    0
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      The QMenubar does not resize because the item/Action is still there, its just not painted, because its hidden.

      I would suggest, clear() the QMenuBar and repopulate it from a QList< QAction> where you removed the unwanted item.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      Romain CR 1 Reply Last reply
      2
      • Romain CR Offline
        Romain CR Offline
        Romain C
        wrote on last edited by Romain C
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          The QMenubar does not resize because the item/Action is still there, its just not painted, because its hidden.

          I would suggest, clear() the QMenuBar and repopulate it from a QList< QAction> where you removed the unwanted item.

          Romain CR Offline
          Romain CR Offline
          Romain C
          wrote on last edited by
          #4

          @J.Hilk Ok, I miss an information.
          When I resize my window, QMenuBar update content and fit to visible actions. So I tried a force repaint() of the QMenubar but nothing happens.

          I already think of an clear and new populate of the Qmenubar, but it seems weird in my point of view.

          Thks for your answer

          J.HilkJ 1 Reply Last reply
          0
          • Romain CR Romain C

            @J.Hilk Ok, I miss an information.
            When I resize my window, QMenuBar update content and fit to visible actions. So I tried a force repaint() of the QMenubar but nothing happens.

            I already think of an clear and new populate of the Qmenubar, but it seems weird in my point of view.

            Thks for your answer

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Romain-C

            Like I said, repainting won't change much, the QActions are hidden, but they still take up the space.

            I usually don't use QMenuBar so I'm not familiar with it, but a quick check of the docu showed me:

            void QWidget::removeAction(QAction *action);
            void QWidget::insertAction(QAction *before, QAction *action);
            

            Use those to "hide/show" the QActions and the layout should adjust automatically to the changes.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            Romain CR 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @Romain-C

              Like I said, repainting won't change much, the QActions are hidden, but they still take up the space.

              I usually don't use QMenuBar so I'm not familiar with it, but a quick check of the docu showed me:

              void QWidget::removeAction(QAction *action);
              void QWidget::insertAction(QAction *before, QAction *action);
              

              Use those to "hide/show" the QActions and the layout should adjust automatically to the changes.

              Romain CR Offline
              Romain CR Offline
              Romain C
              wrote on last edited by Romain C
              #6

              @J.Hilk
              Ok, my question is not well explained.
              I'm already using insert and remove action and it work well, before posting this question =)

              I'm complained about a possible bug in QT. When you hide / show a component in a layout, space is usually taken by others components. And in my mind, it should be the same for QActions (except is not really widget in fact).
              At this time, when a QAction is hide this action really dissapear but their still a gap. BUT when you resize the window this gap disappear, so it seems to have an update problem somewhere, in my opinion.
              If after a resize, the gap still there, I would understand the logic.

              What do you think?
              Romain

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Work's ok for me. The toolbar shrinks when last action is hidden. If this example works for you too then the bug is somewhere in your code, not in Qt.

                #include <QApplication>
                #include <QMenuBar>
                #include <QPushButton>
                #include <QVBoxLayout>
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                
                    QWidget w;
                    w.setLayout(new QVBoxLayout());
                    w.layout()->setMenuBar(new QMenuBar());
                    w.layout()->menuBar()->addAction(new QAction("I'm an action"));
                    w.layout()->addWidget(new QPushButton("Hide action"));
                    QObject::connect(w.findChild<QPushButton*>(), &QPushButton::clicked, [&]{
                        w.layout()->menuBar()->actions().front()->setVisible(false);
                    });
                    w.show();
                
                    return a.exec();
                }
                
                Romain CR 1 Reply Last reply
                2
                • Chris KawaC Chris Kawa

                  Work's ok for me. The toolbar shrinks when last action is hidden. If this example works for you too then the bug is somewhere in your code, not in Qt.

                  #include <QApplication>
                  #include <QMenuBar>
                  #include <QPushButton>
                  #include <QVBoxLayout>
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                  
                      QWidget w;
                      w.setLayout(new QVBoxLayout());
                      w.layout()->setMenuBar(new QMenuBar());
                      w.layout()->menuBar()->addAction(new QAction("I'm an action"));
                      w.layout()->addWidget(new QPushButton("Hide action"));
                      QObject::connect(w.findChild<QPushButton*>(), &QPushButton::clicked, [&]{
                          w.layout()->menuBar()->actions().front()->setVisible(false);
                      });
                      w.show();
                  
                      return a.exec();
                  }
                  
                  Romain CR Offline
                  Romain CR Offline
                  Romain C
                  wrote on last edited by
                  #8

                  @Chris-Kawa Hi Chris,
                  I was coming back on that post, I do a small project on my side just to test this feature and setVisible behavior works fine.
                  Perhaps we are filtering events, or missed something on the main project. I'm making an investigate about that, It confirm that is the right way to deal with.

                  Thanks all, for your help!
                  Romain

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved