Need to help to select the QGraphicsPixmapItem when click on it.
-
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_OBJECTpublic:
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);
}@
-
Thanks in advance ...... :)
-
it should be enough to call this on your QGraphicsPixmapItem:
@item->setFlag(QGraphicsItem::ItemIsSelectable);@ -
But, I also want to draw rectangle around the image when i click on or select it ...
-
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.
-
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. ...
-
Ok....
Can i do same task for QGraphicsPathItem or whatever i draw on View using this method ?
-
as long as you stay generic to QGraphicsItem ... why not.
-
paint method is not calling ..... :(
-
sry.... i forgot to addItem in the scene. :P
-
But when i click on or select the image, it does not show its Selected State(i.e. QStyle::State_Selected) .