首先启用QCheckbox的tristate属性
//启用QCheckBox的tristate属性,使checkbox有三种状态。例:
questionCheckBox->setTristate(true);
//使用setCheckState(Qt::CheckState state)函数设置checkbox当前的状态。例:
//Qt::CheckState是枚举类型有三个取值:Checked、UnChecked、PartiallyChecked。
qCheckBox->setCheckState(Qt::CheckState::Checked);
qCheckBox->setCheckState(Qt::CheckState::UnChecked);
qCheckBox->setCheckState(Qt::CheckState::PartiallyChecked)
qss文件中设置三种状态的indicator样式:
QCheckBox::indicator:indeterminate{ /* 对应Qt::CheckState::PartiallyChecked状态 */
image:url(:/images/images/checkbox-red.png);
}
QCheckBox::indicator:checked{ /*checked状态*/
image:url(:/images/images/checkbox-green-correct.png);
}
QCheckBox::indicator:unchecked{ /*unchecked状态*/
image:url(:/images/images/checkbox-unchecked-blue.png);
}
可以实现的效果:
状态:unchecked
状态:checked
状态:partiallyChecked对应qss中的indeterminate 伪状态
实现一个题目图例例表: