Qt 自定义 ”开关“ 控件

一、自绘控件

采用纯代码自绘控件,对编码者要求比较高。

 

二、用现有 QCheckBox 实现

准备两个图片,打开和关闭

在 QCheckBox 控件,邮件改写样式表,即可实现开关状态改变。使用时需要设置 width 和 height ,否则不会动态适应大小。

QCheckBox::indicator {
width:  100px;
height: 100px;
}
QCheckBox::indicator:unchecked {
image: url(:/on);
}
QCheckBox::indicator:checked {
image: url(:/off);
}

也可以将此QSS写入到代码当中

ui->checkBox->setStyleSheet("QCheckBox::indicator {width:  100px;height: 100px;}QCheckBox::indicator:unchecked {image: url(:/on);}QCheckBox::indicator:checked {image: url(:/off);}");

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.