Search code examples
qtqscrollarea

How to change the setPage of scrollBar in Qt?


I create a list of 64 items using scrollAreaWidgetContents on Qt the list is like this: enter image description here

I want when I press the scroll down arrow I got 4 sentences not 5. So for one click down or up of the arrow I skip one sentence.

The code of scrollArea's stylesheet:

QFrame{
    border:solid;
}

QScrollBar:vertical {
    width: 15px;
    margin: 30px 0 30px 0;
    border-style: solid;
}

QScrollBar::handle:vertical {
    min-height: 30px;
    border: solid ;    
}

QScrollBar::add-line:vertical {
    subcontrol-position: bottom;
    subcontrol-origin: margin;
    border: solid ;
    height: 30px;
}

QScrollBar::sub-line:vertical {
    subcontrol-position: up;
    subcontrol-origin: margin;
    border: solid ;
    height: 30px;
}

QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {
    background: none;
}

QScrollBar::up-arrow:vertical
{
    image: url(icons/Freccia_Su.png);
}

QScrollBar::down-arrow:vertical
{
    image: url(icons/Freccia_Giu.png);
}

I found the setPage in the documentation of QScrollBar but I don't understand how to add it? Can someone help me to fix this issue?


Solution

  • I think you may be expecting too much from QScrollArea. I can't tell from your question how you have set the contents of the QScrollArea, but if you have set a QTextEdit as its widget, then the QScrollArea doesn't know anything about the size of the text, for example. You may be better off looking at QListWidget:

    //assuming a ui with a QListWidget called listWidget
    
        ui.setupUi(this);
        QStringList items;
        for (int i = 0; i < 100; i++)
          items << "item " + QString::number(i);
        ui.listWidget->insertItems(0, items);
    

    This will automatically scroll by one item per click of the scroll bars.

    You can create more complicated list entries using QListWidgetItem (see the docs for QListWidget).

    The items can contain widgets themselves, so you can have a custom widget with radiobuttons etc, and use QListWidget::setItemWidget() to set it on each item. The scrolling will take the widget size into account.