Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set a custom style into the QScrollBar of a QScrollArea?
Forum Updated to NodeBB v4.3 + New Features

How to set a custom style into the QScrollBar of a QScrollArea?

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 4 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CesarC Cesar

    @Abderrahmene_Rayene i already had the flag ScrollBarAlwaysOn set on, and im testing with a QScrollArea that the QScrollBar is visible.

    At my side its still not getting called, even applying the style on the QMainWindow / QScrollArea QScrollBar

    C Offline
    C Offline
    CPPUIX
    wrote on last edited by CPPUIX
    #11

    @Cesar Then there's something else you should mention, because it does call drawComplex on my end, after I replicated what you posted, and the ScrollBar is drawn red and without arrows.

    This is how it looks like:

    scrollbar.png

    JonBJ CesarC 3 Replies Last reply
    0
    • C CPPUIX

      @Cesar Then there's something else you should mention, because it does call drawComplex on my end, after I replicated what you posted, and the ScrollBar is drawn red and without arrows.

      This is how it looks like:

      scrollbar.png

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #12

      @Abderrahmene_Rayene
      You don't put a debug inside the if (sliderOption) to tell us, but are you saying that

      const QStyleOptionSlider* sliderOption = qstyleoption_cast<const QStyleOptionSlider*>(option);
      

      is returning nullptr/so false for the if? You never get to fprintf(stderr,"innercomplex\n");?

      C 1 Reply Last reply
      0
      • JonBJ JonB

        @Abderrahmene_Rayene
        You don't put a debug inside the if (sliderOption) to tell us, but are you saying that

        const QStyleOptionSlider* sliderOption = qstyleoption_cast<const QStyleOptionSlider*>(option);
        

        is returning nullptr/so false for the if? You never get to fprintf(stderr,"innercomplex\n");?

        C Offline
        C Offline
        CPPUIX
        wrote on last edited by
        #13

        @JonB I have edited my reply, see if you can correct what's wrong or provide a clarification.

        1 Reply Last reply
        0
        • C CPPUIX

          @Cesar Then there's something else you should mention, because it does call drawComplex on my end, after I replicated what you posted, and the ScrollBar is drawn red and without arrows.

          This is how it looks like:

          scrollbar.png

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #14

          @Abderrahmene_Rayene said in How to set a custom style into the QScrollBar of a QScrollArea?:

          if (control == CC_ScrollBar && option->state & State_Horizontal)

          Where do you (or the OP) get option->state & State_Horizontal from? I see Qt::Orientation QStyleOptionSlider::orientation? Do not assume QStyle::State_Horizontal is what you need for a QScrollbar....

          C 1 Reply Last reply
          0
          • JonBJ JonB

            @Abderrahmene_Rayene said in How to set a custom style into the QScrollBar of a QScrollArea?:

            if (control == CC_ScrollBar && option->state & State_Horizontal)

            Where do you (or the OP) get option->state & State_Horizontal from? I see Qt::Orientation QStyleOptionSlider::orientation? Do not assume QStyle::State_Horizontal is what you need for a QScrollbar....

            C Offline
            C Offline
            CPPUIX
            wrote on last edited by CPPUIX
            #15

            @JonB OP is using this function:

            [override virtual] void QProxyStyle::drawComplexControl(QStyle::ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = nullptr) const
            

            which should get called to draw the scrollBar.

            As for State_Horizontal, it didn't work for me because I was setting the style for the vertical scroll bar, my bad. It works fine with the horizontal scroll bar. So why shouldn't one use it for QScrollBar.

            Sorry for the over-editing, I'm replying and testing simultaneously, I have included an image to display the outcome, it works fine, I can't replicate OP's problem.

            JonBJ 1 Reply Last reply
            0
            • C CPPUIX

              @Cesar Then there's something else you should mention, because it does call drawComplex on my end, after I replicated what you posted, and the ScrollBar is drawn red and without arrows.

              This is how it looks like:

              scrollbar.png

              CesarC Offline
              CesarC Offline
              Cesar
              wrote on last edited by
              #16

              @Abderrahmene_Rayene im not sure if i understood what you did, could you post a reproducible example of the code you're using?

              C 1 Reply Last reply
              0
              • CesarC Cesar

                @Abderrahmene_Rayene im not sure if i understood what you did, could you post a reproducible example of the code you're using?

                C Offline
                C Offline
                CPPUIX
                wrote on last edited by CPPUIX
                #17

                @Cesar I had TQScrollArea and ScrollBarStyle in a separate header file, but I included them in the mainwindow header so that I don't post 3 files contents.

                mainwindow.cpp:

                #include "mainwindow.h"
                #include "./ui_mainwindow.h"
                
                MainWindow::MainWindow(QWidget *parent)
                    : QMainWindow(parent)
                    , ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                
                
                    TQScrollArea *area = new TQScrollArea(this);
                    QWidget *widget = new QWidget();
                    widget->setGeometry(0,0,700,400);
                    area->setWidget(widget);
                    area->horizontalScrollBar()->setStyle(new ScrollBarStyle);
                    area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
                    area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
                
                    area->setGeometry(0,0,500,300);
                }
                
                MainWindow::~MainWindow()
                {
                    delete ui;
                }
                

                mainwindow.h:

                #ifndef MAINWINDOW_H
                #define MAINWINDOW_H
                
                #include <QMainWindow>
                #include <QWidget>
                #include <QScrollArea>
                #include <QObject>
                #include <QWidget>
                #include <QScrollBar>
                #include <QDebug>
                #include <QStyleOptionSlider>
                #include <QPainter>
                #include <QProxyStyle>
                
                class ScrollBarStyle : public QProxyStyle
                {
                public:
                
                
                    void drawControl(ControlElement element, const QStyleOption *option,
                                     QPainter *painter, const QWidget *widget) const override
                    {
                
                        fprintf(stderr,"before\n");
                        if (element == CE_ScrollBarSlider)
                        {
                            fprintf(stderr,"after\n");
                            const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(option);
                            if (slider)
                            {
                            }
                        }
                        else
                        {
                            QProxyStyle::drawControl(element, option, painter, widget);
                        }
                    }
                
                    void drawComplexControl(ComplexControl control, const QStyleOptionComplex*
                                                                        option,  QPainter* painter, const QWidget* widget) const override
                    {
                        fprintf(stderr,"beforecomplex\n");
                
                        if (control == CC_ScrollBar && option->state & State_Horizontal)
                        {
                            fprintf(stderr,"aftercomplex\n");
                            const QStyleOptionSlider* sliderOption = qstyleoption_cast<const QStyleOptionSlider*>(option);
                            if (sliderOption)
                            {
                                fprintf(stderr,"innercomplex\n");
                                QRect handleRect = subControlRect(control, option, SC_ScrollBarSlider, widget);
                                painter->setBrush(Qt::red);
                                painter->drawRect(handleRect);
                                return;
                            }
                        }
                
                        QProxyStyle::drawComplexControl(control, option, painter, widget);
                
                    }
                };
                
                class TQScrollArea : public QScrollArea
                {
                    Q_OBJECT
                public:
                
                    TQScrollArea(QWidget* parent = 0) : QScrollArea(parent)
                    {
                        this->verticalScrollBar()->setStyle(new ScrollBarStyle);
                    }
                };
                
                QT_BEGIN_NAMESPACE
                namespace Ui { class MainWindow; }
                QT_END_NAMESPACE
                
                class MainWindow : public QMainWindow
                {
                    Q_OBJECT
                
                public:
                    MainWindow(QWidget *parent = nullptr);
                    ~MainWindow();
                
                private:
                    Ui::MainWindow *ui;
                
                };
                #endif // MAINWINDOW_H
                

                mainc.cpp is just the regular autogenerated one, didn't touch it.

                1 Reply Last reply
                0
                • C CPPUIX

                  @JonB OP is using this function:

                  [override virtual] void QProxyStyle::drawComplexControl(QStyle::ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = nullptr) const
                  

                  which should get called to draw the scrollBar.

                  As for State_Horizontal, it didn't work for me because I was setting the style for the vertical scroll bar, my bad. It works fine with the horizontal scroll bar. So why shouldn't one use it for QScrollBar.

                  Sorry for the over-editing, I'm replying and testing simultaneously, I have included an image to display the outcome, it works fine, I can't replicate OP's problem.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #18

                  @Abderrahmene_Rayene said in How to set a custom style into the QScrollBar of a QScrollArea?:

                  As for State_Horizontal, it didn't work for me because I was setting the style for the vertical scroll bar, my bad. It works fine with the horizontal scroll bar. So why shouldn't one use it for QScrollBar.

                  At the time you were saying the condition was not being entered. I didn't say option->state & State_Horizontal would not work, I said to check assumption, since there isa specific QStyleOptionSlider::orientation == Qt::Horizontal for a slider style option.

                  C 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Abderrahmene_Rayene said in How to set a custom style into the QScrollBar of a QScrollArea?:

                    As for State_Horizontal, it didn't work for me because I was setting the style for the vertical scroll bar, my bad. It works fine with the horizontal scroll bar. So why shouldn't one use it for QScrollBar.

                    At the time you were saying the condition was not being entered. I didn't say option->state & State_Horizontal would not work, I said to check assumption, since there isa specific QStyleOptionSlider::orientation == Qt::Horizontal for a slider style option.

                    C Offline
                    C Offline
                    CPPUIX
                    wrote on last edited by
                    #19

                    @JonB thanks, that's a useful tip.

                    1 Reply Last reply
                    0
                    • CesarC Cesar

                      I have created a custom QProxyStyle "ScrollBarStyle" and im setting it on the vertical scrollbar of a QScrollArea

                      but the functions drawControl / drawComplexControl never get called, i also tried using QCommonStyle, same problem.

                      What I'm missing?

                      class ScrollBarStyle : public QProxyStyle
                      {
                      public:
                         void drawControl(ControlElement element, const QStyleOption *option,
                                                          QPainter *painter, const QWidget *widget) const override
                         {
                            qDebug() << "element: " << element;
                      
                            if (element == CE_ScrollBarSlider)
                            {
                               const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(option);
                               if (slider)
                               {
                               }
                            }
                            else
                            {
                               QProxyStyle::drawControl(element, option, painter, widget);
                            }
                         }
                      
                      
                      
                         void drawComplexControl(ComplexControl control, const QStyleOptionComplex* 
                            option,  QPainter* painter, const QWidget* widget) const override
                         {
                            qDebug() << "control: " << control;
                      
                            if (control == CC_ScrollBar && option->state & State_Horizontal)
                            {
                               const QStyleOptionSlider* sliderOption = qstyleoption_cast<const QStyleOptionSlider*>(option);
                               if (sliderOption)
                               {
                                     QRect handleRect = subControlRect(control, option, SC_ScrollBarSlider, widget);
                                     painter->setBrush(Qt::red);
                                     painter->drawRect(handleRect);
                                     return;
                               }
                            }
                      
                            QProxyStyle::drawComplexControl(control, option, painter, widget);
                      
                         }
                      };
                      
                      
                      class ScrollArea : public QScrollArea
                      {
                          Q_OBJECT
                      public:
                      
                          ScrollArea(QWidget* parent = 0) : QScrollArea(parent) 
                          {
                              //this->setStyle(new ScrollBarStyle);
                              this->horizontalScrollBar()->setStyle(new ScrollBarStyle);
                          }
                      }
                      C Offline
                      C Offline
                      CPPUIX
                      wrote on last edited by
                      #20

                      @Cesar Btw you said you're setting style for the vertical scroll bar, but your code is handling horizontal bar style.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved