QSlider风格设置

QT的滑动条在开发的过程中经常使用,默认的QSlider风格比较简陋,一般达不到UI设计的效果,本篇记录一个QSlider使用过程中风格的设置。

1.qss常用的字段属性

1.1背景属性

属性意思
background Background背景属性
background-colorBrush背景颜色
background-imageUrl背景图
background-repeatRepeat是否重绘
background-positionAlignment背景位置
background-attachmentAttachment确定背景图像是相对于窗口滚动还是固定。
background-clipOrigin绘制区域可裁剪
background-originOrigin背景位置和背景图像一起使用

2.边框属性boder

属性意思
borderBorder边框属性
border-topBorder顶部边框属性
border-rightBorder右边边框属性
border-bottomBorder底部边框属性
border-leftBorder左边边框属性
border-colorBox Colors边框颜色
border-top-colorBrush顶部边框颜色
border-right-colorBrush右边边框颜色
border-bottom-colorBrush底部边框颜色
border-left-colorBrush左边边框颜色
border-imageBorder Image边框背景图
border-radiusRadius边框圆角弧度

3.外边距属性margin

属性意思
margin Box Lengths外边距属性
margin-topLength顶部外边距属性
margin-rightLength右边外边距属性
margin-bottomLength底部外边距属性
margin-leftLength左边外边距属性

4.内边距属性padding

代码实现

#include "qsliderdemo.h"
#include "ui_qsliderdemo.h"

QSliderDemo::QSliderDemo(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::QSliderDemo)
{
    ui->setupUi(this);
    initView();
}

QSliderDemo::~QSliderDemo()
{
    delete ui;
}


void QSliderDemo::initView()
{
    // 水平QSlider  滑动块前面的区域风格  滑动块后面的区域风格  滑块风格 margin: 20px 0;
    QString horizontalStyle = QString(
                "QSlider::groove:horizontal{border: 0px solid #ff0000;height:4px;}"
                "QSlider::sub-page:horizontal{background: #2F89FC;border-radius: 2px;}"
                "QSlider::add-page:horizontal{background: #FFFFFF;border-radius: 2px;}"
                "QSlider::handle:horizontal{background:#FFFFFF;border: 1px solid #666666;border-radius:8px;width:18px;height:18px;margin-top:-6px;margin-bottom: -6px;}"
                );


    ui->horizontalSlider->setStyleSheet(horizontalStyle);

    QString horizontalStyle2 = QString(
                "QSlider::groove:horizontal{border: 0px solid #ff0000;height:4px;}"
                "QSlider::sub-page:horizontal{background: #2F89FC;border-radius: 2px;}"
                "QSlider::add-page:horizontal{background: #FFFFFF;border-radius: 2px;}"
                "QSlider::handle:horizontal{background:#FFFF00;border:1px solid #0000ff;border-radius:8px;width:18px;height:18px;margin:-6px 1px -6px 1px;}"
                );

    ui->horizontalSlider_2->setStyleSheet(horizontalStyle2);

    //滑动块以图片作背景
    QString horizontalStyle3 = QString(
                "QSlider::groove:horizontal{border: 0px solid #ff0000;height:4px;}"
                "QSlider::sub-page:horizontal{background: #2F89FC;border-radius: 2px;}"
                "QSlider::add-page:horizontal{background: #FFFFFF;border-radius: 2px;}"
                "QSlider::handle:horizontal{border-image:url(:/image/slider.png);width:17px;height:17px;margin-top:-6px;margin-bottom: -6px;}"
                );

    ui->horizontalSlider_3->setStyleSheet(horizontalStyle3);

    // 垂直QSlider  滑动块上面的区域风格  滑动块下面的区域风格  滑块风格
    QString verticalStyle = QString(
                "QSlider::groove:vertical{border: 0px solid #ff0000;width:4px;}"
                "QSlider::sub-page:vertical{background: #2F89FC;border-radius: 2px;}"
                "QSlider::add-page:vertical{background: #FFFFFF;border-radius: 2px;}"
                "QSlider::handle:vertical{background:#FFFFFF;border: 1px solid #666666;border-radius:8px;width:18px;height:18px;margin-left:-6px;margin-right: -6px;}"
                );


    ui->verticalSlider->setStyleSheet(verticalStyle);

    QString verticalStyle2 = QString(
                "QSlider::groove:vertical{border: 0px solid #ff0000;width:4px;}"
                "QSlider::sub-page:vertical{background: #2F89FC;border-radius: 2px;}"
                "QSlider::add-page:vertical{background: #FFFFFF;border-radius: 2px;}"
                "QSlider::handle:vertical{background:#FFFF00;border:1px solid #0000ff;border-radius:8px;height:18px;width:18px;margin:1px -6px 1px -6px;}"
                );

    ui->verticalSlider_2->setStyleSheet(verticalStyle2);

    //滑动块以图片作背景
    QString verticalStyle3 = QString(
                "QSlider::groove:vertical{border: 0px solid #ff0000;width:4px;}"
                "QSlider::sub-page:vertical{background: #2F89FC;border-radius: 2px;}"
                "QSlider::add-page:vertical{background: #FFFFFF;border-radius: 2px;}"
                "QSlider::handle:vertical{border-image:url(:/image/slider.png);width:17px;height:17px;margin-left:-6px;margin-right: -6px;}"
                );

    ui->verticalSlider_3->setStyleSheet(verticalStyle3);
}

运行效果

参考文档

https://doc.qt.io/qt-5/stylesheet-examples.html
https://blog.csdn.net/qq_33659478/article/details/119253405
https://doc.qt.io/qt-5/stylesheet-reference.html
https://blog.csdn.net/tax10240809163com/article/details/50899023
https://blog.csdn.net/weixin_45001971/article/details/126107638

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值