Any good way to make a checkbox readonly, but also not grayed-out (hardly visible).
setEnabled(bool)
which works, but the checkbox then is grayed-out and hardly readableCheckBox
class.setCheckable
does not work either, it does not allow me to set a checked state at all:
cb = this->ui->cb_RealWorld->isCheckable();
this->ui->cb_RealWorld->setCheckable(true);
this->ui->cb_RealWorld->setChecked(someValue);
this->ui->cb_RealWorld->setCheckable(cb);
So the best thing I have is to use enable/disable and accept the grayed out style.
------- Edit -------
Following the 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 does not work for me, maybe because I am using Windows and the icons are not available under the path as in the examples.
PS: Related, but no answer here
Disabling a QCheckbox in a tricky way
Qt - How to disable QCheckBox while retaining checked state?
A checkbox HTML element doesn't have a "readonly" property. Consequently, the only way to make a checkbox appear to be "readonly" is to disable the control. A checkbox control can be disabled in Alpha Anywhere using Client-side Enable Expressions.
Practically speaking, this is the solution that many of us are looking for. How to make the checkbox un-changeable (answer: disable it) but still preserve the value of whether it is checked or not upon save (answer: use a hidden input type). Make sure to only add the hidden input if you have also disabled the checkbox.
You can style checkbox label and readonly inputs with CSS, e.g.: input [readonly="readonly"] {} but the browser should make the checkbox should appear greyed out when set to readonly.
$(':checkbox[readonly]'). click(function(){ return false; });
Following the below my code:
this->ui->cb_RealWorld->setAttribute(Qt::WA_TransparentForMouseEvents);
this->ui->cb_RealWorld->setFocusPolicy(Qt::NoFocus);
This is Devopia's solution as a function:
void SetReadOnly(QCheckBox* checkBox, bool readOnly)
{
checkBox->setAttribute(Qt::WA_TransparentForMouseEvents, readOnly);
checkBox->setFocusPolicy(readOnly ? Qt::NoFocus : Qt::StrongFocus);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With