[SOLVED] How to set a scrollbar to scroll down automatically?



  • I have a QScrollArea containing some widgets displaied in a vertical layout (lalgLayout), I would like the vertical scrolbar to scroll automatically dow in order to show always the bottom of the widget. I tried using iether setDown(true) or setSliderPoisition(whatever) but I noticed that non of the two functions seem to affect my scrollbar at all. Here is an example of the code I am using:

    @
    QScrollArea * exprScroll = new QScrollArea();
    exprScroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    exprScroll->setWidget(lalgExpression3);
    lalgLayout->addWidget(exprScroll);
    exprScroll->verticalScrollBar()->setSliderDown(true);
    @

    or I tried this, which doesn't seem to affect at all the position of the slider which results to be always set to 0:
    @
    QScrollArea * exprScroll = new QScrollArea();
    exprScroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    exprScroll->setWidget(lalgExpression3);
    lalgLayout->addWidget(exprScroll);
    exprScroll->verticalScrollBar()->setSliderPosition(10);
    @

    Finally I tried this, but obtaining the same result, that is the slider is always at the top, never at the bottom:

    @
    if (lalgLayout->isEmpty()) { // inserisce con scrollbarr!
    QScrollArea * exprScroll = new QScrollArea();
    QScrollBar * barra = new QScrollBar(Qt::Vertical);
    barra->setSliderDown(true);
    exprScroll->setVerticalScrollBar(barra);
    exprScroll->setWidget(lalgExpression3);
    lalgLayout->addWidget(exprScroll);
    barra->setSliderPosition(10);
    @

    What am I missing? What should I do in order to have my commands actually affect the position of the slider?

    Thank in advance, MkC


  • Moderators

    Hi,

    You can use the "ensureWidgetVisible":http://qt-project.org/doc/qt-5.0/qtwidgets/qscrollarea.html#ensureWidgetVisible function to make sure the added widget gets displayed, so the scrollbar will scroll automatically to that position. You just need to pass the added widget point.



  • I tried with "ensureWidgetVisible":http://qt-project.org/doc/qt-5.0/qtwidgets/qscrollarea.html#ensureWidgetVisible and also with "ensureVisible":http://qt-project.org/doc/qt-5.0/qtwidgets/qscrollarea.html#ensureVisible but none of them seems to affect my scroll area at all....I vae the impression that any command I try is not passed to the scroll area and I don't understand why.


  • Moderators

    Or,
    @ui->scrollArea->verticalScrollBar()->setValue(
    ui->scrollArea->verticalScrollBar()->maximum());
    @


  • Moderators

    Hi,

    Did you call the above code everytime you added the child widget to QScrollArea ?



  • Yep, something like that, in fact, in the end, it was enough to split this line:
    @ QScrollArea * exprScroll = new QScrollArea();@
    into
    @ QScrollArea * exprScroll;@
    and
    @ exprScroll = new QScrollArea();@

    The first one had to be put at the beginning of the code, so that expreScroll is declared as a global variable. Then the second one has to be put in another function that builds exprScroll, finally the rest of the above code can be in the function that adds widgets to the scroll area.

    I have a similar situation were I still have the same problem, but I guess it is a matter of nested functions to be carefully managed :)

    Thanks for your help.


Log in to reply
 

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