Results 1 to 7 of 7

Thread: Scrolling several QGraphicsViews with another widget

  1. #1
    Join Date
    Jul 2011
    Location
    Paris
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Scrolling several QGraphicsViews with another widget

    Hello,

    I am trying to gain control of the scrolling of my QGraphicsViews through another widget which in this case is a QGraphicsObject.

    What I have is this GraphicsObject item that can be moved only horizontally. When my GraphicsViews are zoomed-in, the item has space to move. So I am trying to control the scrolling of the views with this item, so when I move the item to one side or the other, it will be like moving the views's scroll bars horizontally to one side or the other.

    I have tried doing it like this:

    - First I get the value of how much the item moved in either direction and send it through a signal, I connect that to a slot in my QGraphicsView class that has this :

    Qt Code:
    1. void myView::scrollViewSlot(int dx)
    2. {
    3. int position = horizontalScrollBar()->value();
    4. position += dx;
    5. horizontalScrollBar()->setValue(position);
    6. }
    To copy to clipboard, switch view to plain text mode 

    This is so that wherever the value of the horizontal scrollbar was, in this case I add to that value how much I would like to scroll and set the value of the horizontalScrollbar of the view, so it should move (scroll) the views. I understand that the value to add to the current horizontalScrollBar value must be well calculated by making tests no matter what the value is the views never scroll. I have made sure that the singal/slots are well connected and when I move the item, the slot gets reached with a value but the views don't move.

    What am I missing to make this work properly?
    Thanks.

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scrolling several QGraphicsViews with another widget

    Does the view move at all when you try to scroll it by some large number?
    Maybe your dx is so small you can't see the movement?

  3. #3
    Join Date
    Jul 2011
    Location
    Paris
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scrolling several QGraphicsViews with another widget

    Thanks! I was trying that before until I found in the documentation of scrollContentsBy:

    Calling this function in order to scroll programmatically is an error, use the scroll bars instead (e.g. by calling QScrollBar::setValue() directly).
    So just made a good signal/slot system and called the QGraphicsView::horizontalScrollBar->setValue(value) with value being how much I moved the item and it works fine.

  4. #4
    Join Date
    Jul 2011
    Location
    Paris
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scrolling several QGraphicsViews with another widget

    Made some tests and although it scrolls the view contents fine, the value I am giving seems not to be precise. Does anyone have an idea of how I can calculate the amount of movement in the scene to the amount of what it would be if the scrollbar slider was moved?

  5. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scrolling several QGraphicsViews with another widget

    Set your slider (the one that controlls the scrolling) min value to 0 and max value to 100 then:
    Qt Code:
    1. viewScrollBar->setValue( viewScrollBar->maximum() * ( controlSlider->value() / 100.0f ) );
    To copy to clipboard, switch view to plain text mode 
    should do the trick.

  6. #6
    Join Date
    Jul 2011
    Location
    Paris
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scrolling several QGraphicsViews with another widget

    Thanks, that does give me some ideas however I am not controlling the scrolling with a Scrollbar or slider, it is a Rectangular graphicsItem. When I zoom-in, the rectangular item shrinks exactly like the scrollbars of the view. So what I am doing is just hiding the scroll bars of the QGV and control the scrolling by moving the rectangular item horizontally. So I am having trouble in "mapping" when I move the rectItem, how much I moved this item should equivalently scroll the views by that amount.

    I tried getting the values in scene coordinates, since my scene is the same size of my view's viewport,.. and try to get an equivalent number to set viewsScrollBar->setValue(value + dx). Being value the scrollBar's current value and dx the amount I need to move it.

    Any idea how I can achieve this?? Thanks for the help!

  7. #7
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scrolling several QGraphicsViews with another widget

    I'm not sure if I understood what you're trying to do...
    You have graphics item in the scene that controls graphics view scroll bars?
    What I wrote previously still applies, you just get values from different place.

    Assuming that the item you use to control has min and max bound and you can know them then it would be something like that:
    Qt Code:
    1. double percent = controlItemCurrentX / controlItemMaxX; // one of the values has to be double/float otherwise it won't work
    2. viewScrollBar->setValue( viewScrollBar->maximum() * percent );
    To copy to clipboard, switch view to plain text mode 
    the idea is to get control item percentage position alongside its axis, then you know where along the scene axis viewport should be.

    To make it more accurate you may need to factor in viewport size into the calculation.

  8. The following user says thank you to Spitfire for this useful post:

    Sergex (25th November 2011)

Similar Threads

  1. Replies: 0
    Last Post: 10th June 2010, 23:38
  2. Scrolling 2 QGraphicsViews with 1 scrollbar
    By dentist in forum Newbie
    Replies: 2
    Last Post: 14th April 2010, 02:12
  3. Replies: 0
    Last Post: 26th November 2009, 15:49
  4. QScrollbar show scratching on scrolling on any widget
    By santosh.kumar in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2008, 05:58
  5. Scrolling in QTextEdit Widget
    By ajb_advance in forum Newbie
    Replies: 2
    Last Post: 25th September 2007, 11:34

Tags for this Thread

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.