Results 1 to 20 of 21

Thread: add pixels to qgraphicsitem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    Join Date
    Nov 2010
    Posts
    315
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanked 53 Times in 51 Posts

    Default Re: add pixels to qgraphicsitem

    From my experience you should restore state of painter at the end of paint.
    Without it you can have strange artifacts. Of course if you are using some optimization flag to auto store painter then this problem will not appear.
    When you are writing own graphics item you shouldn't try to evaluate impact of transformation, especially when you have little experience with graphics freamework.

    Try correct this code like that:
    Qt Code:
    1. class MyRect : public QObject, public QGraphicsRectItem
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyRect( QGraphicsItem *pParent=0 )
    7. : QGraphicsRectItem( pParent )
    8. {
    9. setItUp();
    10. }
    11. MyRect( const QRectF & rect, QGraphicsItem* pParent=0 )
    12. : QGraphicsRectItem( rect, pParent )
    13. {
    14. setItUp();
    15. }
    16. ~MyRect() {}
    17.  
    18. void setItUp()
    19. {
    20. m_penWidth = 1.0;
    21. setPen( QPen(Qt::green) );
    22. setAcceptHoverEvents( true );
    23. setFlag( QGraphicsItem::ItemIsSelectable, true );
    24. }
    25.  
    26. QRectF boundingRect() const
    27. { //looks ok
    28. return this->rect().adjusted( -m_penWidth, -m_penWidth,
    29. m_penWidth, m_penWidth );
    30. }
    31.  
    32. protected:
    33. void paint( QPainter *painter,
    34. const QStyleOptionGraphicsItem *option,
    35. QWidget *widget )
    36. {
    37. Q_UNUSED(widget);
    38.  
    39. // this way is faster then painter->store();
    40. QPen oldPen = painter->pen();
    41. QBrush oldoldBrush = painter->brush();
    42.  
    43. painter->setBrush(brush());
    44. if (option->state & QStyle::State_Selected) {
    45. painter->setPen(pen());
    46. } else {
    47. QPen hilitePen(QBrush(Qt::red),m_penWidth);
    48. painter->setPen(hilitePen);
    49. }
    50. painter->drawRect(rect());
    51.  
    52. // restore painter:
    53. painter->setPen(oldPen);
    54. painter->setBrush(oldBrush);
    55. }
    56.  
    57. private:
    58. qreal m_penWidth;
    59. };
    To copy to clipboard, switch view to plain text mode 

    Another thing why you are trying do it yourself?
    Why not just manipulate properties of QGraphicsRectItem?
    Just use QAbstractGraphicsShapeItem::setBrush and QAbstractGraphicsShapeItem::setPen when state of item is changed.
    Re-implement itemChange method to react on that.
    Last edited by MarekR22; 13th February 2011 at 18:17.

Similar Threads

  1. Replies: 9
    Last Post: 13th June 2012, 15:42
  2. Replies: 5
    Last Post: 23rd January 2011, 11:27
  3. QGraphicsItem mousePressEvent only on opaque pixels
    By jonks in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2010, 10:24
  4. Why must I pad boundingRect() by 7 pixels
    By xenome in forum Qt Programming
    Replies: 3
    Last Post: 10th September 2009, 14:07
  5. Pixels
    By Dante in forum Qt Programming
    Replies: 1
    Last Post: 21st April 2009, 20:50

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.