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. QScrollBar subControlRect ignores orientation Qt5.9.1
Forum Updated to NodeBB v4.3 + New Features

QScrollBar subControlRect ignores orientation Qt5.9.1

Scheduled Pinned Locked Moved Solved General and Desktop
qscrollbarsubcontrolrectqstylesub-controls
7 Posts 2 Posters 2.4k 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.
  • O Offline
    O Offline
    Oleg21
    wrote on last edited by
    #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)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Can you post a minimal compilable sample that shows that ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      O 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Can you post a minimal compilable sample that shows that ?

        O Offline
        O Offline
        Oleg21
        wrote on last edited by
        #3

        @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()
        {
        
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You are running that on Windows, right ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          O 2 Replies Last reply
          0
          • SGaistS SGaist

            You are running that on Windows, right ?

            O Offline
            O Offline
            Oleg21
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • SGaistS SGaist

              You are running that on Windows, right ?

              O Offline
              O Offline
              Oleg21
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                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