Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to solve the qApp`s QStyleSheetStyle and QProxyStyle issue



  • First, I need a scroll bar who is overlapped. the overlapped scroll bar is suspend on viewport, and make it alpha value, then, the viewport will be greater.

    This is the style sheet:

    /* WBOverlapScrollBar */
    /* 各种表,列表用的滚动条,用类名区别于容器滚动条 */
    WBOverlapScrollBar {
        margin: 0px;
        border: none;
        border-radius: 2px;
        padding: 0px;
        background: transparent !important;
    }
    
    WBOverlapScrollBar:vertical
    {
        width: 7px;
        margin: 0px;
        border: 0px;
        padding: 0px;
    }
    
    WBOverlapScrollBar::handle:vertical
    {
        min-height: 60px;
        margin: 0px 1px 0px 2px;
        border: none;
        border-radius: 2px;
        padding: 0px;
        background: rgba(0,0,0,20%);
    }
    
    WBOverlapScrollBar::handle:vertical:hover {
        margin: 0px 1px 0px 0px;
        border-radius: 3px;
        background: rgba(0, 0, 0, 30%);
    }
    
    WBOverlapScrollBar:horizontal
    {
        height: 7px;
        margin: 0px;
        border: 0px;
        padding: 0px;
    }
    
    WBOverlapScrollBar::handle:horizontal
    {
        min-width: 60px;
        margin: 2px 0px 1px 0px;
        border: none;
        border-radius: 2px;
        padding: 0px;
        background: rgba(0,0,0,20%);
    }
    
    WBOverlapScrollBar::handle:horizontal:hover {
        margin: 0px 0px 1px 0px;
        border-radius: 3px;
        background: rgba(0, 0, 0, 30%);
    }
    
    WBOverlapScrollBar::add-line,
    WBOverlapScrollBar::sub-line,
    WBOverlapScrollBar::add-page,
    WBOverlapScrollBar::sub-page {
        width: 0px;
        height: 0px;
        background: transparent;
    }
    

    This is proxy stype:

    #include "wbcontrols_global.h"
    #include <QProxyStyle>
    
    class WB_CONTROLS_EXPORT WBOverlapScrollBarStyle : public QProxyStyle
    {
        Q_OBJECT
    
    public:
        WBOverlapScrollBarStyle(QStyle *style = nullptr);
    
    protected:
        int pixelMetric(PixelMetric m,
                        const QStyleOption *opt = nullptr,
                        const QWidget *widget = nullptr) const override;
    
        int styleHint(StyleHint sh,
                      const QStyleOption *opt = nullptr,
                      const QWidget *w = nullptr,
                      QStyleHintReturn *shret = nullptr) const override;
    };
    
    WBOverlapScrollBarStyle::WBOverlapScrollBarStyle(QStyle *style) : QProxyStyle(style)
    {
    
    }
    
    int WBOverlapScrollBarStyle::pixelMetric(QStyle::PixelMetric m, const QStyleOption *opt, const QWidget *widget) const
    {
        ///
        /// 设置滚动条层叠于视口上
        ///
        if (QStyle::PM_ScrollView_ScrollBarOverlap == m) {
            return 100;
        }
        return QProxyStyle::pixelMetric(m, opt, widget);
    }
    
    int WBOverlapScrollBarStyle::styleHint(QStyle::StyleHint sh, const QStyleOption *opt, const QWidget *w, QStyleHintReturn *shret) const
    {
        ///
        /// 这个枚举文不达意,或是没有实现;
        /// 但是,它可以让滚动条霸占整个表的高度/宽度.
        ///
        if (QStyle::SH_ScrollBar_Transient == sh) {
           // int i = 0;
           return 1;
        }
        return QProxyStyle::styleHint(sh, opt, w, shret);
    }
    

    My app has a qApp level`s style sheet, when I type style sheet of WBOverlapScrollBar in it, WBOverlapScrollBarStyle will be not effect; it will be effect but type it to other position. I have some idea, but no time to check. any experence to me, Thanks!

    void QWidget::setStyle(QStyle *style)
    {
        Q_D(QWidget);
        setAttribute(Qt::WA_SetStyle, style != 0);
        d->createExtra();
    #ifndef QT_NO_STYLE_STYLESHEET
        if (QStyleSheetStyle *styleSheetStyle = qt_styleSheet(style)) {
            //if for some reason someone try to set a QStyleSheetStyle, ref it
            //(this may happen for exemple in QButtonDialogBox which propagates its style)
            styleSheetStyle->ref();
            d->setStyle_helper(style, false);
        } else if (qt_styleSheet(d->extra->style) || !qApp->styleSheet().isEmpty()) {
            // if we have an application stylesheet or have a proxy already, propagate
            d->setStyle_helper(new QStyleSheetStyle(style), true);
        } else
    #endif
            d->setStyle_helper(style, false);
    }
    


  • @spobit
    SH_ScrollBar_Transient has the declaration:

    Determines if the style supports transient scroll bars. Transient scroll bars appear when the content is scrolled and disappear when they are no longer needed.
    

    But, it does not work on windows 10 qt-5.12.4.


  • Lifetime Qt Champion

    Hi
    I thought only MacOs has support for
    SH_ScrollBar_Transient
    not Windows. 10 ?

    Maybe others know for sure if it can work or not.


Log in to reply