改变下拉列表的样式
// 1. 添加代理
QStyledItemDelegate* styleItemDelegate = new QStyledItemDelegate();
ui->comboBox->setItemDelegate(styleItemDelegate);
// 2. qss样式
QComboBox {
border: 1px solid rgb(208, 211, 211); /* 边框 */
color: rgba(0, 0, 0, 255); /* 字体颜色 */
background-color: rgb(241, 244, 244); /* 背景色 */
}
QComboBox QAbstractItemView {
border: 1px solid darkgray; /* 下拉列表边框 */
selection-background-color: lightgray; /* 下拉列表选项选中的颜色 */
padding-top:2px; /* 下拉列表距顶端和底端的距离 */
padding-bottom:2px;
}
QComboBox QAbstractItemView::item{
height: 20px; /* 设置下拉列表选项的行高 */
}
QComboBox {
border: 1px solid #bebebe;
padding: 1px 18px 1px 3px;
font: normal normal 16px "Microsoft YaHei";
color: #555555;
background: transparent;
}
QComboBox:editable{
background: transparent;
}
QComboBox:!editable, QComboBox::drop-down:editable{
background: transparent;
}
QComboBox:!editable:on, QComboBox::drop-down:editable:on{
background: transparent;
}
QComboBox:!on{
}
QComboBox:on{ /* the popup opens */
color: #555555;
border-color: #327cc0;
background: transparent;
}
QComboBox::drop-down{
subcontrol-origin: padding;
subcontrol-position: top right;
width: 20px;
border-left-width: 1px;
border-left-color: darkgray;
}
QComboBox::down-arrow {
image: url(:/common/combobox_arrow);
}
QComboBox::down-arrow:on {
image: url(:/common/combobox_arrow_up);
}
QComboBox QAbstractItemView {
outline: 0;
border: 1px solid #327cc0;
background-color: #F1F3F3;
font: normal normal 14px "Microsoft YaHei";
}
QComboBox QAbstractItemView::item {
height: 32px;
color: #555555;
background-color: transparent;
}
QComboBox QAbstractItemView::item:hover {
color: #FFFFFF;
background-color: #327cc0;
}
QComboBox QAbstractItemView::item:selected {
color: #FFFFFF;
background-color: #327cc0;
}
QComboBox QAbstractScrollArea QScrollBar:vertical {
background-color: #d0d2d4;
}
QComboBox QAbstractScrollArea QScrollBar::handle:vertical {
background: rgb(160,160,160);
}
QComboBox QAbstractScrollArea QScrollBar::handle:vertical:hover {
background: rgb(90, 91, 93);
}
注意:
QCombox *box = new QComboBox();
box->setView(new QListView());