Quantcast
Channel: Installation and Deployment
Viewing all articles
Browse latest Browse all 22112

Need to help to select the QGraphicsPixmapItem when click on it.

$
0
0
Hello everyone, I want to draw image on QGraphicsView. For this, I created a sub class of QGraphicsPixmapItem. When i click on QGraphicsView, it shows a QFileDialog to select image from the system and displaying successfully on view. Now, I want to select this image when i click on this. For this, I write the code but it does not work. The following is the my code. /*  This is my HEADER File which is subclass of QGraphicsPixmapItem   */   #include <QGraphicsPixmapItem> #include <QDebug>   class WhiteBoardItems : public QGraphicsPixmapItem { public:     WhiteBoardItems(QWidget *parent = 0);       virtual ~WhiteBoardItems();       virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);     virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);     virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);   }; This is Header File of subclass of QGraphicsView. #include "WhiteBoardItems.h"   class WhiteBoardItems;   class WhiteBoardView : public QGraphicsView {     Q_OBJECT   public:     WhiteBoardView(QWidget* pParent = 0);     virtual ~WhiteBoardView();   protected:       void mousePressEvent(QMouseEvent *event);     void mouseMoveEvent(QMouseEvent *event);     void mouseReleaseEvent(QMouseEvent *event);   private:       void AddImgToview(QString fileName);       bool vbMousePressed, vbImageSelected;       QString vstrImgFileName;       WhiteBoardItems* mImageItem;   }; /*  This is CPP File of the subclass */   #include "WhiteBoardItems.h"   WhiteBoardItems::WhiteBoardItems(QWidget *parent) :     QGraphicsPixmapItem() {   }   WhiteBoardItems::~WhiteBoardItems() {   }   void WhiteBoardItems::mousePressEvent(QGraphicsSceneMouseEvent *event) {     qDebug() << "Mouse is clicked in QGraphicsItem";   }   void WhiteBoardItems::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {   }   void WhiteBoardItems::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {   } This is CPP File of subclass of QGraphicsView . void WhiteBoardView::mousePressEvent (QMouseEvent *event) {     if(event->button() == Qt::LeftButton)     {         vbMousePressed = true;                 if(vbImageSelected)         {             previousPt = event->pos();         }     } }   void WhiteBoardView::mouseReleaseEvent (QMouseEvent *event) {         if(vbImageSelected)         {             vstrImgFileName = QFileDialog::getOpenFileName(this,"OPEN FILE", QDir::currentPath(),tr("Image Files (*.png *.jpg *.bmp *.jpeg *.gif)"));                         if (QImage(vstrImgFileName).isNull() && !vstrImgFileName.isEmpty())             {                    QMessageBox::information(this,"White Board","There is some problem with image file. This file can't be loaded.");                      return;             }               AddImgToview(vstrImgFileName);           }                 vbMousePressed = false; }   void WhiteBoardView::AddImgToview(QString fileName) {     mImageItem = new WhiteBoardItems();       mImageItem = (WhiteBoardItems*)(this->scene()->addPixmap(QPixmap(fileName)));     mImageItem->setFlag(QGraphicsItem::ItemIsMovable);     mImageItem->setFlag(QGraphicsItem::ItemIsSelectable);       mImageItem->setX(previousPt.x());     mImageItem->setY(previousPt.y());       mImageItem->setScale(0.5); }

Viewing all articles
Browse latest Browse all 22112


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>