QGraphicsRectItem that shows an image QPixmap or QImage....
-
Whit the help of some link I'm able to create a subcalss QGraphicsRectItem for responds to some mouse event into GraphicsScene .... It is Right ....
But now I'm not see the way to show into these new QGraphicsRectItem a Qpixmap or better QImage (as static var so better is QImage ....)my .h code
#ifndef HIGHLIGHTRECTITEM_H #define HIGHLIGHTRECTITEM_H #include <QGraphicsRectItem> #include <QPen> #include <QLabel> #include <QGraphicsScene> #include <QGraphicsView> #include <QGraphicsItem> #include <QGraphicsRectItem> #include <QGraphicsPolygonItem> #include <QDesktopWidget> #include <QDialog> #include <QWidget> #include <QWidgetAction> #include <QWidgetData> #include <QWidgetItem> #include <QPixmap> #include <QtWidgets/qstyle.h> #include <QGraphicsRectItem> #include <QGraphicsSceneMouseEvent> class HighlightRectItem : public QGraphicsRectItem { public: static QImage boximg; HighlightRectItem(QGraphicsItem* parent = 0); bool selectRectItem; void setAnchorPoint(const QPointF& anchorPoint); public slots: protected: void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); protected slots: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); private: QPen m_pen; QPointF anchorPoint; bool m_dragged; signals: }; #endif // HIGHLIGHTRECTITEM_H
and my .cpp code ...
#include "highlightrectitem.h" #include <QPainter> HighlightRectItem::HighlightRectItem(QGraphicsItem *parent) : QGraphicsRectItem(parent), m_dragged(false) { setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemSendsGeometryChanges); setAcceptHoverEvents(true); selectRectItem = false; } QImage HighlightRectItem::boximg("/home/niceimage-qt.png"); void HighlightRectItem::setAnchorPoint(const QPointF &anchorPoint) { this->anchorPoint = anchorPoint; } void HighlightRectItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { m_pen.setColor(Qt::red); update(); } void HighlightRectItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { m_pen.setColor(Qt::green); update(); } void HighlightRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { if(selectRectItem) { selectRectItem = false; } else { selectRectItem = true; } } void HighlightRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) { m_dragged = true; QGraphicsRectItem::mouseMoveEvent(event); } void HighlightRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { if(m_dragged){ QList<QGraphicsItem*> colItems = collidingItems(); if(colItems.isEmpty()) this->setPos(anchorPoint); else { QGraphicsItem* closestItem = colItems.at(0); qreal shortestDist = 100000; foreach(QGraphicsItem* item, colItems){ QLineF line(item->sceneBoundingRect().center(), this->sceneBoundingRect().center()); if(line.length() < shortestDist){ shortestDist = line.length(); closestItem = item; } } this->setPos(closestItem->scenePos()); } m_dragged = false; } QGraphicsRectItem::mouseReleaseEvent(event); } void HighlightRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { //painter->; painter->setPen(m_pen); //painter->begin(&boximg); painter->drawRect(rect()); //painter->end(); }
I Just use QPainter for draw the outline .... but I'm not able re-implement for show QImage ...
Regards
Giorgio -
Whit the help of some link I'm able to create a subcalss QGraphicsRectItem for responds to some mouse event into GraphicsScene .... It is Right ....
But now I'm not see the way to show into these new QGraphicsRectItem a Qpixmap or better QImage (as static var so better is QImage ....)my .h code
#ifndef HIGHLIGHTRECTITEM_H #define HIGHLIGHTRECTITEM_H #include <QGraphicsRectItem> #include <QPen> #include <QLabel> #include <QGraphicsScene> #include <QGraphicsView> #include <QGraphicsItem> #include <QGraphicsRectItem> #include <QGraphicsPolygonItem> #include <QDesktopWidget> #include <QDialog> #include <QWidget> #include <QWidgetAction> #include <QWidgetData> #include <QWidgetItem> #include <QPixmap> #include <QtWidgets/qstyle.h> #include <QGraphicsRectItem> #include <QGraphicsSceneMouseEvent> class HighlightRectItem : public QGraphicsRectItem { public: static QImage boximg; HighlightRectItem(QGraphicsItem* parent = 0); bool selectRectItem; void setAnchorPoint(const QPointF& anchorPoint); public slots: protected: void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); protected slots: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); private: QPen m_pen; QPointF anchorPoint; bool m_dragged; signals: }; #endif // HIGHLIGHTRECTITEM_H
and my .cpp code ...
#include "highlightrectitem.h" #include <QPainter> HighlightRectItem::HighlightRectItem(QGraphicsItem *parent) : QGraphicsRectItem(parent), m_dragged(false) { setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemSendsGeometryChanges); setAcceptHoverEvents(true); selectRectItem = false; } QImage HighlightRectItem::boximg("/home/niceimage-qt.png"); void HighlightRectItem::setAnchorPoint(const QPointF &anchorPoint) { this->anchorPoint = anchorPoint; } void HighlightRectItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { m_pen.setColor(Qt::red); update(); } void HighlightRectItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { m_pen.setColor(Qt::green); update(); } void HighlightRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { if(selectRectItem) { selectRectItem = false; } else { selectRectItem = true; } } void HighlightRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) { m_dragged = true; QGraphicsRectItem::mouseMoveEvent(event); } void HighlightRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { if(m_dragged){ QList<QGraphicsItem*> colItems = collidingItems(); if(colItems.isEmpty()) this->setPos(anchorPoint); else { QGraphicsItem* closestItem = colItems.at(0); qreal shortestDist = 100000; foreach(QGraphicsItem* item, colItems){ QLineF line(item->sceneBoundingRect().center(), this->sceneBoundingRect().center()); if(line.length() < shortestDist){ shortestDist = line.length(); closestItem = item; } } this->setPos(closestItem->scenePos()); } m_dragged = false; } QGraphicsRectItem::mouseReleaseEvent(event); } void HighlightRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { //painter->; painter->setPen(m_pen); //painter->begin(&boximg); painter->drawRect(rect()); //painter->end(); }
I Just use QPainter for draw the outline .... but I'm not able re-implement for show QImage ...
Regards
Giorgio