Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] How to set a scrollbar to scroll down automatically?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 20.8k 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
    mkcerusky
    wrote on 18 Oct 2013, 21:54 last edited by
    #1

    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

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 19 Oct 2013, 05:05 last edited by
      #2

      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.

      157

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mkcerusky
        wrote on 19 Oct 2013, 06:45 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 19 Oct 2013, 07:13 last edited by
          #4

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

          157

          1 Reply Last reply
          0
          • P Offline
            P Offline
            p3c0
            Moderators
            wrote on 19 Oct 2013, 10:50 last edited by
            #5

            Hi,

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

            157

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mkcerusky
              wrote on 19 Oct 2013, 16:19 last edited by
              #6

              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.

              1 Reply Last reply
              0
              • J JoeCFD referenced this topic on 14 Apr 2023, 17:53

              6/6

              19 Oct 2013, 16:19

              • Login

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