C++ (Qt) class Item: public QGraphicsItem{public: Item(QGraphicsItem*parent = 0) :QGraphicsItem(parent),down(false){} QRectF boundingRect() const { return QRectF(0,0,100,70); } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QPen pen(QBrush(Qt::black),3, Qt::SolidLine); painter->setBrush(down ? Qt::red : Qt::green); painter->setPen(pen); painter->drawLine(10,10,200,10); painter->drawLine(200,10,200,50); painter->drawLine(200,50,50,50); painter->drawLine(50,50,50,70); painter->drawLine(50,70,100,70); painter->drawLine(100,70,100,100); painter->drawLine(10,10,10,100); painter->drawLine(10,100,100,100); } protected: void mousePressEvent(QGraphicsSceneMouseEvent* event) { if(!down) { down = true; update(); qDebug()<<down; } } void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) { if(event->buttons() == 0) { down = false; update(); qDebug()<<down; } } private: bool down;};
boundingRect();
C++ (Qt)class Item: public QGraphicsItem{public: Item(QGraphicsItem*parent = 0) :QGraphicsItem(parent),down(false){} QRectF boundingRect() const { return QRectF(0,0,300,200); } QPainterPath shape() const { QPainterPath path; QPolygon polygon; polygon << QPoint(10 ,10) << QPoint(200,10) << QPoint(200,50) << QPoint(100,50) << QPoint(100,80) << QPoint(150,80) << QPoint(150,200) << QPoint(10,200) << QPoint(10,10); path.addPolygon(polygon); return path; } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QPen pen(QBrush(Qt::black),3, Qt::SolidLine); painter->setBrush(down ? Qt::red : Qt::green); painter->setPen(pen); painter->drawPath(shape()); } protected: void mousePressEvent(QGraphicsSceneMouseEvent* event) { if(!down) { down = true; update(); qDebug()<<down; } } void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) { if(event->buttons() == 0) { down = false; update(); qDebug()<<down; } } private: bool down;};
C++ (Qt) explicit GraphicsScene(QObject*parent = 0): QGraphicsScene(parent) { addPixmap(QPixmap("D:/Qt/Qt5.2.1/Tools/QtCreator/bin/Graphics/lodka.bmp")); sceneImage = new QImage("D:/Qt/Qt5.2.1/Tools/QtCreator/bin/Graphics/lodka.bmp"); } ... void mousePressEvent(QGraphicsSceneMouseEvent* event) { qDebug()<<event->scenePos(); //положнеие курсора относительно сцены QPointF scenePoint = event->scenePos(); sceneImage->setPixel( (int)(scenePoint.x()), (int)(scenePoint.y()) ,???); }
void QImage::setPixel ( int x, int y, uint index_or_rgb )This is an overloaded function.Sets the pixel index or color at (x, y) to index_or_rgb.
update()
C++ (Qt)class GraphicsScene: public QGraphicsScene{ Q_OBJECT public: explicit GraphicsScene(QObject*parent = 0): QGraphicsScene(parent) { sceneImage = new QImage("D:/Qt/Qt5.2.1/Tools/QtCreator/bin/Graphics/lodka.bmp"); QPixmap pixmap; addPixmap(pixmap.fromImage(*sceneImage)); } virtual ~GraphicsScene() { delete sceneImage; } protected: void mousePressEvent(QGraphicsSceneMouseEvent* event) { qDebug()<<event->scenePos(); //положнеие курсора относительно сцены QPointF scenePoint = event->scenePos(); sceneImage->setPixel( (int)(scenePoint.x()), (int)(scenePoint.y()) ,155); QPixmap pixmap; addPixmap(pixmap.fromImage(*sceneImage)); } private: QImage *sceneImage;};