QComboBox⾃定义设置样式⽰例:
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 */
borderbox
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::drop-down:checked{}
/
/设置下拉列表框样式
⽅法⼀
//QComboBox下拉列表不⽀持直接定义样式,需要在源代码中使⽤到样式委托对象QStyledItemDelegate
//⽤于下拉框item的样式美化
QStyledItemDelegate* itemDelegate = new QStyledItemDelegate();
uiboBox1->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);
}
⽅法⼆
//,当需要更加丰富的显⽰的时候,例如图⽚加⽂字等等,可以选择QListWidget;但是需要添加代理,才可以把数据写到combobox上。【使⽤listview时候,不需要代理】
//事例代码
m_listWidget = new QListWidget(this);
// 设置⼦项⽬代理,否则下拉框选项周围会出现虚线框
m_listWidget->setItemDelegate(new NoFocusFrameDelegate(this));
uiboBox->setEditable(true);
uiboBox->setModel(m_listWidget->model());
uiboBox->setView(m_listWidget);
// 在下拉框中添加5个选项
for (int i = 0; i < 5; ++i)
{
ComboboxItem* item = new ComboboxItem(this);//⾃定义的widget 类
item->setLabelContent(QString("Account") + QString::number(i, 10));
connect(item, SIGNAL(chooseAccount(const QString&)), this, SLOT(onChooseAccount(const QString&)));//不使⽤代理时候,可以使⽤槽函数来实现选择某⼀项。
QListWidgetItem* widgetItem = new QListWidgetItem(m_listWidget);
m_listWidget->setItemWidget(widgetItem, item);
}
⽅法三:使⽤listview
cbb->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
combox->setView(new QListView());
int count = combox->count();
for(int i=0; i<count; ++i)
{
static_cast< QStandardItemModel* >( combox->view()->model() )->item( i )->setTextAlignment( Qt::AlignCenter );
}
//qss
QComboBox QAbstractItemView{
outline: 0px;
font: 11pt "PingFang SC Medium";
background-color: rgb(255, 255, 255);
}
QComboBox QAbstractItemView::item{
margin-left:10px;
margin-right: 10px;
margin-top: 5px;
margin-bottom: 5px;
height: 52px;
border-radius: 5px;
border: 1px outset rgb(212,212,212);
border: 1px solid rgb(212,212,212);
background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0.320, stop:0 rgba(234, 234, 234, 255), stop:1 rgba(255, 255, 255, 255)); }
QComboBox QAbstractItemView::item:selected{
margin-left: 10px;
margin-right: 10px;
margin-top: 5px;
margin-bottom: 5px;
height: 52px;
border-radius: 5px;
color: rgb(255, 255, 255);
border: 1px outset rgb(30,139,195);
border: 1px solid rgb(96,173,215);
background-color: rgb(72,186,248);
}
3D效果
/* 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::down-arrow:on { /* shift the arrow when popup is open */
top: 1px;
left: 1px;
}
点击的时候,不是下拉⽽是弹窗的实现:使⽤事件过滤器
cbb->installEventFilter(this);
//需要注册的控件才会触发该事件
bool SampleProOptionDlg::eventFilter(QObject *obj, QEvent *ev)
{
if(ev->type() == QEvent::MouseButtonPress){
QComboBox *cbb = qobject_cast<QComboBox*> (obj);
if(nullptr != cbb){
slotTestType(cbb);
return true;//关键
}
}
return QDialog::eventFilter(obj, ev);
}
使⽤技巧
⾃动补全
使⽤QLineEdit配合使⽤
ui->comboBox->setLineEdit(ui->lineEdit);
QCompleter *completer = new QCompleter(users, this);
ui->lineEdit->setCompleter(completer);
//⾃动补全下拉列表样式可以通过completer->popup()去设置。和QCombobox下拉列表⼀样
如果需要把值⾃动填⼊需要加⼊槽函数实现
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
word_list<<"Java"<<"C++"<<"C#"<<"PHP"<<"Perl"<<"Python"<<"Delphi"<<"Ruby";
search_line_edit = new QLineEdit(this);
completer = new QCompleter(this);
string_list_model = new QStringListModel(word_list, this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setModel(string_list_model);
search_line_edit->setCompleter(completer);
connect(search_line_edit, SIGNAL(editingFinished()), this, SLOT(editComplete()));
}
void Widget::editComplete()
{
QString text = search_line_edit->text();
if(QString::compare(text, QString("")) != 0) {
bool is_contains = ains(text, Qt::CaseInsensitive);
if(!is_contains) {
word_list<<text;
string_list_model->setStringList(word_list);
//completer->setModel(new QStringListModel(wordList, this));
}
}
}
每次编译完成后按回车键,会将不存在列表中的⽂本加⼊到改列表中。Qt::CaseSensitivity取值,Qt::CaseInsensitive:⼤⼩写不敏感;Qt::CaseSensitive:⼤⼩写敏感。默认为:Qt::CaseSensitive。
补全路径
{
QDirModel *model = new QDirModel(this);
search_line_edit = new QLineEdit(this);
completer = new QCompleter(this);
completer->setModel(model);
search_line_edit->setCompleter(completer);
}
/*普通常⽤*/
QComboBox QAbstractItemView
{
border: 1px solid #d2d2d2;
outline: 0px;
font: 13pt "思源⿊体 CN Medium";
background-color: rgb(255, 255, 255);
border-top: 1px solid rgb(62, 179, 233);
}
QComboBox QAbstractItemView::item
{
margin-left:10px;
margin-right: 10px;
margin-top: 5px;
margin-bottom: 5px;
height: 52px;
border-radius: 5px;
border: 1px outset rgb(212,212,212);
border: 1px solid rgb(212,212,212);
background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0.320, stop:0 rgba(234, 234, 234, 255), stop:1 rgba(255, 255, 255, 255)); }
QComboBox QAbstractItemView::item:selected
{
margin-left: 10px;
margin-right: 10px;
margin-top: 5px;
margin-bottom: 5px;
height: 52px;
border-radius: 5px;
color: rgb(255, 255, 255);
border: 1px outset rgb(30,139,195);
border: 1px solid rgb(96,173,215);
background-color: rgb(30,139,195);
}
QComboBox
{
height:41px;
color: #444444;
padding-left:5px;
font: 13pt "思源⿊体 CN Medium";
border-radius: 2px;
border: 1px solid rgb(195, 195, 195);
}
QComboBox:checked
{
border: 1px solid rgb(62, 179, 233);
}
QComboBox::drop-down
{
width:39px;
height:41px;
background-image: url(:/Style/img/QComboBox/comBox_uncheck(39x41).png);
}
QComboBox::drop-down:checked
{
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论