QMenuBar, QAction - setVisible slot

  • 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

  • Moderators

    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.

  • This post is deleted!

  • @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

  • Moderators


    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.

  • @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?

  • Moderators

    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, [&]{
        return a.exec();

  • @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!

Log in to reply