Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsRectItem that shows an image QPixmap or QImage....
Forum Update on Monday, May 27th 2025

QGraphicsRectItem that shows an image QPixmap or QImage....

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by
    #1

    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

    bkt

    gfxxG 1 Reply Last reply
    0
    • gfxxG gfxx

      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

      gfxxG Offline
      gfxxG Offline
      gfxx
      wrote on last edited by
      #2

      @gfxx Sorry stupid question ....

      painter->drawImage(QPoint(0,0), boximg);
      

      solve the problem .....
      unfortunately I always read too little references of Qt online classes .... :(

      regards
      Giorgio

      bkt

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved