Results 1 to 6 of 6

Thread: QGraphicsItem paint problem

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

    Default QGraphicsItem paint problem

    Hello,

    I have following files

    architem.h
    =========

    Qt Code:
    1. #ifndef ARCITEM_H
    2. #define ARCITEM_H
    3.  
    4. #include <QtWidgets>
    5.  
    6. class ArcItem : public QGraphicsItem
    7. {
    8. public:
    9. ArcItem(QGraphicsItem *parent=0);
    10. ~ArcItem();
    11.  
    12. QRectF boundingRect() const;
    13. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    14. void setRect(QRectF rct);
    15.  
    16. protected:
    17. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
    18.  
    19. private:
    20. QRectF arcRect;
    21. int startAngle;
    22. int spanAngle;
    23.  
    24. };
    25.  
    26. #endif // ARCITEM_H
    To copy to clipboard, switch view to plain text mode 

    arcitem.cpp
    =========

    Qt Code:
    1. #include "arcitem.h"
    2.  
    3. ArcItem::ArcItem(QGraphicsItem *parent): QGraphicsItem(parent),
    4. arcRect(0,0,100,100),
    5. startAngle(1*16),
    6. spanAngle(200*16)
    7. {
    8. setFlags(ItemIsSelectable|ItemIsMovable|ItemSendsGeometryChanges);
    9. setData(0,"ArcItem");
    10. }
    11.  
    12. ArcItem::~ArcItem()
    13. {
    14.  
    15. }
    16.  
    17. QVariant ArcItem::itemChange(GraphicsItemChange change, const QVariant &value)
    18. {
    19. return QGraphicsItem::itemChange(change, value);
    20. }
    21.  
    22.  
    23.  
    24. QRectF ArcItem::boundingRect() const
    25. {
    26. return arcRect;
    27. }
    28.  
    29. void ArcItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    30. {
    31. Q_UNUSED(option)
    32. Q_UNUSED(widget)
    33. painter->save();
    34. painter->drawArc(arcRect,startAngle,spanAngle);
    35. painter->restore();
    36. }
    37.  
    38. void ArcItem::setRect(QRectF rct)
    39. {
    40. arcRect = rct;
    41. update(rct);
    42. }
    To copy to clipboard, switch view to plain text mode 


    When I am resizing this item using handles, it show uncleared lines on scene as the picture attached.
    Actually, boundingrect is set from outside of this class by calling setRect custom method in this class.
    Although item gets resized, it behaves as per attachment.
    Any help on this?


    Thanks

    Manish
    Attached Images Attached Images

  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: QGraphicsItem paint problem

    The new rect is not necessarly encompassing the old rect, right?

    So calling update with just the new rect might leave area unupdated that was part of the previous rect.

    Maybe call update with the union of the old and the new rect?

    Cheers,
    _

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

    Default Re: QGraphicsItem paint problem

    Thanks Anda.
    I did that as well but still the problem persists. I have infact updated bigger viewport as well.

  4. #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: QGraphicsItem paint problem

    Is your item the only item showing that problem?

    Have you tried without the ItemSendsGeometryChanges flag?

    Cheers,
    _

  5. #5
    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: QGraphicsItem paint problem

    I don't see you calling prepareGeometryChange() after the bounding rect is changed.
    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.


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

    Default Re: QGraphicsItem paint problem

    Thanks Wysota.
    Yes, prepareGeometryChange() has made the trick. Now it is working well and the problem is solved.


    Thanks

    Manish

Similar Threads

  1. Custom QGraphicsItem paint function problem
    By andzero in forum Newbie
    Replies: 1
    Last Post: 12th October 2011, 19:35
  2. Help on QGraphicsItem::paint please
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2011, 09:11
  3. QGraphicsItem paint not triggered
    By roband915 in forum Qt Programming
    Replies: 9
    Last Post: 31st March 2011, 11:06
  4. Qgraphicsitem parent/child paint problem.
    By repka3 in forum Qt Programming
    Replies: 1
    Last Post: 24th July 2009, 23:03
  5. Paint QGraphicsItem problem
    By dreamer in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2008, 19:18

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.