سؤال

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.

هل كانت مفيدة؟

المحلول

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

Another way to make the button invisible is:

ui->errorMask->setStyleSheet("QPushButton { background-color: rgba(10, 0, 0, 0); }");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top