• Qt设置horizontal line 和vertical line的颜色


    在Qt中,要设置水平线(QFrame)和垂直线(QSplitter)的颜色,可以使用样式表(stylesheet)或者直接设置QPalette。
    下面是两种设置的示例:

    1. 使用样式表(stylesheet)设置颜色:
        QFrame* horizontalLine = new QFrame(this);
        horizontalLine->move(20, 20);
        horizontalLine->setFixedHeight(1);
        horizontalLine->setFrameShape(QFrame::HLine);
        horizontalLine->setStyleSheet("background-color: red;"); // 设置水平线颜色为红色
    
    
        QSplitter* verticalLine = new QSplitter(Qt::Vertical, this);
        verticalLine->move(10,20);
        verticalLine->setFixedWidth(1);
        verticalLine->setLineWidth(1);
        verticalLine->setStyleSheet("background-color: blue;"); // 设置垂直线颜色为蓝色
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    使用样式表的优点在于可以更加灵活地设置样式,比如颜色、背景图像等。

    1. 使用QPalette设置颜色:
    QFrame* horizontalLine = new QFrame();
    horizontalLine->move(20, 20);
    horizontalLine->setFixedHeight(1);
    horizontalLine->setFrameShape(QFrame::HLine);
    QPalette pal = palette();
    pal.setColor(QPalette::Background, Qt::red);
    horizontalLine->setAutoFillBackground(true);
    horizontalLine->setPalette(pal); // 设置水平线颜色为红色
    
    QSplitter* verticalLine = QSplitter(Qt::Vertical);
    verticalLine->move(10,20);
    verticalLine->setFixedWidth(1);
    verticalLine->setLineWidth(1);
    QPalette pal = palette();
    pal.setColor(QPalette::Background, Qt::blue);
    verticalLine->setAutoFillBackground(true);
    verticalLine->setPalette(pal); // 设置垂直线颜色为蓝色
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    效果:
    在这里插入图片描述

    使用QPalette设置颜色时,需要设置QFrame和QSplitter的背景色(QPalette::Background)为所需的颜色。
    通过设置setAutoFillBackground(true)来启用自动填充背景色。

    这两种方法都可以用来设置水平线和垂直线的颜色,可以选择适合自己的方法来设置。

  • 相关阅读:
    56. 合并区间 --力扣 --JAVA
    【实战】CEF框架集成MFC DLL的一些坑
    CentOs程序环境准备
    【LeetCode-中等题】208. 实现 Trie (前缀树)
    【图像分割】基于matlab直方图的自适应阈值方法分割前景与背景【含Matlab源码 2144期】
    Oracle设置某个表字段递增
    工具清单 - IDE工具
    k8s 实战篇 - 镜像打包部署 - springboot&mysql - 3
    spring简单配置
    pytorch神经网络工具箱
  • 原文地址:https://blog.csdn.net/MrHHHHHH/article/details/133966907