当前位置: 首页 > article >正文

qt的slider样式定制

 

 

 实现上图样式滑块

滑块的圆形通过样式表实现:

this->setStyleSheet(R"(
            QSlider::groove:horizontal {
                background: rgb(51, 51, 51);
                width: 80px;
                height: 8px;
                border-radius: 4px;
            }
            
            QSlider::handle:horizontal {
                background: rgb(207, 207, 207);
                border: 1px solid #5c5c5c;
                width: 14px;
                margin: -4px 0;
                border-radius: 8px;
            }

            QSlider::handle:horizontal:hover {
                background: rgb(207, 207, 207);
                border: 2px solid #5c5c5c;
                width: 12px;
                margin: -4px 0;
                border-radius: 8px;
            }

        )");

样式表中需要注意滑轨的高度8+滑块的margin4*2=滑块的宽度14+滑块boder1*2=16,再设置radius为16/2=8,满足上述关系,滑块才能为圆形

 

滑块底部的蓝色渐变条需要通过过重写slider的painter函数实现,注意绘制渐变条在滑轨两端的处理,不要绘制超过滑轨两端。

通过计算,在左右两侧各绘制渐变条即可

 QSlider::paintEvent(event);
        QPainter painter(this);
        QStyleOptionSlider opt;
        initStyleOption(&opt);
        painter.setRenderHint(QPainter::Antialiasing);

        QRect grooveRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
        QRect handleRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);

        int leftBoundary = ((handleRect.left() - 11) > grooveRect.left()) ? (handleRect.left() - 11) : grooveRect.left();
        int rightBoundary = ((handleRect.right() + 11) < grooveRect.right()) ? (handleRect.right() + 11) : grooveRect.right();
        // 绘制左侧渐变蓝色条
        QLinearGradient leftGradient(leftBoundary, grooveRect.top(), handleRect.left(), grooveRect.top());
        leftGradient.setColorAt(0, QColor(32, 128, 247, 0));
        leftGradient.setColorAt(1, QColor(32, 128, 247, 255));
        painter.fillRect(QRect(leftBoundary, grooveRect.top()+2, handleRect.left() - leftBoundary, 4), leftGradient);

        // 绘制右侧渐变蓝色条
        QLinearGradient rightGradient(handleRect.right(), grooveRect.top(), rightBoundary, grooveRect.top());
        rightGradient.setColorAt(0, QColor(32, 128, 247, 255));
        rightGradient.setColorAt(1, QColor(32, 128, 247, 0));
        painter.fillRect(QRect(handleRect.right(), grooveRect.top() + 2, rightBoundary - handleRect.right(), 4), rightGradient);


http://www.kler.cn/a/600350.html

相关文章:

  • 从 0 到 1 构建 Python 分布式爬虫,实现搜索引擎全攻略
  • 什么是PHP伪协议
  • 如何将maltab开发的app嵌入PPT中展示并且可实时互动
  • 【QA】外观模式在Qt中有哪些应用?
  • 算法-枚 举
  • 二次封装 el-tooltip
  • 《基于Python的财务数据可视化与决策支持系统开发》开题报告
  • 从零开始学习PX4源码14(board-字符设备串口)
  • SOFABoot-09-模块隔离
  • MongoDB 配合python使用的入门教程
  • Docker学习笔记(十二)docker镜像没有vi怎么优雅的编辑文本
  • 2025最新-智慧小区物业管理系统
  • torch.nn和torch.nn.function的区别
  • 探索Google Test(gtest):C++单元测试的强大工具
  • ES聚合学习(三)
  • 常见CMS漏洞(一):WordPress
  • 【中间件】Rabbit离线部署操作
  • 初识R语言饼状图
  • 使用 langchain_deepseek 实现自然语言转数据库查询SQL
  • PRC框架(以Dubbo为例),分布式事务解决方案