Results 1 to 20 of 26

Thread: Rubberband relative to QGraphicsScene

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    37
    Thanked 53 Times in 40 Posts

    Default Re: Rubberband relative to QGraphicsScene

    I had a look at the sources of qt and was shocked to see their own rubberband implementation for the QGView! They are drawing the rubberband from a QPainter manually in paintEvent() and updating manually. They aren;t using QRubberBand widget at all now!

    Anyways i really feel there is something related to hovering which is giving you pains. Have you tried my example and checked performance issue ?
    It would be better if you could post a minimal working example.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  2. #2
    Join Date
    Aug 2006
    Posts
    250
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    19
    Thanked 49 Times in 36 Posts

    Default Re: Rubberband relative to QGraphicsScene

    Quote Originally Posted by Gopala Krishna View Post
    I had a look at the sources of qt and was shocked to see their own rubberband implementation for the QGView! They are drawing the rubberband from a QPainter manually in paintEvent() and updating manually. They aren;t using QRubberBand widget at all now!
    Ah yes, I suspected something like that.

    Anyways i really feel there is something related to hovering which is giving you pains. Have you tried my example and checked performance issue ?
    It would be better if you could post a minimal working example.
    Yes, that's next on the list. I tried your example on my work machine and there are no problems with performance or weird selection behaviour. I'll modify that minimal example to handle the rubber band in the view and check if it still works.
    Last edited by pherthyl; 12th December 2007 at 21:07.

  3. #3
    Join Date
    Aug 2006
    Posts
    250
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    19
    Thanked 49 Times in 36 Posts

    Default Re: Rubberband relative to QGraphicsScene

    Quote Originally Posted by pherthyl View Post
    I'll modify that minimal example to handle the rubber band in the view and check if it still works.
    Ok, so with the following code it works fine on my work computer. I saw the "no mouse release event" bug once or twice but now I can't reproduce it anymore. I will try this same code at home and see what the situation is there.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class View : public QGraphicsView
    4. {
    5. public:
    6. View() : QGraphicsView()
    7. {
    8. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    9. }
    10.  
    11. protected:
    12. void mouseMoveEvent(QMouseEvent* evt) {
    13. if(evt->buttons().testFlag(Qt::LeftButton)) {
    14. // handle the rubberband
    15. if(rubberBand->isVisible()) {
    16. QPoint mouseDownView = mapFromScene(mouseDownPos);
    17. lastMouseViewPos = evt->pos();
    18. QRect rubberRect(mouseDownView, lastMouseViewPos);
    19. rubberRect = rubberRect.normalized();
    20. rubberBand->setGeometry(rubberRect);
    21. QPolygonF p = mapToScene(rubberRect);
    22. path.addPolygon(p);
    23. scene()->setSelectionArea(path, Qt::IntersectsItemShape);
    24. }
    25. }
    26. QGraphicsView::mouseMoveEvent(evt);
    27. }
    28.  
    29. void mouseReleaseEvent(QMouseEvent* evt) {
    30. qDebug() << "release";
    31. rubberBand->hide();
    32. QGraphicsView::mouseReleaseEvent(evt);
    33. }
    34.  
    35. void mousePressEvent(QMouseEvent* evt) {
    36. qDebug() << "press";
    37. QGraphicsView::mousePressEvent(evt);
    38. if(!scene()->itemAt(mapToScene(evt->pos()))) {
    39. mouseDownPos = mapToScene(evt->pos()).toPoint();
    40. lastMouseViewPos = evt->pos();
    41. rubberBand->setGeometry(mapFromScene(mouseDownPos).x(), mapFromScene(mouseDownPos).y(), 0,0);
    42. rubberBand->show();
    43. }
    44. }
    45. private:
    46. QPoint mouseDownPos;
    47. QPoint lastMouseViewPos;
    48. QRubberBand *rubberBand;
    49. };
    50.  
    51. void addItems(View *s)
    52. {
    53. QGraphicsEllipseItem *item = s->scene()->addEllipse(10,10, 150, 150);
    54. item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    55.  
    56. item = s->scene()->addEllipse(100,210, 50, 150);
    57. item->setFlags(QGraphicsItem::ItemIsSelectable);
    58.  
    59. }
    60.  
    61. int main(int argc, char *argv[])
    62. {
    63. QApplication app(argc, argv);
    64. QRectF rect = QRectF(0,0, 1024,768);
    65. QGraphicsScene* scene = new QGraphicsScene(rect);
    66. View *s = new View();
    67. s->setScene(scene);
    68. addItems(s);
    69.  
    70. s->show();
    71. return app.exec();
    72. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2006
    Posts
    250
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    19
    Thanked 49 Times in 36 Posts

    Default Re: Rubberband relative to QGraphicsScene

    Quote Originally Posted by pherthyl View Post
    I tried your example on my work machine and there are no problems with performance or weird selection behaviour.
    No performance problems on my home machine with that simple example either.

    Turns out the problem in my app wasn't the rubberband but the drawing of the background. In my drawbackground I was drawing borders for pages, in a not-so-efficient way.

    Qt Code:
    1. void ProjectScene::drawBackground(QPainter *painter, const QRectF &rect) {
    2. if(renderBackground) {
    3. painter->setBrush(Qt::white);
    4. painter->setPen(Qt::black);
    5.  
    6. int hPages = getNumHorizontalPages();
    7. int vPages = getNumVerticalPages();
    8. QRectF currPage;
    9. for(int h = 0; h < hPages; h++) {
    10. for(int v = 0; v < vPages; v++) {
    11. currPage.setRect(h*pageSize.width(), v*pageSize.height(), pageSize.width(), pageSize.height());
    12. if(currPage.intersects(rect)) {
    13. painter->drawRect(currPage);
    14. }
    15. }
    16. }
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    I'll have to rethink that function.

    So now I just have to figure out the strange selection bug...

  5. #5
    Join Date
    Aug 2006
    Posts
    250
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    19
    Thanked 49 Times in 36 Posts

    Default Re: Rubberband relative to QGraphicsScene

    Quote Originally Posted by pherthyl View Post
    So now I just have to figure out the strange selection bug...
    Aha! Now I've isolated the problem.

    Can someone please try out the attached code and select the items as shown in the screenshot? The ellipse is correctly selected, while the rectangle is not. Not sure where this is coming from yet. Something to do with that the rect item has a default shape implementation?
    Attached Images Attached Images
    Attached Files Attached Files

  6. #6
    Join Date
    Aug 2006
    Posts
    250
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    19
    Thanked 49 Times in 36 Posts

    Default Re: Rubberband relative to QGraphicsScene

    Well I fixed the selection bug by implementing my own version of setSelectionArea and reporting the bug to Trolltech. Now everything works.

  7. #7
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    37
    Thanked 53 Times in 40 Posts

    Default Re: Rubberband relative to QGraphicsScene

    Nice, but what bug was there in qt's setSelectionArea function ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  8. #8
    Join Date
    Aug 2006
    Posts
    250
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    19
    Thanked 49 Times in 36 Posts

    Default Re: Rubberband relative to QGraphicsScene

    Quote Originally Posted by Gopala Krishna View Post
    Nice, but what bug was there in qt's setSelectionArea function ?
    No idea. I just gave them the compileable example and let them figure it out
    But I wrote my own that just iterates through all the items and uses either shape().intersects() (for line items) and sceneBoundingRect().intersects() for my boxes. Maybe not so great for large numbers of items (haven't checked how qt does it), but I don't anticipate having more than about 100 on the scene at once.

  9. #9
    Join Date
    Aug 2006
    Posts
    250
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    19
    Thanked 49 Times in 36 Posts

    Default Re: Rubberband relative to QGraphicsScene

    Got a response from Trolltech support:

    This does indeed look like a bug in Qt. The problem is most likely the
    same as the one reported here

    http://trolltech.com/developer/task-...ntry&id=191706

    as QGraphicsScene uses QPainterPath for calculating whether the
    rectangle intersects with the selection rectangle.

    We'll verify that fixing this also solves your problem.

    Thanks for reporting.

  10. #10
    Join Date
    Aug 2006
    Posts
    250
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    19
    Thanked 49 Times in 36 Posts

    Default Re: Rubberband relative to QGraphicsScene

    Quote Originally Posted by pherthyl View Post
    I'll have to rethink that function.
    CacheBackground fixed this problem.

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37
  2. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  3. QPrinter on QGraphicsScene Border Problem
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 15:49
  4. QGraphicsScene and QGraphicsView
    By rossd in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2007, 14:43
  5. When to use QGraphicsScene or QWidget
    By fossill in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2007, 23:58

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
  •  
Qt is a trademark of The Qt Company.