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. Need to help to select the QGraphicsPixmapItem when click on it.
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 7.9k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    Himanshu Rohilla
    wrote on last edited by
    #1

    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);
    

    }@

    HImanshu Rohilla

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Himanshu Rohilla
      wrote on last edited by
      #2

      Thanks in advance ...... :)

      HImanshu Rohilla

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        it should be enough to call this on your QGraphicsPixmapItem:
        @item->setFlag(QGraphicsItem::ItemIsSelectable);@

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Himanshu Rohilla
          wrote on last edited by
          #4

          But, I also want to draw rectangle around the image when i click on or select it ...

          HImanshu Rohilla

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Himanshu Rohilla
            wrote on last edited by
            #5

            I am attaching the URL of images of my desire result.

            The following link is just my normal image on view.

            http://www.image-upload.net/di/28SA/33.png

            The following link will show my desirable result.

            http://www.image-upload.net/di/QQVM/44.png

            HImanshu Rohilla

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              then also implement the paint method:
              @
              void MyGraphicsPixmapItem::paint(QPainter painter, const QStyleOptionGraphicsItem option, QWidget *widget)
              {
              Q_UNUSED(widget);

              painter->setRenderHint(QPainter::SmoothPixmapTransform, (d->transformationMode == Qt::SmoothTransformation));
              
              painter->drawPixmap(this->offset(), this->pixmap());
              
              if (option->state & QStyle::State_Selected)
              {
                  //draw your selection frame
              }
              

              }
              @

              But since it seems that you also want interaction with the graphicsitem it would be better to introduce a subclassed QGraphicsItemGroup and do your painting and interaction there. Because you would also have to update the bounding rect of the pixmap item once it is selected, etc. ...

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • H Offline
                H Offline
                Himanshu Rohilla
                wrote on last edited by
                #7

                Ok....

                Can i do same task for QGraphicsPathItem or whatever i draw on View using this method ?

                HImanshu Rohilla

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  as long as you stay generic to QGraphicsItem ... why not.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    Himanshu Rohilla
                    wrote on last edited by
                    #9

                    paint method is not calling ..... :(

                    HImanshu Rohilla

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      Himanshu Rohilla
                      wrote on last edited by
                      #10

                      sry.... i forgot to addItem in the scene. :P

                      HImanshu Rohilla

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        Himanshu Rohilla
                        wrote on last edited by
                        #11

                        But when i click on or select the image, it does not show its Selected State(i.e. QStyle::State_Selected) .

                        HImanshu Rohilla

                        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