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 get width and height of QScrollbars arrow widgets
QtWS25 Last Chance

How to get width and height of QScrollbars arrow widgets

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 11.1k Views
  • 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.
  • M Offline
    M Offline
    markus.liebe
    wrote on 23 Jul 2010, 06:47 last edited by
    #1

    Hi there,

    is it possible to get the height (for vertical scrollbars) and the width (for horizontal scrollbars) of the arrow widgets of a QScrollbar?

    @+-+
    |^| arrow widget top
    +-+
    | |
    | | scrollbar
    | |
    +-+
    |v| arrow widget bottom
    +-+
    @

    In the example above I am interested in the height of "arrow widget bottom" and "arrow widget top".

    In the docs for QScrollBar I did not find anything like that.

    Regards,
    Markus

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kibsoft
      wrote on 23 Jul 2010, 10:26 last edited by
      #2

      I think it's impossible at least in standart way. See to the source code of QScrollBar, maybe it helps you.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        ogoffart
        wrote on 23 Jul 2010, 17:19 last edited by
        #3

        using the style.

        From memory:

        QStyleOptionSlider newScrollbar;
        newScrollbar.initFrom(scrollbar);
        // fill other stuff in newScrollbar, see source code of QScrollBar::initStyleOption
        h = scrollbar->style()->subControlRect(CC_ScrollBar, &option,SC_ScrollBarSubPage, scrollbar).height();

        Good luck.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          markus.liebe
          wrote on 27 Jul 2010, 11:50 last edited by
          #4

          Hi again.

          I tried the approach using the style. But I had no luck. In the example below I try to get the height of the subcontrol (arrow widget) but I always get the height of the complete scrollbar:

          (by the way: it seems as if the forum software gets confused by the parantheses even though I used the "code" icon to insert the code)

          @#include <QtGui>
          #include <QtGui>

          class MainWindow : public QMainWindow
          {
          Q_OBJECT

          public:
          MainWindow(QWidget *parent = 0)
          : QMainWindow(parent),
          m_textEdit(new QTextEdit())
          {
          setCentralWidget(new QWidget(this));
          centralWidget()->setLayout(new QVBoxLayout());

              QPushButton *pb = new QPushButton("print info");
              connect(pb,SIGNAL(clicked()),this,SLOT(printInfo()));
          
              centralWidget()->layout()->addWidget(m_textEdit);
              centralWidget()->layout()->addWidget(pb);
          
              for(int counter = 0; counter < 200; counter++){
                  m_textEdit->append(QString("Line %1").arg(counter));
              }
          
              resize(320,240);
          }
          
          ~MainWindow(){
          
          }
          

          public slots:
          void printInfo()
          {
          QScrollBar *scrollbar = m_textEdit->verticalScrollBar();
          QStyleOptionSlider *option = new QStyleOptionSlider;
          option->initFrom(scrollbar);

              // Print height and width for SC_ScrollBarAddLine,SC_ScrollBarSubLine,SC_ScrollBarAddPage and SC_ScrollBarSubPage
          
              qDebug() << "SC_ScrollBarAddLine: ";
              QRect rect = style()->subControlRect(QStyle::CC_ScrollBar,option,QStyle::SC_ScrollBarAddLine,scrollbar);
              qDebug() << "Height: " << rect.height();
              qDebug() << "Width: " << rect.width();
              qDebug() << "--------------------------------------------------------------------------------";
          
              qDebug() << "SC_ScrollBarSubLine: ";
              rect = style()->subControlRect(QStyle::CC_ScrollBar,option,QStyle::SC_ScrollBarSubLine,scrollbar);
              qDebug() << "Height: " << rect.height();
              qDebug() << "Width: " << rect.width();
              qDebug() << "--------------------------------------------------------------------------------";
          
              qDebug() << "SC_ScrollBarAddPage: ";
              rect = style()->subControlRect(QStyle::CC_ScrollBar,option,QStyle::SC_ScrollBarAddPage,scrollbar);
              qDebug() << "Height: " << rect.height();
              qDebug() << "Width: " << rect.width();
              qDebug() << "--------------------------------------------------------------------------------";
          
              qDebug() << "SC_ScrollBarSubPage: ";
              rect = style()->subControlRect(QStyle::CC_ScrollBar,option,QStyle::SC_ScrollBarSubPage,scrollbar);
              qDebug() << "Height: " << rect.height();
              qDebug() << "Width: " << rect.width();
              qDebug() << "--------------------------------------------------------------------------------";
          }
          

          private:
          QTextEdit* m_textEdit;
          };

          int main(int argc, char *argv[])
          {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
          }

          #include "main.moc"
          @

          In my case the output of the little program (after pressing the button) is:

          @SC_ScrollBarAddLine:
          Height: 185
          Width: 16

          SC_ScrollBarSubLine:
          Height: 185
          Width: 0

          SC_ScrollBarAddPage:
          Height: 185
          Width: 0

          SC_ScrollBarSubPage:
          Height: 185
          Width: 0

          @

          any hints?
          Regards, Markus

          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on 31 Jul 2010, 10:45 last edited by
            #5

            One crazy workaround you can use is to set a stylesheet to your scroll bar. You can set some default height to "QScrollBar:horizontal" and also the width to "QScrollBar::add-line:horizontal" (or vertical) and to "QScrollBar::sub-line:horizontal" (or vertical). Now you can anytime retrieve this stylesheet as a QString using qscrollbar->stylesheet(). You can now also set some custom value by reconstructing this string and using qscrollbar->setStyleSheet() ...

            Maybe not the most elagant solution, but I tried and it'll work ;)

            1 Reply Last reply
            0
            • ? This user is from outside of this forum
              ? This user is from outside of this forum
              Guest
              wrote on 31 Jul 2010, 11:01 last edited by
              #6

              The default size should be documented, anything custom (depending on your use case), my approach should work

              1 Reply Last reply
              0
              • M mpergand referenced this topic on 21 Jul 2023, 13:34
              • B buhtz referenced this topic on 21 Jul 2023, 13:43

              4/6

              27 Jul 2010, 11:50

              • Login

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