Results 1 to 4 of 4

Thread: Trouble painting customized QGraphicsPathItem

  1. #1
    Join Date
    Jun 2016
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Trouble painting customized QGraphicsPathItem

    Hello,
    I am trying to create a class that inherits from QGraphicsPathItem so that the class can have some custom data members. This will be helpful when selecting an item on the scene with a mouse click and then displaying or changing attributes of that selected item.
    The code that adds a new path to to a view is:
    Qt Code:
    1. void processPath(QPainterPath newPath)
    2. {
    3. newPath = mapToScene(newPath);
    4. QGraphicsPathItem *newItem = new QGraphicsPathItem(newPath);
    5. newItem->setPen(ItemPen);
    6. newItem->setBrush(ItemFill);
    7. CareZone *newCZ = new CareZone(newItem);
    8. CZGraphicsItem *newCZGI = new CZGraphicsItem(newItem,newCZ);
    9.  
    10. scene()->addItem(newCZGI);
    11. carezones.push_back(newCZ);
    12. viewport()->update();
    13. }
    To copy to clipboard, switch view to plain text mode 

    If I change scene()->addItem(newCZGI) to scene()->addItem(newItem) it works but then it is using a QGraphicsPathItem and not a the customized CZGraphicsItem. CareZone is a class that contains data members. CZGraphicsItem contains a pointer to it.

    I have added paint() and boundingRect() functions to the CZGraphicsItem class.

    The paint method is :
    Qt Code:
    1. void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0)
    2. {
    3. qDebug() << "paint called" ;
    4. painter->setPen(gpi->pen());
    5. painter->setBrush(gpi->brush());
    6. painter->drawPath(gpi->shape());
    7. QGraphicsPathItem::paint(painter,option,widget);
    8. }
    To copy to clipboard, switch view to plain text mode 
    I think it should use the same paint method used by QGraphicsPathItem but I do not know how to do this. The same is true for boundingRect().
    The boundingRect method is:
    Qt Code:
    1. QRectF boundingRect()
    2. {
    3. qDebug() << "boundRect() called";
    4. return gpi->boundingRect();
    5. }
    To copy to clipboard, switch view to plain text mode 
    gpi is the QGraphicsPathItem passed in the constructor (newItem in the first snippet of code).

    I am not seeing either debugging message when running the applications. I do see a debug message that I placed in main though.

    Thanks,
    jkutchka

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Trouble painting customized QGraphicsPathItem

    If CZGraphicsItem is a QGraphicsPathItem, why doesn't it get a path?
    What do you create newItem for?
    Why are you passing it to CZGraphicsItem's constructor?

    Cheers,
    _

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

    jkutchka (15th June 2016)

  4. #3
    Join Date
    Jun 2016
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Smile Re: Trouble painting customized QGraphicsPathItem

    The problem was in the CZGraphicsItem constructor. I had:
    Qt Code:
    1. CZGraphicsItem(QGraphicsPathItem *GPI, CareZone *cz) :
    To copy to clipboard, switch view to plain text mode 
    expecting a copy of the QGraphicsPathItem. Changing it to:
    Qt Code:
    1. CZGraphicsItem(QGraphicsPathItem *GPI, CareZone *cz) :
    2. QGraphicsPathItem(GPI->shape())
    To copy to clipboard, switch view to plain text mode 
    fixed the problem.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Trouble painting customized QGraphicsPathItem

    Why not just pass the path?

    Cheers,
    _

Similar Threads

  1. Combine QGraphicsPathItem
    By mvbhavsar in forum Newbie
    Replies: 4
    Last Post: 11th February 2015, 12:08
  2. qgraphicspathitem how to get the outline
    By tf520 in forum Qt Programming
    Replies: 2
    Last Post: 27th September 2014, 23:05
  3. QGraphicsPathItem
    By tonio in forum Qt Programming
    Replies: 4
    Last Post: 25th June 2009, 14:35
  4. QGraphicsPathItem Doubt
    By arjunasd in forum Qt Programming
    Replies: 7
    Last Post: 29th July 2007, 03:30
  5. Trouble with image painting (QPainter + QImage)
    By krivenok in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2006, 20:25

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.