在我的项目中,我使用以下按钮在可见和不可见之间切换:
ui->button->setVisible(true);
//or
ui->button->setVisible(false);
但是,似乎当它们不可见时,它们也行不通吗?我该如何解决?
我已经尝试过了:
ui->button->setEnabled(true);
对于所有的人,但没有任何改变。
最佳答案
当您调用QWidget::setVisible(false)
时,您不仅将其从 View 中隐藏,而且从逻辑上将其从布局中删除,因此它不再在那里响应按键或鼠标单击。您想要的是将小部件保留在其中,而不显示它。在您遇到的情况下,我会尝试更改与您的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
这样,按钮在逻辑上仍然位于布局中(并占用适当的空间),但是由于完全以透明颜色显示,因此它没有显示。
关于c++ - 使QPushButton不可见,但仍然有效吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17645319/