Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make QPushButton invisible yet still work?

In my project, I have some pushbuttons that change between visible and invisible using this:

ui->button->setVisible(true);
//or
ui->button->setVisible(false);

However, it seems that when they are invisible, they also do not work? How can I get around this?

I have already tried this:

ui->button->setEnabled(true);

for all of them but nothing changes.

like image 677
mrg95 Avatar asked Jul 15 '13 00:07

mrg95


Video Answer


2 Answers

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.

like image 158
Fred Avatar answered Oct 22 '22 23:10

Fred


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("").

like image 28
Jamin Grey Avatar answered Oct 22 '22 23:10

Jamin Grey