programmatically moving the scrollbar in a scrollarea
-
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 scrollareaIn 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 0Apparently I am doing something wrong
Any help would be appreciatedbest
jan -
Have you tried QScrollArea::ensureVisible() instead of manipulating the scrollbar?
-
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 scrollareaIn 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 0Apparently I am doing something wrong
Any help would be appreciatedbest
jan@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.
-
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@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.
-
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.
-
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@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.1What does "did not work" mean? And where does this value 100 come from? Is the table height 100 pixels + the height of the QScrollAreal?
-
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 scrollareaIn 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 0Apparently I am doing something wrong
Any help would be appreciatedbest
janHi,
@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 ?
-
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.
-
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 defaultvbar->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.