QT QComBox详细用法 自定义QComboBox控件

部分样式需要设置窗口属性才能生效

    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);
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是唐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值