How to get width and height of QScrollbars arrow widgets



  • 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



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



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



  • 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



  • 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 ;)



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


Log in to reply
 

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