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

QScrollBar Implementation Uses 2 Styles?



  • I'm having an issue related to the value of QStyle::PM_ScrollBarExtent pixel metric so I created a QProxyStyle that just returns 0 for that metric. The issue is QScrollBar has one style for its public implementation, and another for its QScrollBarPrivate implementation, so my proxy style isn't called when I would like it to be.

    Does anyone know why that's the case, and is there a way to override that private style?


  • Lifetime Qt Champion

    Ok, I understand your issue better. The key information missing was that you are using a style sheet. So indeed, there's a dedicated internal style that is used when you set a style sheet.

    Depending on the exact outcome you want, you should do all in your custom style.


  • Lifetime Qt Champion

    Hi,

    Can you show what is happening when using your proxy ?



  • @SGaist, I isolated a related issue with a relatively small example, showing that the displayed scroll bar size doesn't match the returned size for the SC_ScrollBarSlider sub-control rectangle:

    #include <QApplication>
    #include <QDebug>
    #include <QLabel>
    #include <QScrollArea>
    #include <QScrollBar>
    #include <QStyleOption>
    
    class ScrollBar : public QScrollBar {
      public:
    
        ScrollBar(QWidget* parent = nullptr) : QScrollBar(parent) {}
    
      protected:
        void paintEvent(QPaintEvent* event) override {
          auto style_option = QStyleOptionSlider();
          initStyleOption(&style_option);
          auto slider_rect = style()->subControlRect(QStyle::CC_ScrollBar,
            &style_option, QStyle::SC_ScrollBarSlider);
          qDebug() << slider_rect;
          QScrollBar::paintEvent(event);
        }
    };
    
    int main(int argc, char** argv) {
      auto application = new QApplication(argc, argv);
      auto w = new QScrollArea();
      w->setStyleSheet(QString(R"(
        QScrollBar::vertical {
          border: none;
          width: 13px;
        }
    
        QScrollBar::handle {
          background-color: red;
          min-height: 60px;
        }
    
        QScrollBar::add-line, QScrollBar::sub-line,
        QScrollBar::add-page, QScrollBar::sub-page,
        QScrollBar::up-arrow, QScrollBar::down-arrow {
          background: none;
          border: none;
          height: 0px;
          width: 0px;
        })"));
      auto s = new ScrollBar(w);
      w->setVerticalScrollBar(s);
      auto l = new QLabel("Test Label", w);
      l->setStyleSheet("background-color: aqua;");
      l->setFixedSize(10000, 10000);
      w->setWidget(l);
      w->resize(400, 300);
      w->show();
      return application->exec();
    }
    

    Output and window displayed on Windows 10:

    scrollbar.png

    I would expect the slider_rect in the paintEvent to be 13x60 pixels, and at position (0, 0), but the output shows the rectangle is 13x9 pixels and is at position (0, 17), which is the default geometry without a custom style applied.

    The issue this creates is if I click on the top of the scroll bar (let's say at (0, 0)) and drag down, I have to move the mouse 17 pixels before the scroll bar handle starts to move down.

    I'm not sure if this is a bug or if I'm abusing the QScrollBar in some way, but the goal is to have a button-less scroll bar that moves seamlessly with the mouse.


  • Lifetime Qt Champion

    Ok, I understand your issue better. The key information missing was that you are using a style sheet. So indeed, there's a dedicated internal style that is used when you set a style sheet.

    Depending on the exact outcome you want, you should do all in your custom style.


Log in to reply