让QPushButton隐形但仍然有效?

问题描述 投票:5回答:3

在我的项目中,我有一些按钮在可见和不可见之间变换使用:

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

然而,似乎当它们看不见时,它们也不起作用?我怎么能绕过这个?

我已经尝试过了:

ui->button->setEnabled(true);

对于他们所有人,但没有任何变化。

c++ qt qpushbutton
3个回答
10
投票

当您调用QWidget::setVisible(false)时,您不仅可以将其隐藏在视图中,还可以将其从布局中逻辑删除,因此不再响应按键或鼠标单击。你想要的是将小部件保持在那里而不显示它。我会尝试在你的情况下改变与你的QPalette相关的QPushButton,使其透明(即看不见)

// 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

这样,按钮仍然在逻辑上处于布局中(并占据适当的空间),但它没有显示,因为它完全以透明颜色显示。


3
投票

setVisible()设置按钮是否可见,将其从小部件的布局中完全删除。 setEnabled()设置按钮是否被禁用(灰色)。

如果您希望它可用,但不是在视觉上存在,请尝试使用flat将按钮设置为pushButton->setFlat(true)。这使按钮文本可见,但按钮背景在按下之前不可见(尝试并查看我的意思)。如果您也想隐藏文本,可以使用pushButton->setText("")将文本设置为空。


0
投票

使按钮不可见的另一种方法是:

ui->errorMask->setStyleSheet("QPushButton { background-color: rgba(10, 0, 0, 0); }");
© www.soinside.com 2019 - 2024. All rights reserved.