当前位置: 首页 > 网络知识

Qt 设置horizontal line 和vertical line的颜色

时间:2026-01-29 09:26:25

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

1.使用样式表(stylesheet)设置颜色:

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

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

2.使用QPalette设置颜色:

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

效果:

使用QPalette设置颜色时,需要设置QFrame和QSplitter的背景色(QPalette::Background)为所需的颜色。

通过设置setAutoFillBackground(true)来启用自动填充背景色。

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



上一篇:Visual Stdio 修改默认堆栈大小
下一篇:Qt 解决qtcreator工程文件例程报错error: cannot initialize object parameter of type ‘QWidget‘ with an expression
Qt
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素