PDA

View Full Version : setting value to verticalScrollBar has no effect



Absurd
8th September 2017, 11:35
Hey there.
I am new to this forum and to Qt Programming in general.
Making my first step through PyQt (yep, I am Python enthusiastic, I hope there's room for PyQt here as well).

I am trying to write a small qt app, but having some difficulty to set up a scroll bar the way I want (all the way to the top).
I have sub-classed QGraphicsView, to which I added a Scene with some items on it.
The items do not all fit into the QGraphicsView so a vertical scroll bar appears.
But the scroll bar is not scrolled all the way up, but rather it is scrolled exactly in the middle.
So, I tried doing that from (Python's) initializer:

self.verticalScrollBar().setValue(self.verticalScr ollBar().minimum())
but this seems to have no effect!

Curiously, when I do it like that:

# in initializer:
QTimer.singleShot(0, self.test)

def test(self):
self.verticalScrollBar().setValue(self.verticalScr ollBar().minimum())

it does seem to work, and the vertical scroll bar is set to the top!

I understand that the difference between running something from the initializer and with QTimer.singleShot(0, self.test) is that self.test will be called after the event handler is running.
So the question is: why doesn't that working when I set the scroll bar value before the event handler is running, but does work when setting the value after the event handler is running?

Any help would be greatly appreciated.
Thanks!

d_stranz
8th September 2017, 19:18
So the question is: why doesn't that working when I set the scroll bar value before the event handler is running, but does work when setting the value after the event handler is running?

It has nothing to do with the event handler in actuality. The behavior you observe is because in the initializer the view is created but not yet sized and visible, so calculations of the scrollbar position have no effect. By the time the widget is made visible (via the showEvent) positioning the scrollbar thumb works. That your single shot time accomplished the same thing is just a result of the widget being shown (which occurred prior to your timeout being serviced). You could remove the timer completely, add an override for the showEvent with that scrollbar code in it, and you'd see the same.

Absurd
9th September 2017, 11:17
Great!
That worked out perfectly, thanks!