qt - 下拉列表中所选项目的 QComboBox 样式

标签 qt qcombobox qtstylesheets

我想设置组合框下拉列表中所选项目的突出显示样式。

与其他问题的区别在于,我不想设置“所选”项目(悬停在其上)的样式,而是设置已选择的项目的样式。

默认是某种在文本上绘制的勾号。我希望所选项目具有粗体文本且没有勾号。

或者在最坏的情况下,只需将文本向右移动,以使刻度正确可见。

我拥有的是这样的:

enter image description here

注意第 17 项,它在数字 17 上打勾。

这是我的样式表:

QComboBox
{
    subcontrol-origin: padding;
    subcontrol-position: top right;
    selection-background-color: #111;
    selection-color: yellow;
    color: white;
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #565656, stop: 0.1 #525252, stop: 0.5 #4e4e4e, stop: 0.9 #4a4a4a, stop: 1 #464646);
    border-style: solid;
    border: 1px solid #1e1e1e;
    border-radius: 5;
    padding: 1px 0px 1px 20px;
}


QComboBox:hover, QPushButton:hover
{
    border: 1px solid yellow;
    color: white;
}

QComboBox:editable {
    background: red;
    color: pink;
}

QComboBox:on
{
    padding-top: 0px;
    padding-left: 0px;
    color: white;
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #2d2d2d, stop: 0.1 #2b2b2b, stop: 0.5 #292929, stop: 0.9 #282828, stop: 1 #252525);
    selection-background-color: #ffaa00;
}

QComboBox:!on
{
    color: white;
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #666, stop: 0.1 #555, stop: 0.5 #555, stop: 0.9 #444, stop: 1 #333);
}

QComboBox QAbstractItemView
{
    border: 2px solid darkgray;
    color: black;
    selection-background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #111, stop: 1 #333);
}

QComboBox::drop-down
{
     subcontrol-origin: padding;
     subcontrol-position: top right;
     width: 15px;
     color: white;
     border-left-width: 0px;
     border-left-color: darkgray;
     border-left-style: solid; /* just a single line */
     border-top-right-radius: 3px; /* same radius as the QComboBox */
     border-bottom-right-radius: 3px;
     padding-left: 10px;
 }

QComboBox::down-arrow, QSpinBox::down-arrow, QTimeEdit::down-arrow, QDateEdit::down-arrow
{
     image: url(:/icons/down_arrow.png);
     width: 7px;
     height: 5px;
}

我试图覆盖项目延迟:

ui->modeComboBox->setItemDelegate(new QStyledItemDelegate());

随着

QComboBox QAbstractItemView::item:selected style 

或者覆盖 View :

QListView * listView = new QListView(ui->modeComboBox);

listView->setStyleSheet("QListView::item {                              \
                         border-bottom: 5px solid white; margin:3px; }  \
                         QListView::item:selected {                     \
                         border-bottom: 5px solid black; margin:3px;    \
                         color: black;                                  \
                        }");
ui->modeComboBox->setView(listView);

但在这两种情况下,这都会完全禁用所选项目(第 17 项)的突出显示

更新1

我测试设置::item:checked 样式表,但没有帮助:

QListView * listView = new QListView(ui->modeComboBox);
listView->setStyleSheet("QListView::item {                              \
                         border-bottom: 5px solid white; margin:3px; }  \
                         QListView::item:selected {                     \
                         border-bottom: 5px solid black; margin:3px;    \
                         color: black; }                                \
                         QListView::item:checked {                      \
                         background-color: green;                       \
                         color: green;}"
                         );
ui->modeComboBox->setView(listView);

此外,我将其添加到样式表中只是为了确定:

QComboBox QListView::item:checked {
 background-color: green;
}

选中17模式的结果是(黑色只是鼠标悬停):

enter image description here

更新2

好的,我可以更改选中项目的字体粗细,但我无法从该项目中删除勾号。我尝试了我的样式表文件,发现这两个选择器负责选中项目突出显示的样式:

QWidget:item:selected
{
     border: 0px solid #999900;
     background: transparent;
}
QWidget:item:checked
{
     font-weight: bold;
}

如果我删除::item:selected 则::item:checked 不起作用(它不会使选中的项目变为粗体)并且勾号消失。

在 Qt 论坛上,他们建议我以某种方式缩短“组合框图标的空间”。我找不到负责的选择器。

最佳答案

我知道这真的很晚了,但对于有同样问题的人来说: 我在另一个问题中找到了这个:Not able to hide Choice-Indicator of the QComboBox

这应该隐藏指示器/刻度线:

QComboBox::indicator{
    background-color:transparent;
    selection-background-color:transparent;
    color:transparent;
    selection-color:transparent;
}

关于qt - 下拉列表中所选项目的 QComboBox 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939990/

相关文章:

python - 将项目放到 QComboBox 上?

c++ - QTimer for QGLWidget,在其他机器上的绘图和计时不正确

c++ - 在堆栈上分配和从函数返回 QSharedPointer

python - 更改PyQt中ComboBox的背景颜色

python - ScrollBar QStyleSheet 显示时带有背景 它应该是透明的

c++ - 以编程方式编辑 QSS 样式表

qt - 更改父伪状态的后代 QLabel 属性

连接到 Qt 信号的 Python lambda 函数在不同线程中创建时不会运行

c++ - 热键、快捷键和加速键有什么区别?

c++ - 当对话框中的任何小部件发出信号时,是否可以调用插槽?