如何使 QCheckBox 只读,但不灰显
How to make QCheckBox readonly, but not grayed-out
任何使复选框只读且不灰显(几乎不可见)的好方法。
- 我已经使用了
setEnabled(bool)
,但它的复选框是灰色的,难以阅读
- 我可以对切换信号做出反应并重置状态。但是我需要一种标志来确定该框是否为只读然后重置检查状态,这意味着我需要创建自己的
CheckBox
class.
setCheckable
也不行,它根本不允许我设置选中状态:
cb = this->ui->cb_RealWorld->isCheckable();
this->ui->cb_RealWorld->setCheckable(true);
this->ui->cb_RealWorld->setChecked(someValue);
this->ui->cb_RealWorld->setCheckable(cb);
所以我最好的办法就是使用 enable/disable 并接受灰色样式。
--------编辑------
遵循 stylesheet examples I was hoping I could set the style of a disabled checkbox like the one of an enabled. Failed so far to do so. More specific: Changing the icon like in the examples 对我不起作用,可能是因为我正在使用 Windows 并且图标在示例中的路径下不可用。
PS:相关,但这里没有答案
Disabling a QCheckbox in a tricky way
Qt - How to disable QCheckBox while retaining checked state?
按照下面我的代码:
this->ui->cb_RealWorld->setAttribute(Qt::WA_TransparentForMouseEvents);
this->ui->cb_RealWorld->setFocusPolicy(Qt::NoFocus);
这是 Devopia 的功能解决方案:
void SetReadOnly(QCheckBox* checkBox, bool readOnly)
{
checkBox->setAttribute(Qt::WA_TransparentForMouseEvents, readOnly);
checkBox->setFocusPolicy(readOnly ? Qt::NoFocus : Qt::StrongFocus);
}
在 Windows 上,记得 #include "windows.h"
并按如下方式设置标志:
this->ui->cb_RealWorld->setWindowFlags(this->ui->cb_RealWorld->windowFlags() | Qt::WindowTransparentForInput);
继承自 QCheckBox 并重写 nextCheckState 方法https://doc.qt.io/qt-5/qcheckbox.html#nextCheckState做这个技巧。
void ReadOnlyCheckBox::nextCheckState()
{
}
任何使复选框只读且不灰显(几乎不可见)的好方法。
- 我已经使用了
setEnabled(bool)
,但它的复选框是灰色的,难以阅读 - 我可以对切换信号做出反应并重置状态。但是我需要一种标志来确定该框是否为只读然后重置检查状态,这意味着我需要创建自己的
CheckBox
class. setCheckable
也不行,它根本不允许我设置选中状态:cb = this->ui->cb_RealWorld->isCheckable(); this->ui->cb_RealWorld->setCheckable(true); this->ui->cb_RealWorld->setChecked(someValue); this->ui->cb_RealWorld->setCheckable(cb);
所以我最好的办法就是使用 enable/disable 并接受灰色样式。
--------编辑------
遵循 stylesheet examples I was hoping I could set the style of a disabled checkbox like the one of an enabled. Failed so far to do so. More specific: Changing the icon like in the examples 对我不起作用,可能是因为我正在使用 Windows 并且图标在示例中的路径下不可用。
PS:相关,但这里没有答案
Disabling a QCheckbox in a tricky way
Qt - How to disable QCheckBox while retaining checked state?
按照下面我的代码:
this->ui->cb_RealWorld->setAttribute(Qt::WA_TransparentForMouseEvents);
this->ui->cb_RealWorld->setFocusPolicy(Qt::NoFocus);
这是 Devopia 的功能解决方案:
void SetReadOnly(QCheckBox* checkBox, bool readOnly)
{
checkBox->setAttribute(Qt::WA_TransparentForMouseEvents, readOnly);
checkBox->setFocusPolicy(readOnly ? Qt::NoFocus : Qt::StrongFocus);
}
在 Windows 上,记得 #include "windows.h"
并按如下方式设置标志:
this->ui->cb_RealWorld->setWindowFlags(this->ui->cb_RealWorld->windowFlags() | Qt::WindowTransparentForInput);
继承自 QCheckBox 并重写 nextCheckState 方法https://doc.qt.io/qt-5/qcheckbox.html#nextCheckState做这个技巧。
void ReadOnlyCheckBox::nextCheckState()
{
}