Make QPushButton invisible yet still work?
Asked Answered
W

3

6

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.

Wellestablished answered 15/7, 2013 at 0:54 Comment(2)
What do you mean "still work". How (code-wise) are you wanting to interact with them while they are invisible? And what do you mean, 'nothing changes'. What are you hoping 'setEnabled()' does? Describe what you hope it does, without using the word 'enable' or 'disable'. Not trying to be snarky or a pain, just trying to understand what you are expecting and asking for! =)Fructificative
I want the button to be usable i guess. You click it, and stuff happens. I meant "nothing changes" as in, the buttons are still unusable.Wellestablished
H
10

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.

Hyperplane answered 15/7, 2013 at 1:35 Comment(1)
This is a great suggestion for hiding design elements dynamically without having the layout change automatically ! (which is simply impossible in Qt4). Thank you for the idea !Bunyabunya
F
3

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

Fructificative answered 15/7, 2013 at 3:58 Comment(0)
I
0

Another way to make the button invisible is:

ui->errorMask->setStyleSheet("QPushButton { background-color: rgba(10, 0, 0, 0); }");
Indention answered 20/3, 2019 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.