3

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.

Geore Shg
  • 1,299
  • 5
  • 23
  • 38

3 Answers3

7

QAction * QToolBar::addWidget ( QWidget * widget )

You should hide returned QAction

Kamil Klimek
  • 12,884
  • 2
  • 43
  • 58
  • Interesting suggestion. This would be an elegant solution if the majority of your toolbar items are QActions and only have one or two QWidgets on it. –  Feb 19 '12 at 03:08
  • Even if you add QWidget to QToolBar it will return `QAction *` for it: `QAction * QToolBar::addWidget ( QWidget * widget )` – Kamil Klimek Dec 19 '12 at 08:58
1

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.

0
toolbar->actions().at(0)->setVisible(false);
animuson
  • 53,861
  • 28
  • 137
  • 147
Dmitriy Kachko
  • 2,804
  • 1
  • 19
  • 21