Results 1 to 5 of 5

Thread: Combine QGraphicsPathItem

  1. #1
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Combine QGraphicsPathItem

    Hello,


    I have 2 QGraphicsPathItem having there own QPainterPath set within it.
    Now I want 2nd QGraphicsPathItem to get connected to 1st QGraphicsPathItem using connectPath or any other function so that resultant QGraphicsPathItem is 1st QGraphicsPathItem.

    I don't want to do this within single QGraphicsPathItem since I will not be able to modify individual elements at run time.

    How to implement this?


    Thanks

    Manish

  2. #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: Combine QGraphicsPathItem

    Subclass the item, teach it to accept pointers to other instances of that class and use itemChange() method to move those items when your item moves.
    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.


  3. #3
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Combine QGraphicsPathItem

    I have following code but still it is not working.

    Qt Code:
    1. #include "cubeitem.h"
    2.  
    3. CubeItem::CubeItem(QGraphicsItem *parent):QGraphicsPathItem(parent),
    4. m_Point1(10,10),
    5. m_Point2(100,100),
    6. m_Point3(50,150)
    7. {
    8. startPoint = m_Point1;
    9. setFlags(ItemIsSelectable|ItemIsMovable);
    10. setShapePath();
    11. }
    12.  
    13. CubeItem::~CubeItem()
    14. {
    15.  
    16. }
    17.  
    18. void CubeItem::setLastItem(QGraphicsPathItem *pi)
    19. {
    20. startPoint = pi->shape().currentPosition();
    21. QPainterPath path1 = pi->shape();
    22. QPainterPath path2;
    23. path2.moveTo(path1.currentPosition());
    24. path2.addPath(shape());
    25. path1.connectPath(path2);
    26. pi->setPath(path1);
    27. setPath(path2);
    28. update();
    29. }
    30.  
    31. void CubeItem::setPoint1(QPointF arg)
    32. {
    33. m_Point1 = arg;
    34. setShapePath();
    35. update();
    36. }
    37. void CubeItem::setPoint2(QPointF arg)
    38. {
    39. m_Point2 = arg;
    40. setShapePath();
    41. update();
    42. }
    43.  
    44. void CubeItem::setPoint3(QPointF arg)
    45. {
    46. m_Point3 = arg;
    47. setShapePath();
    48. update();
    49. }
    50.  
    51. void CubeItem::setShapePath()
    52. {
    53. // path.moveTo(startPoint);
    54. path.cubicTo(m_Point1, m_Point2, m_Point3);
    55. setPath(path);
    56. setBrush(QBrush(Qt::yellow));
    57. QPen pen(Qt::gray);
    58. pen.setWidth(0);
    59. setPen(pen);
    60. }
    To copy to clipboard, switch view to plain text mode 


    Also, in mainwindow class, I have following code written on button clicked.

    Qt Code:
    1. void MainWindow::on_tbCube_clicked()
    2. {
    3. cbi = new CubeItem;
    4. scene->addItem(cbi);
    5. if (scene->selectedItems().size()>0){
    6. QGraphicsPathItem *pi = qgraphicsitem_cast<QGraphicsPathItem*>(scene->selectedItems().last());
    7. if (pi){
    8. cbi->setLastItem(pi);
    9. }
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  4. #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: Combine QGraphicsPathItem

    In what way is this code supposed to "be working"?
    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.


  5. #5
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Combine QGraphicsPathItem

    Made the changes as below. Now what is happening is that if I move the second item after selecting, it does not retain first item's last point. And hence both looks detached.


    Qt Code:
    1. #include "cubeitem.h"
    2.  
    3. CubeItem::CubeItem(QGraphicsPathItem *pi, QGraphicsItem *parent):QGraphicsPathItem(parent),QObject(),
    4. m_Point1(10,10),
    5. m_Point2(100,100),
    6. m_Point3(50,150),
    7. m_pi(pi)
    8. {
    9. if (pi){
    10. startPoint = pi->shape().currentPosition();
    11. startPoint = mapFromItem(pi,startPoint);
    12. }
    13. else{
    14. startPoint = m_Point1;
    15. }
    16. setFlags(ItemIsSelectable|ItemIsMovable|ItemSendsGeometryChanges);
    17. setData(0,"CubeItem");
    18. setShapePath();
    19. qDebug() << startPoint;
    20. }
    21.  
    22. CubeItem::~CubeItem()
    23. {
    24.  
    25. }
    26.  
    27. void CubeItem::setPoint1(QPointF arg)
    28. {
    29. m_Point1 = arg;
    30. setShapePath();
    31. update();
    32. }
    33. void CubeItem::setPoint2(QPointF arg)
    34. {
    35. m_Point2 = arg;
    36. setShapePath();
    37. update();
    38. }
    39.  
    40. void CubeItem::setPoint3(QPointF arg)
    41. {
    42. m_Point3 = arg;
    43. setShapePath();
    44. update();
    45. }
    46.  
    47. void CubeItem::setShapePath()
    48. {
    49. path.moveTo(startPoint);
    50. qDebug() << m_Point1 << "=="<< m_Point2 << "=="<< m_Point3;
    51. path.cubicTo(m_Point1, m_Point2, m_Point3);
    52. setPath(path);
    53. QPen pen(Qt::gray);
    54. pen.setWidth(0);
    55. setPen(pen);
    56. }
    57.  
    58.  
    59.  
    60. QVariant CubeItem::itemChange(GraphicsItemChange change, const QVariant &value)
    61. {
    62. if (change==ItemPositionChange){
    63. if (m_pi){
    64. setPoint1(startPoint);
    65. }
    66. }
    67. return value;
    68. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. qgraphicspathitem how to get the outline
    By tf520 in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2014, 00:05
  2. QGraphicsPathItem
    By tonio in forum Qt Programming
    Replies: 4
    Last Post: 25th June 2009, 15:35
  3. QGraphicsPathItem - slow on Mac, fine on X11
    By aeh in forum Qt Programming
    Replies: 0
    Last Post: 18th May 2009, 13:42
  4. Increasing the selection area for a qgraphicsPathitem
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2007, 00:03
  5. QGraphicsPathItem Doubt
    By arjunasd in forum Qt Programming
    Replies: 7
    Last Post: 29th July 2007, 04:30

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.