StyleSheets

scrollbar stylesheet

-------------------------------------------

this->scrollBarWidgets(Qt::AlignBottom);

    this->scrollBarWidgets(Qt::AlignRight);
 
    this->verticalScrollBar()->setStyleSheet("QScrollBar:vertical"
                                            "{"
                                            "width:8px;"
                                            "background:rgba(0,0,0,0%);"
                                            "margin:0px,10px,0px,10px;"
                                            "padding-top:2px;"
                                            "padding-bottom:2px;"
                                            "}"
                                            "QScrollBar::handle:vertical"
                                            "{"
                                            "width:8px;"
                                            "background:rgba(0,0,0,25%);"
                                            " border-radius:3px;"
                                            "min-height:50;"
                                            "}"
                                            "QScrollBar::handle:vertical:hover"
                                            "{"
                                            "width:8px;"
                                            "background:rgba(0,0,0,50%);"
                                            " border-radius:3px;"
                                            "min-height:50;"
                                            "}"
                                            "QScrollBar::add-line:vertical"
                                            "{"
                                            "height:0px;width:8px;"
                                            "subcontrol-position:bottom;"
                                            "}"
                                            "QScrollBar::sub-line:vertical"
                                             "{"
                                            "height:0px;width:8px;"
                                            "subcontrol-position:top;"
                                            "}"
                                            "QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical"
                                            "{"
                                            "background:rgba(0,0,0,10%);"
                                            "border-radius:3px;"
                                            "}"
                                            );
 
    this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal"
                                               "{"
                                               "height:8px;"
                                               "background:rgba(0,0,0,0%);"
                                               "padding-right:2px;"
                                               "padding-left:2px;"
                                               "}"
                                               "QScrollBar::handle:horizontal"
                                               "{"
                                               "height:8px;"
                                               "background:rgba(0,0,0,25%);"
                                               " border-radius:3px;"
                                               "min-width:50;"
                                               "}"
                                               "QScrollBar::handle:horizontal:hover"
                                               "{"
                                               "height:8px;"
                                               "background:rgba(0,0,0,50%);"
                                               " border-radius:3px;"
                                               "min-width:50;"
                                               "}"
                                               "QScrollBar::add-line:horizontal"
                                               "{"
                                               "width:0px;height:8px;"
                                               "subcontrol-position:right;"
                                               "}"
                                               "QScrollBar::sub-line:horizontal"
                                                "{"
                                               "width:0px;height:8px;"
                                               "subcontrol-position:left;"
                                               "}"
                                               "QScrollBar::add-page:horizontal,QScrollBar::sub-page:horizontal"
                                               "{"
                                               "background:rgba(0,0,0,10%);"
                                               "border-radius:3px;"
                                               "}"
                                               );
--------------------------------------------

treeview stylesheet

--------------------------------------------

m_treeView->setStyleSheet( QString("QTreeView{ border-image:url(%1/images/ID_DM_LEFT_BG.png);padding-left:%5px;padding-top:%5px;color:rgb(15,108,181);font-size:%4px;}\
                                 QTreeView{show-decoration-selected:1;}\
                                 QTreeView::item{margin:%2px;height:%3px;}\
                                 QTreeView::item:selected{color:rgb(15,108,181);border-image:url(%1/images/ID_DM_TREEITEM_ACTIVED.png);}\
                                 QTreeView::item:has-children {color:rgb(15,108,181);}\
                                 QTreeView::item:!has-children {color:rgb(15,108,181);}\
                                 QTreeView::branch{background:rgb(242,242,242);}\
                                 QTreeView::branch:has-siblings:adjoins-item {border-image:url(%1/images/ID_DM_TREEITEM_BRANCHMORE.png) 0;}\
                                 QTreeView::branch:!has-children:!has-siblings:adjoins-item {border-image:url(%1/images/ID_DM_TREEITEM_BRANCHEND.png) 0;}\
                                 QTreeView::branch:closed:has-children{border-image:none;image:url(%1/images/ID_DM_TREEITEM_FOLD.png);}\
                                 QTreeView::branch:open:has-children{border-image:none;image:url(%1/images/ID_DM_TREEITEM_UNFOLD.png);}\
                                 ").arg(wsImagePath).arg(13*gRatioH).arg(25*gRatioV).arg(12*gRatioH).arg(2*gRatioH));
     //m_treeView->header()->setStyleSheet( "QHeaderView{ background-color:#585858 ; color:rgb(201,204,217);font-size:30px}" );
     m_treeView->header()->setStyleSheet( QString("QHeaderView{ background-color:#F2F2F2; color:rgb(15,108,181);font:bold;font-size:%1px;}").arg(18*gRatioH));

--------------------------------------------

QStringtempStyle=QString("QTreeView{\

                                border:0px solid dfdfdf;\
                                color:#f2f2f2;\
                                outline: none;\
                                show-decoration-selected: 1\
                                }\
                                QTreeView::item{\
                                height:20px;\
                                color:#333333;font-size:12px;font-family:'微软雅黑';\
                                }\
                                QTreeView::item:selected{\
                                background: #f2f2f2;\
                                }\
                                QTreeView::branch{\
                                    image:none;\
                                    background: #f2f2f2;\
                                }\
                                QTreeView::branch:open:has-children:!has-siblings, \
                                QTreeView::branch:open:has-children:has-siblings {\
                                 background: #f2f2f2;\
                                }\
                                QTreeView::branch:open:has-children:!has-siblings:selected, \
                                QTreeView::branch:open:has-children:has-siblings:selected {\
                                 background: #f2f2f2;\
                                }\
                                QTreeView::branch:has-children:!has-siblings:closed, \
                                QTreeView::branch:closed:has-children:has-siblings {\
                                 background: #f2f2f2;\
                                }\
                                QTreeView::branch:has-children:!has-siblings:closed:selected, \
                                QTreeView::branch:closed:has-children:has-siblings:selected {\
                                 background: #f2f2f2;\
                                }\
                            ");
 
                                m_treeWidget->setStyleSheet(tempStyle);

treeveiw去掉虚线框

        m_treeWidget->setStyleSheet("QTreeView::item{height:32px;font:bold;font-size:24px}");

    m_treeWidget->setRootIsDecorated( false ); //remove dotted line border frame
    m_treeWidget->setFrameStyle(QFrame::NoFrame); //remove border frame
    m_treeWidget->setStyleSheet("QTreeView::branch {image:none;}"); //remove children's dotted line border frame
 

treeveiw

通过 Qt之QTreeView(一)即可正常加载数据, Qt之QTreeView(二)即可自定义图标、按钮,注册事件等。。。那么,到这里,基本的使用已经完成,为了界面的美观性,这里提供三种样式以供选择。
样式一:
QString styleOne = "QTreeView{\
        border: 1px solid lightgray;\
    }\
    QTreeView::item {\
            height: 40px;\
            border-radius: 2px;\
            border: 1px solid transparent;\
            background: transparent;\
            color: black;\
    }\
    QTreeView::item:hover {\
            border: 1px solid rgb(185, 215, 250);\
    }\
    QTreeView::item:selected {\
            border: 1px solid rgb(170, 190, 230);\
            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 rgb(230, 240, 250), stop: 0.5 rgb(220, 235, 255), stop: 1.0 rgb(210, 230, 255));\
    }\
    QTreeView::branch:open:has-children {\
            image: url(./Resources/Images/branchOpen.png);\
    }\
    QTreeView::branch:closed:has-children {\
            image: url(./Resources/Images/branchClose.png);\
    }";
效果如下:
Qt之QTreeView(三)

样式二:
QString styleTwo = "QTreeView{\
            border: 1px solid lightgray;\
    }\
    QTreeView::item {\
            height: 40px;\
            border-radius: 2px;\
            border: 1px solid transparent;\
            background: transparent;\
            color: black;\
    }\
    QTreeView::item:has-children {\
            border: none;\
            border-bottom: 1px solid lightgray;\
    }\
    QTreeView::item:hover {\
            border: 1px solid rgb(170, 190, 230);\
            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 rgb(230, 240, 250), stop: 0.5 rgb(220, 235, 255), stop: 1.0 rgb(210, 230, 255));\
    }\
    QTreeView::item:selected {\
            border: 1px solid rgb(170, 190, 230);\
            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 rgb(230, 240, 250), stop: 0.5 rgb(220, 235, 255), stop: 1.0 rgb(210, 230, 255));\
    }\
    QTreeView::branch:open:has-children {\
            image: url(./Resources/Images/branchOpen2.png);\
    }\
    QTreeView::branch:closed:has-children {\
            image: url(./Resources/Images/branchClose2.png);\
    }";

效果如下:
Qt之QTreeView(三)

样式三:
QString styleThree = "QTreeView{\
            border: 1px solid lightgray;\
    }\
    QTreeView::item {\
            height: 25px;\
            border: none;\
            background: transparent;\
            color: black;\
    }\
    QTreeView::item:hover {\
            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 rgb(255, 220, 130), stop: 0.5 rgb(255, 220, 130), stop: 1.0 rgb(255, 230, 150));\
    }\
    QTreeView::item:selected {\
            background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 rgb(255, 200, 100), stop: 0.5 rgb(255, 230, 150), stop: 1.0 rgb(255, 240, 150));\
    }\
    QTreeView::branch:open:has-children {\
            image: url(./Resources/Images/branchOpen.png);\
    }\
    QTreeView::branch:closed:has-children {\
            image: url(./Resources/Images/branchClose.png);\
    }";
效果如下:
Qt之QTreeView(三)

    以上主要通过对边框、背景、文本、滑过、选中来设置不同的样式,仅供参考。如要实现自己想要的效果,那么可以好好研究下QSS!
    好了,介绍了这么多,还是把源码奉上,地址: 自定义QTreeView(详解)


注:
   技术在于交流、沟通,转载请注明出处并保持作品的完整性。

QStringstrOpenIconPath=QApplication::applicationDirPath()+"/plugin/resource/images/16X16.png";

    QString strCloseIconPath = QApplication::applicationDirPath() + "/plugin/resource/images/16X16.png";
 
    QString strOpenSelIconPath = QApplication::applicationDirPath() +"/plugin/resource/images/16X16.png";
    QString strCloseSelIconPath = QApplication::applicationDirPath() + "/plugin/resource/images/16X16.png";
 
    QString tempStyle = QString("QTreeView{\
                                border:0px solid gray;\
                                color:gray;\
                                outline: none;\
                                show-decoration-selected: 1\
                                }\
                                QTreeView::item{\
                                height:20px;\
                                color:#333333;font-size:12px;font-family:'微软雅黑';\
                                }\
                                QTreeView::item:selected{\
                                background: #41b700;\
                                }\
                                QTreeView::branch{\
                                    image:none;\
                                    background: white;\
                                }\
                                QTreeView::branch:open:has-children:!has-siblings, \
                                QTreeView::branch:open:has-children:has-siblings {\
                                 background: white;\
                                 image: url(%1);\
                                }\
                                QTreeView::branch:open:has-children:!has-siblings:selected, \
                                QTreeView::branch:open:has-children:has-siblings:selected {\
                                 background: #41b700;\
                                 image: url(%3);\
                                }\
                                QTreeView::branch:has-children:!has-siblings:closed, \
                                QTreeView::branch:closed:has-children:has-siblings {\
                                 background: white;\
                                 image: url(%2);\
                                }\
                                QTreeView::branch:has-children:!has-siblings:closed:selected, \
                                QTreeView::branch:closed:has-children:has-siblings:selected {\
                                 background: #41b700;\
                                 image: url(%4);\
                                }\
                            ").arg(strOpenIconPath).arg(strCloseIconPath).arg(strOpenSelIconPath).arg(strCloseSelIconPath);
 
 
    m_pTreeWidget->setDirViewStyleSheet(tempStyle);
    m_pTreeWidget->setObjectName("userTreeWidget");
    m_pTreeWidget->setStyleSheet(QString("QWidget#%1{border:1px solid gray;border-right:0px solid gray;border-bottom:0px solid gray;}")\
                                    .arg(m_pTreeWidget->objectName()));



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值