1、不可编辑时样式,背景、下拉框背景都为黑色,字体为白色
ui->comboBox->setStyleSheet("QComboBox::drop-down:pressed"
"{"//下拉按钮设置
"subcontrol-position:center right;"//按钮位置
"image:url(:/11.png);"//按钮图片
"}"
"QComboBox:drop-down"
"{"
"subcontrol-position:center right;"//
"image:url(:/13.png);"//
"}"
"QComboBox:drop-down:hover"
"{"
"subcontrol-position:center right;"
"image:url(:/12.png);"
"}"
"QComboBox"
"{"
"color: rgb(255, 255, 255);"
"border-width: 2px;"//0px透明
"border-style: solid;"
"border-color: rgb(0,0,0);"
"}"
"QComboBox::background"
"{"
"border:1px solid #000000;"
"padding:1px 18px 1px 3px;"
"min-width:100px;"
"font:12px;"
"}"
"QComboBox:!editable,QComboBox::drop-down:editable "
"{"
"background:qlineargradient(x1:0, y1:0, x2:1, y2:0,"
"stop:0 rgb(0,0,0), stop:1 rgb(0,0,0));"
//"background:transparent;"//背景透明
"}"
"QComboBox QAbstractItemView "
"{"
"border: 1px solid rgb(0, 0, 0);"
"background: rgb(0, 0, 0);"
"outline: none;"
"}"
"QComboBox QAbstractItemView::item "
"{"
"height: 25px;"
"color: rgb(255, 255, 255);"
"}"
"QComboBox QAbstractItemView::item:selected "
"{"
"background: rgb(73, 73, 73);"
"color: rgb(0, 0, 0);"
"}"
);
记得添加:ui->comboBox->setView(new QListView());
ui->comboBox->setStyleSheet("QComboBox::drop-down:pressed"
"{"//下拉按钮设置
"subcontrol-position:center right;"//按钮位置
"image:url(:/11.png);"//按钮图片
"}"
"QComboBox:drop-down"
"{"
"subcontrol-position:center right;"//
"image:url(:/13.png);"//
"}"
"QComboBox:drop-down:hover"
"{"
"subcontrol-position:center right;"
"image:url(:/12.png);"
"}"
"QComboBox"
"{"
"color: rgb(255, 255, 255);"
"border-width: 2px;"//0px透明
"border-style: solid;"
"border-color: rgb(0,0,0);"
"}"
"QComboBox::background"
"{"
"border:1px solid #000000;"
"padding:1px 18px 1px 3px;"
"min-width:100px;"
"font:12px;"
"}"
"QComboBox:!editable,QComboBox::drop-down:editable "
"{"
"background:qlineargradient(x1:0, y1:0, x2:1, y2:0,"
"stop:0 rgb(0,0,0), stop:1 rgb(0,0,0));"
//"background:transparent;"//背景透明
"}"
"QComboBox QAbstractItemView "
"{"
"border: 1px solid rgb(0, 0, 0);"
"background: rgb(0, 0, 0);"
"outline: none;"
"}"
"QComboBox QAbstractItemView::item "
"{"
"height: 25px;"
"color: rgb(255, 255, 255);"
"}"
"QComboBox QAbstractItemView::item:selected "
"{"
"background: rgb(73, 73, 73);"
"color: rgb(0, 0, 0);"
"}"
);
2、设置是否编辑
ui->comboBox->addItem("1"); //添加行
ui->comboBox->addItem("2");
ui->comboBox->addItem("3");
ui->comboBox->addItem("4");
ui->comboBox->addItem("5");
ui->comboBox->setEditable(true);//设置为可编辑
3、设置可编辑框的背景颜色
ui->comboBox->lineEdit()->setStyleSheet(
"QLineEdit"
"{"
"background:#000000;"
"}"
);
4、设置显示行数和获取显示行数
int maxVisibleItems() const//获取行数
void setMaxVisibleItems(int maxItems)//设置显示行数
5、设置滚动条
ui->combobox1->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Qt::ScrollBarAsNeeded//超过显示行数时显示
Qt::ScrollBarAlwaysOff//不显示滚动条
Qt::ScrollBarAlwaysOn//一直显示
6、代码设置选中
ui->combobox1->setCurrentIndex(0);
7、去掉弹窗动态
qApp->setEffectEnabled(Qt::UI_AnimateCombo, false);
8、列表文字居中显示
弹窗中行文字居中,弄一个for循环
static_cast<QStandardItemModel*>(combobox1->view()->model())->item(填写行号)->setTextAlignment(Qt::AlignCenter);
设置为可编辑后的combobox显示文字居中
ui->combobox1->lineEdit()->setAlignment(Qt::AlignCenter);
9、信号activated(int index),当用户在组合框中选择一个项目时发送此信号。项目的索引已通过。请注意,即使选择没有更改,也会发送此信号。
connect(comboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated),
[=](int index){ /* ... */ });
10、字体设置
ui->combobox1->setFont(字体);//组合框显示的字体
ui->combobox1->view()->setFont(ft);//下拉框上字体