QScrollBar subControlRect ignores orientation Qt5.9.1



  • Hi everyone!

    I recently started using Qt. Everything was fine, but once I worked with QScrollBar.
    I needed to find out the coordinates of the Sub Controls.
    For example, to find out the QStyle :: SC_ScrollBarSubLine coordinates and dimensions, I used the following code:

    QStyleOptionSlider *opt = new QStyleOptionSlider;
    opt->initFrom(scrollBar1);
     
    QRect r = scrollBar1->style()->subControlRect(QStyle::CC_ScrollBar, opt, QStyle::SC_ScrollBarSubLine);
    

    This code works great with a horizontal scrollbar.
    Because with a vertical scrollbar, it is not possible to get coordinates and dimensions QStyle :: SC_ScrollBarSubLine.
    More precisely, sizes and coordinates can be obtained,
    but they will be equal to the size and coordinates of the QStyle :: SC_ScrollBarSubLine horizontal scroll bar.

    Take a look at the pictures. They graphically show what I'm trying to find out.

    Maybe some of you already encountered similar problems. Tell me how to solve them?

    ![alt text Picture1](image url https://photos.app.goo.gl/BvIqozvxjTbR8Sw83)
    ![alt text Picture2](image url https://photos.app.goo.gl/wSQSRBpPUn9sWi0J2)


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you post a minimal compilable sample that shows that ?



  • @SGaist

    So of course, here is the program code:
    scrollbar_test.h

    #ifndef SCROLLBAR_TEST_H
    #define SCROLLBAR_TEST_H
    
    #include <QMainWindow>
    
    class scrollbar_test : public QMainWindow
    {
        Q_OBJECT
    
    public:
        scrollbar_test(QWidget *parent = 0);
        ~scrollbar_test();
    };
    
    #endif // SCROLLBAR_TEST_H
    

    scrollbar_test.cpp

    #include "scrollbar_test.h"
    #include <QtWidgets>
    #include <QDebug>
    #include <QStyle>
    #include <QStyleOptionSlider>
    
    scrollbar_test::scrollbar_test(QWidget *parent) : QMainWindow(parent)
    {
        QScrollBar *scrollBar1 = new QScrollBar;
        scrollBar1->setParent(this);
        scrollBar1->setGeometry(QRect(10, 10, 100, 200));
        scrollBar1->setMinimum(0);
        scrollBar1->setMaximum(5);
        scrollBar1->setPageStep(5);
        /* When the horizontal position of the scroll bar works correctly */
        //scrollBar1->setOrientation(Qt::Horizontal);
    
        /* When the vertical position of the scroll bar does not work correctly */
        scrollBar1->setOrientation(Qt::Vertical);
    
        QStyleOptionSlider *opt = new QStyleOptionSlider;
        opt->initFrom(scrollBar1);
    
        QRect r = scrollBar1->style()->subControlRect(QStyle::CC_ScrollBar, opt, QStyle::SC_ScrollBarSubLine);
    
        /* In both cases, the variable r contains the same value, but should not be so */
        qDebug() << r;
    }
    
    scrollbar_test::~scrollbar_test()
    {
    
    }
    

  • Lifetime Qt Champion

    You are running that on Windows, right ?



  • @SGaist
    Yes, I work in windows 7. Can this affect the result?
    I tried to run this program under Windows XP. The result is the same.



  • @SGaist
    Hi SGaist. I was able to solve the problem.
    The reason is not in QScrollBar.
    The reason was in QStyleOptionSlider.
    In the code after these lines:

    QStyleOptionSlider *opt = new QStyleOptionSlider;
    opt->initFrom(scrollBar1);
    

    I inserted the following code:

    opt->orientation = Qt::Vertical;
    

    And everything works.
    Probably the reason is the incorrect transfer of parameters to the opt variable.

    Thanks for the help, the issue is resolved.


  • Lifetime Qt Champion

    Great !

    Thanks for sharing your findings !

    One last thing, you are leaking your opt object. You should rather build it on the stack and pass its address to subControlRect.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.