要设置 QComboBox 的样式,可以使用 QSS(Qt Style Sheets)来实现。以下是一个示例,演示了如何设置 QComboBox 的样式:

// 创建一个 QComboBox 对象
QComboBox* comboBox = new QComboBox(this);

// 设置 QComboBox 的样式表
comboBox->setStyleSheet('QComboBox {'
                        'background-color: white;'
                        'border: 1px solid gray;'
                        'border-radius: 3px;'
                        'padding: 1px 18px 1px 3px;'
                        'min-width: 6em;'
                        '}'
                        'QComboBox::drop-down {'
                        'subcontrol-origin: padding;'
                        'subcontrol-position: top right;'
                        'width: 15px;'
                        'border-left-width: 1px;'
                        'border-left-color: darkgray;'
                        'border-left-style: solid;'
                        'border-top-right-radius: 3px;'
                        'border-bottom-right-radius: 3px;'
                        '}'
                        'QComboBox::down-arrow {'
                        'image: url(:/images/down_arrow.png);'
                        '}'
                        'QComboBox::down-arrow:on {'
                        'image: url(:/images/up_arrow.png);'
                        '}'
                        'QComboBox QAbstractItemView {'
                        'border: 1px solid gray;'
                        'selection-background-color: lightgray;'
                        '}');

这里的 QComboBox 样式表包括了多个部分:

  • QComboBox:设置了整个 QComboBox 的背景颜色、边框样式、圆角等属性。
  • QComboBox::drop-down:设置了下拉箭头按钮的样式,包括位置、边框等。
  • QComboBox::down-arrow:设置了下拉箭头按钮的图标。
  • QComboBox::down-arrow:on:设置了当下拉菜单展开时,箭头按钮的图标。
  • QComboBox QAbstractItemView:设置了下拉菜单的样式,包括边框、选中项背景颜色等。

你可以根据自己的需求修改这些样式属性,以达到想要的效果。

Qt QSS 设置 QComboBox 样式 - 完整指南

原文地址: https://www.cveoy.top/t/topic/oAl4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录