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. programmatically moving the scrollbar in a scrollarea
Qt 6.11 is out! See what's new in the release blog

programmatically moving the scrollbar in a scrollarea

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 6 Posters 1.2k Views 3 Watching
  • 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.
  • jeremy_kJ Offline
    jeremy_kJ Offline
    jeremy_k
    wrote on last edited by
    #2

    Have you tried QScrollArea::ensureVisible() instead of manipulating the scrollbar?

    Asking a question about code? http://eel.is/iso-c++/testcase/

    1 Reply Last reply
    0
    • J janK

      I have a QScrollArea with in it a Table.
      The table is way too large to be fully shown in the (relatively small) window.
      From time to time I want to highlight a table item, selection and altering the fonts used there iis no problem at all, but due to the
      limited size of the enclosing window, the high lighted element is not automatically in the viewable part of the scrollarea

      In order to make the selected element visible I want to programatically set the scrollbar such that the area with the
      selected element is visible. Having set the minimum and maximum values for the scrollbar to 0 ..100,
      computing is no problem.
      I compute a value to set the scrollbar and issue a command

      .... -> theWidget -> verticalScrollBar () -> setValue ( (int)(yVal * 100));
      where yVal is fraction between 0 and 1 resulting from my computation
      However, the scrollbar does not move and inquiring the scrollbar value
      theWidget -> verticalScrollBar () -> value ());
      always gives the value 0

      Apparently I am doing something wrong
      Any help would be appreciated

      best
      jan

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @janK said in prorammatically moving the scrollbar in a scrollarea:

      theWidget -> verticalScrollBar () -> setValue ( (int)(yVal * 100));

      Is the range of your scrollbar really only from 0 to 100 ? I doubt this.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • J Offline
        J Offline
        janK
        wrote on last edited by
        #4

        Yes, I have tried ensureVisibility that did not work
        Ans I continuous verify that the range of (int)(yVal * 100) is between 0 .. 100,
        yVal is a value between 0.1

        Christian EhrlicherC jeremy_kJ 2 Replies Last reply
        0
        • J janK

          Yes, I have tried ensureVisibility that did not work
          Ans I continuous verify that the range of (int)(yVal * 100) is between 0 .. 100,
          yVal is a value between 0.1

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @janK said in prorammatically moving the scrollbar in a scrollarea:

          Ans I continuous verify that the range of (int)(yVal * 100) is between 0 .. 100,

          This was not my question. Why do you think the range of your scrollbar is between 0 and 100? Please read my link.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • J Offline
            J Offline
            janK
            wrote on last edited by
            #6

            OK
            The text in the original question states that I did set the min en max for th scrollbar to 0 and 100

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Did you actually check if QScrollArea did not modify the scrollbar? QScrollArea is the owner of the QScrollBar(s) so it also changes min/max to match it's interal logic.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • J janK

                Yes, I have tried ensureVisibility that did not work
                Ans I continuous verify that the range of (int)(yVal * 100) is between 0 .. 100,
                yVal is a value between 0.1

                jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote on last edited by
                #8

                @janK said in programmatically moving the scrollbar in a scrollarea:

                Yes, I have tried ensureVisibility that did not work
                Ans I continuous verify that the range of (int)(yVal * 100) is between 0 .. 100,
                yVal is a value between 0.1

                What does "did not work" mean? And where does this value 100 come from? Is the table height 100 pixels + the height of the QScrollAreal?

                Asking a question about code? http://eel.is/iso-c++/testcase/

                1 Reply Last reply
                0
                • J janK

                  I have a QScrollArea with in it a Table.
                  The table is way too large to be fully shown in the (relatively small) window.
                  From time to time I want to highlight a table item, selection and altering the fonts used there iis no problem at all, but due to the
                  limited size of the enclosing window, the high lighted element is not automatically in the viewable part of the scrollarea

                  In order to make the selected element visible I want to programatically set the scrollbar such that the area with the
                  selected element is visible. Having set the minimum and maximum values for the scrollbar to 0 ..100,
                  computing is no problem.
                  I compute a value to set the scrollbar and issue a command

                  .... -> theWidget -> verticalScrollBar () -> setValue ( (int)(yVal * 100));
                  where yVal is fraction between 0 and 1 resulting from my computation
                  However, the scrollbar does not move and inquiring the scrollbar value
                  theWidget -> verticalScrollBar () -> value ());
                  always gives the value 0

                  Apparently I am doing something wrong
                  Any help would be appreciated

                  best
                  jan

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Hi,

                  @janK said in programmatically moving the scrollbar in a scrollarea:

                  I have a QScrollArea with in it a Table.

                  Something is not fully clear here. QTableView already is a scroll area (albeit not a QScrollArea) so from your description it seems you have a QTableView within a QScrollArea and you try to move your QTableView content but are doing that by modifying your QScrollArea.

                  Can you show your exact setup ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SimonSchroeder
                    wrote on last edited by
                    #10

                    Also, if you restrict the scrollbar to the range 0...100, your new position might come out as the same value within that range and thus not move the scrollbar one bit.

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      C_H_E_N
                      wrote on last edited by C_H_E_N
                      #11

                      As far as I understand, manually setting minimum/maximum may cause unexpected result. See:
                      https://github.com/qt/qtbase/blob/dev/src/widgets/widgets/qscrollarea.cpp#L191
                      The min/max values are most likely measured in pixels. Otherwise, I don't understand why QScrollArea does that by default

                      vbar->setRange(0, v.height() - p.height());
                      

                      So if you limit the maximum to 100, you are probably going to only see very limited part of your table, which may result in looking like "nothing happened" because <100 pixels is perhaps too unnoticeable. You may try unsetting min/max value and just try something like

                      QScrollBar* scrollBar = <whatever_your_scroll_bar_is>;
                      scrollBar->setValue(int(scrollBar->maximum() * 0.1f));
                      

                      And see if you can notice something.

                      And another recommendation is that a minimal reproducing sample can really help here.

                      1 Reply Last reply
                      1

                      • Login

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