24

Any good way to make a checkbox readonly, but also not grayed-out (hardly visible).

  1. I have used setEnabled(bool) which works, but the checkbox then is grayed-out and hardly readable
  2. I can react on a toggle signal and reset the state. But I would need a kind of flag to determine if the box is read-only and then reset the check state, means I need to create my own CheckBox class.
  3. 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?

Community
  • 1
  • 1
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
  • What do you mean by 'read-only'? A check box doesn't record information but only the state of something. – Poriferous Feb 04 '16 at 00:50
  • 1
    Readonly: User can not change the checkbox state, like in QLineEdit where the use cannot change a readonly text – Horst Walter Feb 04 '16 at 00:54
  • 2
    Btw, I do not understand the downvote. But never mind. – Horst Walter Feb 04 '16 at 00:57
  • 3
    It is not useless. I have a form, which is either editable or readonly. The checkbox either displays true (checked) or false (unchecked). Where have I said it will always be checked (pls. let me know so I can fix that part). Only authenticated users can modify the checkbox, but in readonly mode (anonymous users) still I want to display true or false. And I want to use a checkbox to display true/false, only I do not want it to be modified. – Horst Walter Feb 04 '16 at 02:38

4 Answers4

30

Following the below my code:

this->ui->cb_RealWorld->setAttribute(Qt::WA_TransparentForMouseEvents);
this->ui->cb_RealWorld->setFocusPolicy(Qt::NoFocus);
Devopia
  • 620
  • 5
  • 6
  • 1
    Thanks, just to add to this. You leave the checkbox as "enabled" and "checkable". This just makes it so the control ignores mouse events and can not have focus. – gimp3695 Nov 19 '17 at 00:10
11

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);
}
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
1

On Windows, remember to #include "windows.h" and set flags as follows:

this->ui->cb_RealWorld->setWindowFlags(this->ui->cb_RealWorld->windowFlags() | Qt::WindowTransparentForInput);
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
sqr3bk
  • 11
  • 1
1

Inherit from QCheckBox and override nextCheckState method https://doc.qt.io/qt-5/qcheckbox.html#nextCheckState do the trick.

void ReadOnlyCheckBox::nextCheckState()
{

}
Tomas
  • 71
  • 5
  • 1
    Inspired by this, I derived my own checkbox class, added a `readOnly` property to it, then returning from above function if `readOnly` is set or calling `QCheckBox::nextCheckState()` if it is not – mBardos May 18 '21 at 15:29