Drawing a fixed-sized shape at mouse press position on QGraphicsView
Hi, I am trying to write an application to manipulate images using the GraphicsView framework.
I derived QGraphicsPixmapItem to display an image and reimplemented mousePressEvent() to draw a circle at the mouse press position on the image.
I want the image to automatically fit to the window when the view is resized while keeping the circle size fixed.
So I set the ItemIgnoresTransformations flag in order not to resize the circle, and converted the mouse press position to the view coordinate before setting it as the circle position.
But the position of the drawn circle is deviated from the mouse press position.
The following is the simplified code of mine.
Code:
#include <QApplication>
#include <QtCore>
#include <QtGui>
{
public:
{
circle->hide();
}
protected:
{
QPointF pos_view
= view
->mapFromScene
(mapToScene
(event
->pos
()));
const int Radius = 100;
path.addEllipse(pos_view, Radius, Radius);
circle->setPath(path);
circle->show();
update();
}
private:
};
{
public:
MainWindow()
{
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setCentralWidget(&view);
setWindowTitle(tr("Image Viewer"));
item = new MyItem(&view);
item
->setPixmap
(QPixmap("test.tif"));
item->setTransformationMode(Qt::SmoothTransformation);
scene.addItem(item);
view.setScene(&scene);
}
protected:
{
view.fitInView(item, Qt::KeepAspectRatio);
}
private:
MyItem *item;
};
int main(int argc, char *argv[])
{
MainWindow mw;
mw.show();
mw.resize(500, 700);
return app.exec();
}
Could someone tell me what is wrong with this?
Re: Drawing a fixed-sized shape at mouse press position on QGraphicsView
Why do you map to and back from scene coordinates?
You are taking your item coordinates, map them to scene coords, and than map that to view coords, and you give these coords to the item to draw!
The position you get from the event is in item coordinates, and you are in an item, so just use the position you get from the event.
Re: Drawing a fixed-sized shape at mouse press position on QGraphicsView
Hi, thank you very much for reply.
Quote:
Originally Posted by
high_flyer
You are taking your item coordinates, map them to scene coords, and than map that to view coords, and you give these coords to the item to draw!
The position you get from the event is in item coordinates, and you are in an item, so just use the position you get from the event.
But if I just use the position given from the event, the item is not drawn at the mouse press position because I set QGraphicsItem::ItemIgnoresTransformations flag of the item to prevent it from resizing with window...
Is there any better way to keep the item size to fixed number of pixels?
Re: Drawing a fixed-sized shape at mouse press position on QGraphicsView
Quote:
the item is not drawn at the mouse press position because I set QGraphicsItem::ItemIgnoresTransformations flag
I don't see why this should matter?
The flag means that the item doesn't scale.
But its position stays the same in the parent coordinates, and the item coordinates remain the item coordinates.
Re: Drawing a fixed-sized shape at mouse press position on QGraphicsView
Thank you again for your reply.
Quote:
Originally Posted by
high_flyer
I don't see why this should matter?
The flag means that the item doesn't scale.
But its position stays the same in the parent coordinates, and the item coordinates remain the item coordinates.
The item is drawn at the mouse press position if I don't set the QGraphicsItem::ItemIgnoresTransformations, but NOT if the flag is set...
I would appreciate it if you could confirm this using my code above with any image named test.tif.
Re: Drawing a fixed-sized shape at mouse press position on QGraphicsView
I've found a kind of solution.
If I make the item a direct child of the scene, it is drawn at the mouse press position.
Do I need to define the transformation matrix by myself to make the item a child of another item?
Seems tricky...