PDA

View Full Version : add QImage to QGraphisScene



myrabbitrules
17th February 2011, 00:38
Hi!

I have my own subclass of QGraphicsView which displays a QGraphicsScene. I add lines on the scene. Now I also want do add QImages to the scene (or view).
I have tried the following:
I overode QWidget:: paintEvent(QPaintEvent*) in my QGraphicsView subclass:

void myViewSubclass:paintEvent(QPaintEvent * event) {
QGraphicsView::paintEvent(event);
QPainter painter;
painter.begin(this);
QPointF point(GetCenter().x(), GetCenter().y());
painter.drawImage(point, *image);
painter.end();
}
Unfortunately Qt Creator tells me:
QPainter::begin: Widget painting can only begin as a result of a paintEvent
But I only paint in the above method which IS the result of a paint Event...
Where is my mistake? Or is my aprroach to drawing an image on the scene rubbish anyway?

stampede
17th February 2011, 09:17
Why not using the QGraphicsScene::addPixmap() (http://doc.qt.nokia.com/latest/qgraphicsscene.html#addPixmap) method ? It inserts image on the scene and returns an object which you can use to manipulate the image later.

myrabbitrules
17th February 2011, 12:17
Your idea seems to be very good for me. However I have problems adding the pixmap to the GraphicScene's origin, which is supposed to be in the scene's upper left border.
I tried the following:

QGraphicsScene* scene = new QGraphicsScene(0, 0, 500, 500);
QGraphicsView* view = new QGraphicsView;
view->setScene(scene);
QPixmap pixmap = QPixmap::fromImage(image);
QGraphicsPixmapItem* pixmapItem = scene->addPixmap(pixmap);
pixmapItem->setPos(0, 0);
however my image now appears somewhere (but not quite!) in the middle of my view.
Why doesn't it appear in the upper left corner? SetPos is supposed to work in parent's coordinates and when i print the scenePos() coordinates of the PixmapItem I get (0, 0)... But it's not there...

Does anyone have a clue?

SixDegrees
17th February 2011, 13:02
Read ALL of the documentation on the GraphicsView/GraphicsScene/GraphicsItem framework, including the portions on the different, independent coordinate systems used by each object.

http://doc.qt.nokia.com/4.7/graphicsview.html

myrabbitrules
17th February 2011, 15:48
I already read the most inportant sections. But, ok I read it all again.
It says:
An item's position is the coordinate of the item's center point in its parent's coordinate system. The scene is ... regarded as all parent-less items' "parent".
I figured out that my QGraphicsPixmapItem is parent-less. Hence its position is its center point in my GraphicScene's coordinate system. As the item's pos() returns (0, 0) I know that the item is in the right place in my scene. The only possible problem could be, that it is not in the (0, 0) of my GraphicsView. I checked this using the QGraphicsView::mapFromScene() function. And indeed this function returned (x, 0) where x depends on my scene's height and width - even though I wanted x to be always 0.
I don't understand, why a scene QGraphicsScene(0, 0, 500, 500) which I add to my view, is not located at the view's origin (0, 0).
I guess this is the core of my problem. Maybe you can help me figure out, what I could do.