Qt Assistant中官方提供的脚本定义样例如下:
QComboBox {
border: 1px solid gray;
border-radius: 3px;
padding: 1px 18px 1px 3px;
min-width: 6em;
}
QComboBox:editable {
background: white;
}
QComboBox:!editable, QComboBox::drop-down:editable {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
}
/* QComboBox gets the "on" state when the popup is open */
QComboBox:!editable:on, QComboBox::drop-down:editable:on {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #D3D3D3, stop: 0.4 #D8D8D8,
stop: 0.5 #DDDDDD, stop: 1.0 #E1E1E1);
}
QComboBox:on { /* shift the text when the popup opens */
padding-top: 3px;
padding-left: 4px;
}
QComboBox::drop-down {
subcontrol-origin: padding;
subcontrol-position: top right;
width: 15px;
border-left-width: 1px;
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;
}
QComboBox::down-arrow {
image: url(/usr/share/icons/crystalsvg/16x16/actions/1downarrow.png);
}
QComboBox::down-arrow:on { /* shift the arrow when popup is open */
top: 1px;
left: 1px;
}
遗憾的是没有上面的脚本中没有提供QComboBox下拉列表的样式定义。
QComboBox下拉列表不支持直接定义样式,需要在源代码中使用到样式委托对象QStyledItemDelegate
//用于下拉框item的样式美化
QStyledItemDelegate* itemDelegate = new QStyledItemDelegate();
ui.comboBox1->setItemDelegate(itemDelegate);
然后再在qss脚本中定义QAbstractItemView相关样式,以下是简易示例:
QComboBox QAbstractItemView
{
border: 1px solid rgb(161,161,161);
}
QComboBox QAbstractItemView::item
{
height: 24px;
}
QComboBox QAbstractItemView::item:selected
{
background-color: rgba(54, 98, 180);
}