Make QPushButton invisible yet still work?
When you call QWidget::setVisible(false)
, you not only hide it from view, but also logically remove it from the layout, so it is no longer there to respond to key presses or mouse clicks. What you want is to keep the widget there while not displaying it. What I would try in your situation is changing the QPalette
associated with your QPushButton
to make it transparent (i.e. invisible)
// Make the button "invisible"
QBrush tb(Qt::transparent); // Transparent brush, solid pattern
ui->button->setPalette(QPalette(tb, tb, tb, tb, tb, tb, tb, tb, tb)); // Set every color roles to the transparent brush
// Make the button "visible"
ui->button->setPalette(QPalette()); // Back to the default palette
That way, the button is still logically in the layout (and take up the appropriate space), but it does not show up because it's completely displayed with a transparent color.
setVisible()
sets whether the button is visible or not, completely removing it from the widget's layout.
setEnabled()
sets whether the button is disabled (greyed out) or not.
If you want it usable, but not visually present, try setting the button to flat using pushButton->setFlat(true)
.
This leaves the button text visible, but the button background invisible until pressed (try it and see what I mean). If you want the text hidden too, you could set the text to nothing with pushButton->setText("")
.