构造一个复选框QCheckBox,然后使用setTristate()开启三态模式。
QCheckBox *pCheckBox = new QCheckBox(this);
m_pLabel = new QLabel(this);m_pLabel->setText(“Click CheckBox…”);
pCheckBox->setText(QString::fromLocal8Bit(“三态复选框”));// 开启三态模式
pCheckBox->setTristate();// 连接信号槽
connect(pCheckBox, SIGNAL(stateChanged(int)), this, SLOT(onStateChanged(int)));槽函数,判断当前复选框状态,其中包括:选中(Qt::Checked)、半选(Qt::PartiallyChecked)、未选中(Qt::Unchecked)。
void MainWindow::onStateChanged(int state)
{
if (state == Qt::Checked) // “选中”
{
m_pLabel->setText(“Checked”);
}
else if(state == Qt::PartiallyChecked) // “半选”
{
m_pLabel->setText(“PartiallyChecked”);
}
else // 未选中 – Qt::Unchecked
{
m_pLabel->setText(“Unchecked”);
}
}QSS复选框样式。
QCheckBox{
spacing: 5px;
color: white;
}
QCheckBox::indicator {
width: 17px;
height: 17px;
}
QCheckBox::indicator:enabled:unchecked {
image: url(:/Images/checkBox);
}
QCheckBox::indicator:enabled:unchecked:hover {
image: url(:/Images/checkBoxHover);
}
QCheckBox::indicator:enabled:unchecked:pressed {
image: url(:/Images/checkBoxPressed);
}
QCheckBox::indicator:enabled:checked {
image: url(:/Images/checkBoxChecked);
}
QCheckBox::indicator:enabled:checked:hover {
image: url(:/Images/checkBoxCheckedHover);
}
QCheckBox::indicator:enabled:checked:pressed {
image: url(:/Images/checkBoxCheckedPressed);
}
QCheckBox::indicator:enabled:indeterminate {
image: url(:/Images/checkBoxIndeterminate);
}
QCheckBox::indicator:enabled:indeterminate:hover {
image: url(:/Images/checkBoxIndeterminateHover);
}
QCheckBox::indicator:enabled:indeterminate:pressed {
image: url(:/Images/checkBoxIndeterminatePressed);
}连接stateChanged()信号和槽函数,当用户点击复选框时,状态发生改变就会调用槽函数。
根据以上样式可写为:
AirFrame QCheckBox#switch_Button::indicator:unchecked
{
image: url(://resource/area/checkbox/close.png);
}AirFrame QCheckBox#switch_Button::indicator:checked
{
image: url(://resource/area/checkbox/open.png);
}以上是更改使用的。
免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:Qt之QCheckBox https://www.yhzz.com.cn/a/22124.html