Results 1 to 3 of 3

Thread: How to add items in a QGraphicsScene?

  1. #1
    Join Date
    Jul 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default How to add items in a QGraphicsScene?

    Hi,

    I am trying to add some custom QGraphicsItems in a QGraphicsScene on mouse click and at mouse cursor coordinates. But the items are not added at the same coordinates as the mouse cursor's.

    Qt Code:
    1. renderArea::renderArea(QWidget *parent):
    2. QGraphicsView(parent)
    3. {
    4. scene = new QGraphicsScene(this);
    5. scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    6. scene->setSceneRect(0, 0, 850, 480);
    7. setScene(scene);
    8. setCacheMode(CacheBackground);
    9. setViewportUpdateMode(BoundingRectViewportUpdate);
    10. setRenderHint(QPainter::Antialiasing);
    11. setTransformationAnchor(AnchorUnderMouse);
    12. scale(qreal(1.0), qreal(1.0));
    13. setMinimumSize(400, 400);
    14. }
    15.  
    16. void renderArea::mousePressEvent(QMouseEvent *event)
    17. {
    18. QPoint p = event->pos();
    19.  
    20. updateList(p);
    21. }
    22.  
    23. void renderArea::updateList(const QPoint &p)
    24. {
    25. Point point;
    26. point.point = p;
    27. point.isSelected = false;
    28. list.append(point);
    29. if (list.size() > 1)
    30. updateClothoid(list[list.size()-2].point, list[list.size()-1].point);
    31. }
    32.  
    33. void renderArea::updateClothoid(const QPoint &p1, const QPoint &p2)
    34. {
    35. Clothoid *temp = new Clothoid(p1, p2);
    36.  
    37. clothoids.append(temp);
    38.  
    39. scene->addItem(temp);
    40. }
    To copy to clipboard, switch view to plain text mode 

    renderArea being the QGraphicsView and Clothoids the custom QGraphicsItem

    Qt Code:
    1. Clothoid::Clothoid(QPoint startPoint, QPoint endPoint)
    2. {
    3. sPoint = startPoint;
    4. ePoint = endPoint;
    5. startCurvature = 0.0;
    6. endCurvature = 0.0;
    7. clothoidLength = sqrt(pow(endPoint.x() - startPoint.x(),2) +
    8. pow(endPoint.y() - startPoint.y(),2));
    9. }
    10.  
    11. QRectF Clothoid::boundingRect() const
    12. {
    13. qreal penWidth = 1;
    14.  
    15. if ((sPoint.x() &lt ePoint.x()) && (sPoint.y() &lt ePoint.y()))
    16. return QRectF(sPoint.x(), sPoint.y(), ePoint.x() - sPoint.x(), ePoint.y()-sPoint.y())
    17. .normalized()
    18. .adjusted(-penWidth, -penWidth, penWidth, penWidth);
    19.  
    20. if ((sPoint.x() &lt ePoint.x()) && (sPoint.y() > ePoint.y()))
    21. return QRectF(sPoint.x(), ePoint.y(), ePoint.x() - sPoint.x(), sPoint.y() - ePoint.y())
    22. .normalized()
    23. .adjusted(-penWidth, -penWidth, penWidth, penWidth);
    24.  
    25. if ((sPoint.x() > ePoint.x()) && (sPoint.y() &lt ePoint.y()))
    26. return QRectF(ePoint.x(), sPoint.y(), sPoint.x() - ePoint.x(), ePoint.y()-sPoint.y())
    27. .normalized()
    28. .adjusted(-penWidth, -penWidth, penWidth, penWidth);
    29.  
    30. if ((sPoint.x() > ePoint.x()) && (sPoint.y() > ePoint.y()))
    31. return QRectF(ePoint.x(), ePoint.y(), sPoint.x() - ePoint.x(), sPoint.y() - ePoint.y())
    32. .normalized()
    33. .adjusted(-penWidth, -penWidth, penWidth, penWidth);
    34.  
    35. return QRectF();
    36.  
    37. }
    38.  
    39. void Clothoid::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    40. {
    41. QLineF line(sPoint, ePoint);
    42.  
    43. // Draw the line itself
    44. painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    45. painter->drawLine(line);
    46. }
    To copy to clipboard, switch view to plain text mode 

    I am guessing that the coordinates to which I am inserting the items belong to the GraphicsView and not the scene as in my application the scene doesn't cover the entire view. But how could I get the coordinates of the scene in my case?

  2. #2
    Join Date
    Aug 2011
    Location
    Canada
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to add items in a QGraphicsScene?

    hmm, i may not be seeing the code which actuates the rendering to your scene in its entirety, but i would expect to see a 'QPointF QGraphicsView::mapToScene ( const QPoint & point ) const' in use to translate the click coordinates from the View to Scene coordinates, probably in updateList(). is this what you meant?

  3. #3
    Join Date
    Jul 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to add items in a QGraphicsScene?

    yes..that was exactly what I meant. Thanks. It works now.

Similar Threads

  1. QGraphicsScene and Extended Selection of items
    By totem in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2010, 08:37
  2. Animating many items in a QGraphicsScene
    By Luc4 in forum Qt Programming
    Replies: 6
    Last Post: 5th May 2010, 06:55
  3. Position of Items in QGraphicsScene/QGraphicsView
    By StefanK2 in forum Qt Programming
    Replies: 11
    Last Post: 7th July 2009, 14:04
  4. How could I save Items on a QGraphicsScene?
    By pinkfrog in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 05:03
  5. Adding QGraphicsProxyWidget items into QGraphicsScene
    By yagabey in forum Qt Programming
    Replies: 3
    Last Post: 21st November 2008, 06:33

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.