Search code examples
c++qtuser-interfaceqtoolbar

Why cant a QToolButton be hidden after it is added to a QToolBar?


This works...

QToolButton * toolbutton = new QToolButton(this);

//hide before addWidget
toolbutton->hide();

addWidget(toolbutton);

But this doesn't

QToolButton * toolbutton = new QToolButton(this)

addWidget(toolbutton);

//hide after addWidget
toolbutton->hide();

Is there an alternative so I can actually hide after a QToolButton after it is added to a QToolBar? I need to during runtime.


Solution

  • One alternative is to add a QAction instead of a widget and then hide the QAction. I've tried it and it works with QAction::setVisible(false).

    You can also do something like QToolBar::actions().at(3)->setVisible(false); if you know the position of the widget in the QToolBar.