PDA

View Full Version : Identify QImage



mythili
24th May 2013, 09:40
Hi,
I am creating mutliple QImages in a reSizeEvent which is same as Scribble Example. In Scribble example, they create only one QImage.
On MousePress, how to identify which QImage it is? Is it possible to assign any object name while creating like setObjectName. Please help me.
Thank you.

Santosh Reddy
24th May 2013, 11:56
how to identify which QImage it is?
Can't you use QImage variable itself to identify it.

mythili
24th May 2013, 12:00
My QImage variable is same for all images.
Can i get which image it is on mousepress??
Thank you

Santosh Reddy
24th May 2013, 12:17
My QImage variable is same for all images.
Can i get which image it is on mousepress??
Well you can know where the mouse was clicked on the widget, and based on this you need to back-calculate the image which was drawn at that position/region.

If you need to handle mouse click for individual images, then QWidget's paint() function not a good way, rather you should be using QGraphicsView framework.


BTW

I am creating mutliple QImages in a reSizeEvent which is same as Scribble Example
Scribble example does not create multiple QImages in resizeEvent().

mythili
24th May 2013, 12:35
I am able to get the object name of widget. But how to get the image details.

Santosh Reddy
24th May 2013, 12:45
You cannot get the image name (like widget). You will need to implement a tracking mecahnism where the image was drawn, and with what dimention and back-calculate from the mouse click position. There are no direct Qt classes / functions to do so.

In simple words it is possble but is not stright forward and may end by being complex logic. It is better to use QGraphcisView framework to keep things simple.

mythili
24th May 2013, 12:48
can u give me any sample using QGraaphicsView framework.

Thank you

Santosh Reddy
24th May 2013, 13:25
With the available information this the example I can give quickly.



#include <QtGui>
#include <QtWidgets> // Comment this if using Qt4
#include <QApplication>

class GraphicsPixmapItem : public QGraphicsPixmapItem
{
public:
explicit GraphicsPixmapItem(const QPixmap & pixmap, QGraphicsItem * parent = 0)
: QGraphicsPixmapItem(pixmap, parent)
, mName("GraphicsPixmapItem") { }

enum { Type = UserType + 1 };

int type(void) const { return Type; }

QString name(void) const { return mName; }

void setName(const QString & name) { mName = name; }

protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawRect(option->rect);
return QGraphicsPixmapItem::paint(painter, option, widget);
}

private:
QString mName;
};

class GraphicsScene : public QGraphicsScene
{
public:
explicit GraphicsScene(QObject * parent = 0)
: QGraphicsScene(parent)
, mGraphicsTextItem(addText("No Selection")) { }
protected:
void mousePressEvent(QGraphicsSceneMouseEvent * event)
{
GraphicsPixmapItem * item = qgraphicsitem_cast<GraphicsPixmapItem *>(itemAt(event->scenePos(), QTransform()));

if(item)
mGraphicsTextItem->setPlainText(item->name());
else
mGraphicsTextItem->setPlainText("Click on GraphicsPixmapItem");
}

private:
QGraphicsTextItem * mGraphicsTextItem;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QGraphicsView graphicsView;
graphicsView.showMaximized();

GraphicsScene graphicsScene;
graphicsView.setScene(&graphicsScene);

GraphicsPixmapItem * pix1 = new GraphicsPixmapItem(QPixmap("logo1.png"));
GraphicsPixmapItem * pix2 = new GraphicsPixmapItem(QPixmap("logo2.png"));

pix1->setPos(50,50);
pix2->setPos(200,50);

pix1->setName("Pixmap - 1");
pix2->setName("Pixmap - 2");

graphicsScene.addItem(pix1);
graphicsScene.addItem(pix2);

return app.exec();
}


As an alternate to custom GraphicsPixmapItem, you can also use QGraphicsItem::setData(int key, const QVariant & value), in fact it will be more simple to use QGraphicsItem::setData() and QGraphicsItem::data().