Results 1 to 3 of 3

Thread: setting value to verticalScrollBar has no effect

  1. #1
    Join Date
    Sep 2017
    Posts
    2
    Qt products
    Platforms
    Unix/X11

    Default setting value to verticalScrollBar has no effect

    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:
    Qt Code:
    1. self.verticalScrollBar().setValue(self.verticalScrollBar().minimum())
    To copy to clipboard, switch view to plain text mode 
    but this seems to have no effect!

    Curiously, when I do it like that:
    Qt Code:
    1. # in initializer:
    2. QTimer.singleShot(0, self.test)
    3.  
    4. def test(self):
    5. self.verticalScrollBar().setValue(self.verticalScrollBar().minimum())
    To copy to clipboard, switch view to plain text mode 
    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!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: setting value to verticalScrollBar has no effect

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Sep 2017
    Posts
    2
    Qt products
    Platforms
    Unix/X11

    Default Re: setting value to verticalScrollBar has no effect

    Great!
    That worked out perfectly, thanks!

Similar Threads

  1. Replies: 1
    Last Post: 26th April 2016, 14:46
  2. Replies: 5
    Last Post: 16th September 2014, 22:37
  3. How to use verticalScrollBar? [SOLVED]
    By micamica in forum Newbie
    Replies: 4
    Last Post: 19th July 2011, 06:33
  4. Replies: 5
    Last Post: 5th September 2009, 06:34
  5. verticalScrollBar on the left
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 10th August 2006, 15:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.