Results 1 to 10 of 10

Thread: How to tell when QGraphicsView has changed?

  1. #1
    Join Date
    Oct 2009
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default

    Hello, I'm trying to automatically advance() the scene whenever the QGraphicsView changes position. However, I don't know how to tell when the QGraphicsView changes. The scrollHandDrag is enabled, so the user can change the view by scrolling the scroll bars or by dragging the mouse on the scene. I found out that whenever the mouse is being dragged, the "auto mouser dragger" automatically intercepts the mouseMoveEvent so I can't use that when the thing is moved by hand (I had hopes maybe I could disable the scroll bars later so I'd only have to deal with that).

    Anyone know?

    My long unadulterated, uber-unhelpfully-commented code
    is in the next post because this thing has size limits. I'm putting it here just in case it might be helpful to someone.


    I tried subclassing the QGraphicsView class in mapView, but apparently the QGraphicsView constructor passes in a scene and it complained when I tried to pass a scene inside the mapView constructor(which defaults to the QGraphicsView constructor) about "private member" or some such, so I abandoned that approach, although I have a feeling I'll have to do that eventually.
    Attached Files Attached Files
    Last edited by wysota; 30th November 2009 at 08:17.

  2. The following user says thank you to swbluto for this useful post:

    scascio (30th November 2009)

  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to tell when QGraphicsView has changed?

    I think the easiest way is to connect to the scrollbars' valueChanged() signal.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #3
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to tell when QGraphicsView has changed?

    I had same needs and have no scroll bar.

    The solution I have found is to listen to the mouse release event after a drag.
    Since I manges distincts tools, I know when it is a drag event or not because the tool is activated previously. Then I can get the viewed rect of the scene by asking it to the map.

    But this solution does not catch resize event of the view so I need to treat this event too.

    If you set scrollbars, then you need to listen to scrollbars changes too.

    Maybe this will help you but I am interested if you find a better way to do it, since I find painful to catch 3 events for same operation.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to tell when QGraphicsView has changed?

    Quote Originally Posted by scascio View Post
    I had same needs and have no scroll bar.
    Sure you have, it's just invisible.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to tell when QGraphicsView has changed?

    Do you mean that valueChanged signal is emitted in all cases, even with Qt::ScrollBarAlwaysOff?
    Great! I run and try it!

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to tell when QGraphicsView has changed?

    Quote Originally Posted by scascio View Post
    Do you mean that valueChanged signal is emitted in all cases, even with Qt::ScrollBarAlwaysOff?
    The value changes, doesn't it? The fact if you see it on the screen or not is irrelevant.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    scascio (30th November 2009)

  9. #7
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to tell when QGraphicsView has changed?

    Yes. It works very well. For resize events just listen to rangeChanged signal.

    So with only 4 lines I listen to view changes :
    Qt Code:
    1. // somewhere in a derived QGraphicsView method...
    2. connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(changeViewedRect()));
    3. connect(verticalScrollBar() , SIGNAL(valueChanged(int)), this, SLOT(changeViewedRect()));
    4. connect(horizontalScrollBar(), SIGNAL(rangeChanged(int, int)), this, SLOT(changeViewedRect()));
    5. connect(verticalScrollBar() , SIGNAL(rangeChanged(int, int)), this, SLOT(changeViewedRect()));
    To copy to clipboard, switch view to plain text mode 

    How great! Thanks

  10. #8
    Join Date
    Oct 2009
    Posts
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to tell when QGraphicsView has changed?

    Thanks scascio! I reimplemented it in the form below to call the "advance()" function for the scene so that the graphics items can be updated as needed.

    Qt Code:
    1. connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), scene, SLOT(advance()));
    2. connect(verticalScrollBar() , SIGNAL(valueChanged(int)), scene, SLOT(advance()));
    3. connect(horizontalScrollBar(), SIGNAL(rangeChanged(int, int)), scene, SLOT(advance()));
    4. connect(verticalScrollBar() , SIGNAL(rangeChanged(int, int)), scene, SLOT(advance()));
    To copy to clipboard, switch view to plain text mode 

    Problem is, advance() gets called 12 times each time the arrow is clicked on the scroll bar. It gets called hundreds of times when the mouse is used to drag the view. This ends up producing very slow program response.

    Ok, so I had an idea. I only care about the view if the actual view has changed. I think I'll check the view's origin mapped to the scene to see if it's changed - if it's actually changed, then *really* advance.
    Last edited by wysota; 1st December 2009 at 10:36. Reason: Changed [qtclass] to [code]

  11. #9
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to tell when QGraphicsView has changed?

    Quote Originally Posted by swbluto View Post
    ...
    Problem is, advance() gets called 12 times each time the arrow is clicked on the scroll bar. It gets called hundreds of times when the mouse is used to drag the view. ...
    How sad. I dont use scrolbars so I didnt notice it, sorry. And I didnt integrated it in my application yet. For dragging event, I though it is normal to to be called for each mouseMove, isn't it?

    I think you're right to filter change states of the view in the slot, then call advance. But you will need to check the bottom left corner of the view, won't you? Why only the origin?

    And another question that may seems stupid since I dont know your design and since I dont use advance method : why are you connecting this slot to changes of the view? advance is usually used for animation, eg to change state and position of items? I dont understand why items need to advance when you scroll the view of the scene.

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to tell when QGraphicsView has changed?

    Quote Originally Posted by swbluto View Post
    Problem is, advance() gets called 12 times each time the arrow is clicked on the scroll bar. It gets called hundreds of times when the mouse is used to drag the view. This ends up producing very slow program response.
    I think you should be calling advance() using a timer and not when the view on the scene changes. You can't expect to control how the user uses your program but you can expect to control what happens in the application itself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 29th September 2009, 02:28
  2. Replies: 1
    Last Post: 16th September 2009, 11:23
  3. QGraphicsView performance
    By Halabund in forum Newbie
    Replies: 3
    Last Post: 17th April 2009, 10:12
  4. Replies: 0
    Last Post: 5th March 2009, 06:54
  5. QGraphicsView and embeded widgets
    By bunjee in forum Qt Programming
    Replies: 10
    Last Post: 12th October 2008, 07:43

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.