【发布时间】:2015-07-08 12:25:57
【问题描述】:
我想在组合框的下拉菜单中设置所选项目的突出显示样式。
与其他问题的不同之处在于我不想设置“选定”项目的样式(悬停在上面),而是设置已经选择的项目的样式。
默认是在文本上绘制的某种刻度。我希望所选项目具有粗体文本且没有刻度。
或者在最坏的情况下,将文本向右移动,以使刻度正确可见。
我拥有的是这样的:
请注意第 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
或者覆盖视图:
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模式的结果是(黑色只是鼠标悬停):
更新 2
好的,我可以更改选中项目的字体粗细,但我无法从项目中删除勾号。我对我的样式表文件进行了试验,发现这两个选择器负责突出显示选中项的样式:
QWidget:item:selected
{
border: 0px solid #999900;
background: transparent;
}
QWidget:item:checked
{
font-weight: bold;
}
如果我删除 ::item:selected 则 ::item:checked 不起作用(它不会使选中的项目变为粗体)并且勾号消失。
在 Qt 论坛上,他们建议我以某种方式缩短“组合框图标的空间”。我找不到负责的选择器。
【问题讨论】:
-
从未测试过,但您尝试过:
QComboBox QListView::item:checked { background-color: green; }? -
@iuliu 我试过没用,更新问题..谢谢
-
@Iuliu 你的建议对我有帮助,但我在 QWidget 上设置了它.. 但我仍然无法删除代码..
标签: qt qcombobox qtstylesheets