Results 1 to 8 of 8

Thread: Non-rectangle rubber band implementation [Solved]

  1. #1
    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 Non-rectangle rubber band implementation [Solved]

    I'm trying to implement a "lasso" type rubber band to allow users to select things from a QGraphicsView. A rectangle won't work, because the items don't always fall into a neat rectangular area free from other items.

    I have tried subclassing QRubberBand and adding a new Shape (Lasso) with custom paintEvent code and a QPainterPath to accumulate the mouse event points, but that is not working well. I can draw the "lasso", but there are also artifacts (one or more lines drawn from the window top left to the starting mouse point) which somehow seem to get inserted into the path.

    I've checked the mouse positions (as best one can do that in mouse event handlers) and they look OK, and I don't see any (0,0) points being inserted.

    I have looked through many of the previous posts here on rubber bands, and none of them seem to apply; they all deal with customizing the stock QRubberBand, not implementing a new one from scratch.

    So, can anyone point me to code which provides an example of a QPainterPath or polygon / polyline - based rubber band? Any drawing packages for example?
    Last edited by d_stranz; 30th June 2011 at 05:16.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Non-rectangle rubber band implementation

    Qwt implements several different styles of rubberbands: link. I don't know how well they work as I've only used RectRubberBand, but maybe you can glean something from the source.

    HTH

  3. The following user says thank you to norobro for this useful post:

    d_stranz (30th June 2011)

  4. #3
    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: Non-rectangle rubber band implementation

    Yes, I've looked at Qwt and have even implemented my own rubber band styles within the Qwt framework. The Qwt implementation is very complex, since it depends on a state machine and interactions with the rest of the Qwt system. It isn't easy to reuse in a non-Qwt framework. But I'll take another look. There is a polygon picker style, but I think it is implemented using a series of clicks rather than a continuous click and drag.

  5. #4
    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: Non-rectangle rubber band implementation

    OK, I've implemented a custom rubber band class (derived from QWidget) that supports horizontal line, vertical line, rectangle, ellipse, and lasso click-drag styles. It works as desired on a plain old QWidget in a QMainWindow, so now I need to make whatever changes are needed to make it work on top of a QGraphicsView with the ability to select QGraphicsItem instances within the area defined by the path.

  6. #5
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Non-rectangle rubber band implementation

    one or more lines drawn from the window top left to the starting mouse point
    I don't know how do you create the QPainterPath, but it seems like you are not inserting moveTo element to the first point of the path.
    Don't write a post just to thank someone, use "Thanks" button.

  7. #6
    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: Non-rectangle rubber band implementation

    Quote Originally Posted by Rachol View Post
    I don't know how do you create the QPainterPath, but it seems like you are not inserting moveTo element to the first point of the path.
    Actually, I -was- inserting a moveTo element as the first point in the path, that is why I could not understand where the artifacts were coming from. What I believe was happening was that the last element was not a "closeSubpath", and that was the cause of the spurious lines. After I added that call, the artifacts went away.

    So, my lasso rubberband consists of a moveTo call, followed by a series of lineTo calls that track the mouse position. I then use QPainterPath::toFillPolygon() to create a polygon, add this to a new path and append a closeSubpath() to that. I draw this new closed polygon (as a path) in the paint event. Works perfectly.

    Using the QPainterPath as a way to track mouse movement lets me implement the rubber band as a horizontal or vertical line, rectangle, ellipse, or lasso (free-form polygon). The start point is captured on mousePressEvent, and the correct path is created (or appended to) in the mouseMoveEvent.

    One curious thing: in the mouseMoveEvent, the event->button() is always Qt::NoButton, even though a click-drag is in progress with the left mouse button pressed (at least on Windows).

  8. #7
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Non-rectangle rubber band implementation

    Quote Originally Posted by d_stranz View Post
    now I need to make whatever changes are needed to make it work on top of a QGraphicsView with the ability to select QGraphicsItem instances within the area defined by the path.
    I did that once, be aware you'll have to deal with zoom in graphics scene if your app supports this feature.
    Do you plan to draw the lasso on graphicsView foreground ?

  9. #8
    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: Non-rectangle rubber band implementation

    Quote Originally Posted by totem View Post
    I did that once, be aware you'll have to deal with zoom in graphics scene if your app supports this feature.
    It is worse than that. I will have to restrict the allowed area for selection to a sub-area within the scene. Fortunately, the location of this sub-area is more-or-less fixed, so I can determine on mouse press whether the mouse is within the allowed area or not.

    Do you plan to draw the lasso on graphicsView foreground ?
    No, it will be drawn in an independent QWidget that is a child of the QGraphicsView, much the same way that QRubberBand itself works. The rubber band class will emit a signal containing the closed QPainterPath defined by the lasso. The view will be responsible for converting this into appropriate scene coordinates and then selecting items within the path bounds.

    In principle, any widget could serve as the parent for the rubber band widget, but my current intent is to use it with a graphics view. Hmmm, using a lasso to select a non-rectangular section of a table....

    This is all still under development, so I am sure something will bite me before it is all over. One of those might be if the view has scroll bars - what will happen if the scene scrolls as the user enlarges the selection area? I think my allowed selection area could become invalid.
    Last edited by d_stranz; 5th July 2011 at 20:44.

Similar Threads

  1. Conditional rubber band selection in QGraphicsView
    By stevel in forum Qt Programming
    Replies: 5
    Last Post: 14th January 2011, 08:32
  2. How do I clear the rubber band after a selection is done
    By aarelovich in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2010, 12:29
  3. Replies: 2
    Last Post: 17th June 2010, 13:38
  4. How to draw rubber band using XOR
    By lni in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2009, 12:13
  5. Rubber band artifacts?
    By mooreaa in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2008, 18:19

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.