简体   繁体   English

Qt - 如何在保持检查状态的同时禁用QCheckBox?

[英]Qt - How to disable QCheckBox while retaining checked state?

I have a dialog with two checkboxes, let's call them A and B. When A is NOT checked, B should be able to be toggled as the user desires. 我有一个带有两个复选框的对话框,让我们称它们为A和B.当未选中A时,B应该能够根据用户的需要进行切换。 When A IS checked, B should not be able to be toggled. 选中A时,B不应该切换。 Right now I have the following in my constructor function for the dialog: 现在我在对话框的构造函数中有以下内容:

connect(ui->A, SIGNAL(toggled(bool)),
        this, SLOT(setBCheckable(bool)));

...and then I have that function as this: ...然后我有这个功能:

void MyClass::setBCheckable(bool AChecked)
{
    if(AChecked)
    {
        ui->B->setCheckable(false);
    }
    else
    {
        ui->B->setCheckable(true);
    }
}

However, when I do this, I've noticed that when I click A, B will APPEAR to retain it's status, but then when I move the mouse over it (whether A is still clicked or not, just the next time I mouse over after checking A), it refreshes B to NOT being clicked. 但是,当我这样做的时候,我注意到当我点击A时,B将显示它保持状态,但是当我将鼠标移到它上面时(无论A是否仍然被点击,只是下次我鼠标悬停时)检查A)后,它刷新B不被点击。 I cannot have it doing this, since I need the function to retain the value for B even when B cannot be changed. 我不能这样做,因为我需要函数来保留B的值,即使B不能被改变。 Is there a way to do this? 有没有办法做到这一点? To summarize, I need a way for a QCheckBox to keep it's checked status even when the button cannot be toggled. 总而言之,即使按钮无法切换,我也需要一种方法让QCheckBox保持其状态。 Thanks in advance! 提前致谢!

使用函数setEnabled(false)这将禁用但应保持其状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM