在我的项目中,我有一些按钮可以使用它在可见和不可见之间切换:
ui->button->setVisible(true);
//or
ui->button->setVisible(false);
不过,好像隐身的时候也不起作用?我该如何解决这个问题?
我已经试过了:
ui->button->setEnabled(true);
对于所有这些,但没有任何变化。
最佳答案
当您调用 QWidget::setVisible(false)
时,您不仅将它从 View 中隐藏,而且从逻辑上将它从布局中删除,因此它不再响应按键或鼠标点击。您想要的是将小部件保留在那里而不显示它。在您的情况下,我会尝试更改与您的 QPushButton
关联的 QPalette
以使其透明(即不可见)
// 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/