部分样式需要设置窗口属性才能生效
ui->comboBox->setView(new QListView());
ui->comboBox->view()->window()->setWindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);
ui->comboBox->view()->window()->setAttribute(Qt::WA_TranslucentBackground);
样式表
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);
}
效果一
QComboBox *ComBox=new QComboBox(this);
ComBox->setGeometry(0,0,209,45);
ComBox->addItem(tr("1小时"));
ComBox->addItem(tr("2小时"));
ComBox->addItem(tr("3小时"));
ComBox->addItem(tr("4小时"));
ComBox->setStyleSheet("QComboBox{"//基本样式
"border-image:url(:/Sys/output-box.png);"
"font:黑体;"
"font-size:20px;"
"color:rgb(255,255,255);"
"background-color:transparent;"
"width:209px;"
"height:45px;"
"}"
"QComboBox::down-arrow{"//下拉三角样式
"image:url(:/Sys/btn_pulldown.png);"
"background-color:transparent;"
"width:36px;"
"height:22px;"
"right:2px;"
"}"
"QComboBox QAbstractItemView {"//Box内条目
"outline:0px;" //去掉虚线
"}"
"QComboBox::drop-down {"
"subcontrol-origin: padding;"
"subcontrol-position: top right;"
"width: 36px;"
"}"
);
效果二
一个QLabel和QComboBox组成 设置边框颜色 ComboBox三角箭头
#ifndef PROCOMBOBOX_H
#define PROCOMBOBOX_H
#include <QComboBox>
#include <QLabel>
#include <QWidget>
class ProComboBox :public QWidget
{
public:
ProComboBox(QWidget *parent);
void init();
void setLwCwAhsize(int LabelW,int ComboBoxW,int AllH); //Label width ComboBox width All heigth
QLabel *m_Label;
QComboBox *m_ComboBox;
};
#endif // PROCOMBOBOX_H
#include "ProComboBox.h"
ProComboBox::ProComboBox(QWidget *parent = 0)
{
this->setParent(parent);
init();
}
void ProComboBox::init()
{
m_Label = new QLabel(this);
m_Label->setStyleSheet("QLabel{"
"background-color:transparent;"
"color:rgb(0,190,255);"
"border:1px solid #00beff;"
"border-top-left-radius:5px;"
"border-top-right-radius:0px;"
"border-bottom-left-radius:5px;"
"border-bottom-right-radius:0px;}"
);
m_ComboBox = new QComboBox(this);
m_ComboBox->setStyleSheet("QComboBox{"
"background-color:transparent;"
"color:rgb(255,255,255);"
"border:1px solid #00beff;"
"border-top-left-radius:0px;"
"border-top-right-radius:5px;"
"border-bottom-left-radius:0px;"
"border-bottom-right-radius:5px;}"
"QComboBox::drop-down{"
"background-color:transparent;"
"image:url(:/image/boxpull.png);"
"width:26px;right:5px;}"
);
setLwCwAhsize(50,50,34);
}
void ProComboBox::setLwCwAhsize(int LabelW, int ComboBoxW, int AllH)
{
m_Label->setGeometry(0,0,LabelW,AllH);
m_ComboBox->setGeometry(LabelW,0,ComboBoxW,AllH);
resize(LabelW + ComboBoxW,AllH);
}