qt下拉框

QComboBox uses the model/view framework for its popup list and to store its items. By default a QStandardItemModel stores the items and a QListView subclass displays the popuplist.

QComboBox 使用模型/视图框架作为其弹出列表并存储其项目。默认情况下,QStandardItemModel 存储项目,QListView 子类显示弹出列表。

qss

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*下拉框*/
QComboBox
{
border-radius: 3px;
color: rgb(0,0,0);
border: 1px solid #c2c2c2;;
padding-top: 2px;
padding-left: 2px;
background-color: transparent;
}
QComboBox:disabled
{
color:rgb(160,160,160);
background-color: rgba(0, 0, 0, 0.3);
}
/*点击combox的样式*/
QComboBox:on
{
color: rgb(0, 0, 0);
padding-top: 4px;
padding-left: 4px;
background-color: rgba(0, 0, 0, 0.0);
border: 1px solid #c2c2c2;
border-radius: 5px;
}
/*下拉框的样式*/
QComboBox QAbstractItemView
{
height: 30px;
font-size: 16px;
outline: 0px solid gray; /*取消选中虚线*/
border: 1px solid rgb(66,66,66);
color: rgb(0,0,0);
background-color:rgba(0, 0, 0, 0.0);
}
/*选中每一项高度*/
QComboBox QAbstractItemView::item
{
color: rgb(255,255,255);
background-color: rgba(0, 0, 0, 0.3);
}
/*选中每一项的字体颜色和背景颜色*/
QComboBox QAbstractItemView::item:selected
{
color:rgba(75,175,255, 1);
background-color: rgba(0, 0, 0, 0.0);
}
/*下拉箭头的边框*/
QComboBox::drop-down
{
border:none;
}
/*下拉箭头样式*/
QComboBox::down-arrow
{
image: url(./res/comboboxdown.png);
}
/*下拉箭头点击样式*/
QComboBox::down-arrow:on
{
image: url(./res/comboboxdown1.png);
}

cpp

对话框类构造函数里面调用

1
2
3
4
5
6
// 重新设置QListView,否则qss QComboBox QAbstractItemView不生效
ui->comboBox->setView(new QListView(ui->comboBox));
// 不设置Popup则列表不能弹出;不设置FramelessWindowHint则不能无边框;不设置NoDropShadowWindowHint则右下角有默认三角形阴影。
ui->comboBox->view()->parentWidget()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint| Qt::NoDropShadowWindowHint);
// 设置背景透明,否则qss透明度不生效
ui->comboBox->view()->parentWidget()->setAttribute(Qt::WA_TranslucentBackground);

1