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.
#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();
}
#include <QApplication>
#include <QtCore>
#include <QtGui>
class MyItem : public QGraphicsPixmapItem
{
public:
MyItem(QGraphicsView *v): view(v)
{
circle = new QGraphicsPathItem(this);
circle->setFlag(QGraphicsItem::ItemIgnoresTransformations);
circle->hide();
}
protected:
virtual void mousePressEvent(QGraphicsSceneMouseEvent * event)
{
QPointF pos_view = view->mapFromScene(mapToScene(event->pos()));
QPainterPath path;
const int Radius = 100;
path.addEllipse(pos_view, Radius, Radius);
circle->setPath(path);
circle->show();
update();
}
private:
QGraphicsView *view;
QGraphicsPathItem *circle;
};
class MainWindow : public QMainWindow
{
public:
MainWindow()
{
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
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:
virtual void resizeEvent(QResizeEvent *event)
{
view.fitInView(item, Qt::KeepAspectRatio);
}
private:
QGraphicsView view;
QGraphicsScene scene;
MyItem *item;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mw;
mw.show();
mw.resize(500, 700);
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Could someone tell me what is wrong with this?
Bookmarks