简体   繁体   English

如何使 QCheckBox 只读,但不灰显

[英]How to make QCheckBox readonly, but not grayed-out

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我使用了有效的setEnabled(bool) ,但是复选框是灰色的并且几乎不可读
  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.但是我需要一种标志来确定该框是否为只读,然后重置检查状态,这意味着我需要创建自己的CheckBox类。
  3. setCheckable does not work either, it does not allow me to set a checked state at all: 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);

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.更具体:更改示例中的图标对我不起作用,可能是因为我使用的是 Windows,并且图标在示例中的路径下不可用。


PS: Related, but no answer here PS:相关,但这里没有答案

Disabling a QCheckbox in a tricky way以一种棘手的方式禁用 QCheckbox
Qt - How to disable QCheckBox while retaining checked state? Qt - 如何在保留选中状态的同时禁用 QCheckBox?

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:这是 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);

Inherit from QCheckBox and override nextCheckState method https://doc.qt.io/qt-5/qcheckbox.html#nextCheckState do the trick.从 QCheckBox 继承并覆盖 nextCheckState 方法https://doc.qt.io/qt-5/qcheckbox.html#nextCheckState可以解决问题。

void ReadOnlyCheckBox::nextCheckState()
{

}

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

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