Qt :: QGraphicsPixmapItem : overriding paint() method? -
I want to override QGraphicsPixmapItem which can show an image and can use QPainter on the same item.
I have overridden classes and mouse events and paint methods. Every time I click the button it successfully calls icons and paint events but this image does not paint on pixmap.
I load this item object by using the setPixmap function i.e.
QGraphicsSelectionPixmapItem selectitem; Selectionitem-> SetPixmap (imagePixmap);
This is my override class:
Header file: qgraphicsselectionpixmapitem.h
#ifndef QGRAPHICSSELECTIONPIXMAPITEM_H #define QGRAPHICSSELECTIONPIXMAPITEM_H # Include & Lt; QGraphicsPixmapItem & gt; # Include & lt; QDebug & gt; # Include & lt; QGraphicsSceneMouseEvent & gt; # Include & lt; QList & gt; # Include & lt; QVector & gt; # Include & lt; QPainter & gt; # Include & lt; QPixmap & gt; Class QGraphicsSelectionPixmapItem: Public QGraphicsPixmapItem {Public: QGraphicsSelectionPixmapItem (QGraphicsItem * parent = NULL); Zero mouse muwegit (QGraphicsSceneMouseEvent * E); Zero mousepress event (QGraphicsSceneMouseEvent * E); Zero mouseReleaseEvent (QGraphicsSceneMouseEvent * E); Zero Color (QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget); QList & LT; QPointF & gt; Points; QVector & LT; QPointF & gt; PolyPoints; Bull Empty Flag; }; #endif // QGRAPHICSSELECTIONPIXMAPITEM_H
CPP file: qgraphicsselectionpixmapitem.cpp
#include "qgraphicsselectionpixmapitem.h" QGraphicsSelectionPixmapItem :: QGraphicsSelectionPixmapItem (QGraphicsItem * Guardian): QGraphicsPixmapItem (Original) {} Zero QGraphicsSelectionPixmapItem :: mousePressEvent (QGraphicsSceneMouseEvent * E) {mClickflag = true; QDebug () & lt; & Lt; E & gt; Event (); If (e-> Lastposs ()! = E-> pos ()) {points.append (e-> pos ()); Polypoints & lt; & Lt; E & gt; Event (); Updates(); }} Zero QGraphicsSelectionPixmapItem :: mouseMoveEvent (QGraphicsSceneMouseEvent * E) {} Zero QGraphicsSelectionPixmapItem :: mouseReleaseEvent (QGraphicsSceneMouseEvent * E) {mClickflag = false; } Zero QGraphicsSelectionPixmapItem :: Color (QPainter * painter, constant QStyleOptionGraphicsItem * option, QWidget * widget) {qDebug () & lt; & Lt; "In Paint"; If (PolyPoints.empty ()) (painter-> set pen (QPen (QColor (255,0,0), 3)); painter-> DrawPolyline (polyPoints);} QGraphicsPixmapItem :: paint (painter , Option, widget);}
I found this solution: Store anything on QGraphicsItem To make and draw, use QPainterPath.
In the header file:
QPainterPath m_path; Bool firstClick;
Modify mousePressEvent and Paint functions in the cpp file:
QGraphicsSelectionPixmapItem :: QGraphicsSelectionPixmapItem (QGraphicsItem * parent): QGraphicsPixmapItem (parent) {firstCli Ck = true} Zero QGraphicsSelectionPixmapItem :: mousePressEvent (QGraphicsSceneMouseEvent * event) {m_path.setFillRule (Qt :: WindingFill); if (first click) {m_path.moveTo (event- & gt; pos ()); firstClick = false;} And m_path.lineTo (event-> pos ()); Update ();} Zero QGraphicsSelectionPixmapItem :: mouseMoveEvent (QGraphicsSceneMouseEvent * event) {update (); } Zero QGraphicsSelectionPixmapItem :: mouseReleaseEvent (QGraphicsSceneMouseEvent * event) {} Zero QGraphicsSelectionPixmapItem :: Paint (QPainter * illustrator, const QStyleOptionGraphicsItem * option, QWidget * widget) {QGraphicsPixmapItem :: Paint (painters, options, widgets); If (m_path.isEmpty ()) returns; QPen Pen (QColor (255,0,0)); Pen.setWidth (1); Painter- & gt; SetPen (pen); Painter- & gt; SetRenderHint (QPainter :: Antialiasing); Painter- & gt; SetBrush (Qcolor (255,0,0,100)); Painter- & gt; DrawPath (m_path); }
Comments
Post a Comment