Qt 给QCheckBox增加样式

发布时间 2023-11-28 17:38:08作者: 飘杨......

一、概述

  做一个好看的QCheckBox

二、示例

  1.样式

QCheckBox {
    spacing:5px; 
}

QCheckBox::indicator {
    width: 24px;
    height: 24px;
}

QCheckBox::indicator:unchecked {
    image: url(:images/icon_checked.png);
}

QCheckBox::indicator:unchecked:disabled {
    image: url(:/buttonbg/checkbox_disable);
}

QCheckBox::indicator:unchecked:hover {
    image: url(:/buttonbg/checkbox_hover);
}

QCheckBox::indicator:checked {
    image: url(:/buttonbg/checkbox_down);
}

QCheckBox::indicator:indeterminate {
    image: url(:/buttonbg/checkbox_indeterminate);
}

  2.CheckBox.h/CheckBox.cpp

#include "CheckBox.h"

CheckBox::CheckBox(QWidget *parent)
    : QCheckBox(parent)
{
    //这里设置一个默认的样式
    QString css = StyleHelper::getStyleSheet(StyleConfig::COMMON_EDITTEXT_DEFAULT());
    this->setStyleSheet(css);
}

CheckBox::~CheckBox()
{
}

  3.使用

    //CheckBox
    CheckBox* cb = new CheckBox();

  4.使用