PDA

View Full Version : Position in a QPixMap created from a QGraphicsScene



stevel
8th April 2009, 01:34
I am creating a QPixMap from a QGraphicsScene. I then use the QPixMap in a QGraphicsPixMapItem in another QGraphicsScene. Here's the code used to create the QPixMap:



//
// JSL - CreateQPixmap
//
QPixmap EDrawListView::createQPixmap()
{
unselectAll();

QRectF sourceRect = scene()->itemsBoundingRect();
sourceRect.setX(sourceRect.x()-1);
sourceRect.setY(sourceRect.y()-1);
QRectF targetRect(0, 0, sourceRect.width()+2, sourceRect.height()+2);
QPixmap thePixmap(sourceRect.width()+2, sourceRect.height()+2);
thePixmap.fill(Qt::transparent);
QPainter p(&thePixmap);
scene()->render(&p, targetRect, sourceRect);
p.end();
return(thePixmap);
}


The problem is that once the scene has been turned into a QPixMap, I still need to know where a certain item on that scene was in coordinates that relate to the QPixMap, not to the original Scene before the QPixmap was created. If I look at the pos value of the item as it was on the scene, it doesn't relate to the position of the image of the item on the QPixMap. (Because of the itemsBoundingRect code in the call above, I imagine.)
Now that I am writing this, I imagine that saving the itemsboundingRect might be the clue. If I save this rect, then I can use it to offset the pos of the item to find it's location on the QPixMap.

If anyone has any thoughts or suggestions, I would appreciate hearing them.

Thanks.

wysota
8th April 2009, 09:06
What are you doing with the pixmap afterwards and what is the final effect you want to obtain?

stevel
8th April 2009, 18:54
Wysota,

Thanks for your reply. The QPixMap is embedded in a QGraphicsPixMapItem, and inserted in a QGraphicsScene. The final effect I have to have is to make it so when I hover over part of this QGraphicsPixMapItem, I can tell over which of the QGraphicsItems in the QGraphicsScene used to make the QGraphicsPixMap I am hovering.

(Basically it's a QGraphicsScene within a QGraphicsScene, but I am taking a QPixMap of the smaller scene.)

The final effect is you have a QPixMap on the scene, you double click it, it opens a window containing the contents of the QPixMap, you edit it, close it, and it recreates the QPixMap based on the scene in the opened window.

As I said, the problem now is hovering over part of the QPixMap, and identifying which part of the 'subscene' I am over. The scene does not still exist as a scene, as it is constructed and destroyed when it's window is opened and closed, and the pos coordinates are wrong, as described in my first post.

aamer4yu
8th April 2009, 20:02
I dont think using 2 scenes would be good for your requirements.
What I understand is, you need to show a small map of the whole area, like in games etc.

What you need is 2 views of the same scene. If you look at the 40000 chips demo in Qt Demo, you will find there are 4 views of the same scene. Zoom out completely in one view. Now from other views, if you move items, it will be reflected in the zoomed out view.
Similarly you can keep one view as the main, make the second view small size and fit the whole scene in it. and you will get kinda small map ..

Hope I understood your requirement correctly :)

stevel
8th April 2009, 20:17
@aamer4yu

Thanks for the reply. I don't think that you did understand my requirement correctly. I already have the two scene stuff working quite well, and it is exactly what I need. The intent of the smaller scene is just to be a way to edit the QPixMap on the QGraphicsPixMapItem on the larger scene. There is no relationship between what is shown on the QPixMap on the QGraphicsPixMapItem and the larger scene. It is not a view of the larger scene. I have all this working quite well, you double click on the QPixMapGraphicsItem, and it opens a window where you edit the contents of the picture in a QGraphicsScene, then you close it, and it recreates the QPixMap. The problem is the coordinate system, and finding the location on the QPixMap of one of those items from the smaller scene.

wysota
8th April 2009, 22:07
Thanks for the reply. I don't think that you did understand my requirement correctly. I already have the two scene stuff working quite well, and it is exactly what I need.
I'm not sure of that. I'd go for a single scene as well.


The intent of the smaller scene is just to be a way to edit the QPixMap on the QGraphicsPixMapItem on the larger scene.
According to me the "pixmap" should never have been a pixmap. It should be a composition of items, espcially if you want to treat it as something composed from items.

A pixmap is a bitmap, it's composed from pixels not from objects. If your picture is to be a composition of objects then treat it as so.

stevel
8th April 2009, 22:39
No, you guys are not seeing the bigger picture. The way I am doing this makes sense, and really fits my project. Does anyone have a suggestion about my question?

I will try again to explain what I am doing, which as I said, is working well, and doing exactly what I want.

On the main QGraphicsScene, there are many types of Items. One of them is something called a block, which has an icon. The icon is represented by a QGraphicsPixMap. When you double click on a block, you can open up a window which allows you to edit it's icon. This window is also a QGraphicsScene. I cannot, and do not want to represent this as anything other then a QGraphicsPixMapItem, as there can potentially be thousands of blocks on the main window.

For the most part, I don't care about the details of the Scene that was used to build the block, however, there are a couple of items on that scene that I do care about, and I need to save information about where those items are on the QPixMap when it is created. All I want to know is how to get the location (pos) of those items in the coordinates of the QPixMap, not the coordinates of the scene that made the QPixMap. Here is my latest try, which is not working yet.

This code is in the GrapphicsItem whose position I care about, and I am trying to create a position point that is offset by the location of the created QPixMap, but I don't have the logic right obviously.



QPointF point1, point2;

// JSL - Calculate the location of the position of the item on the icon
point1 = mapToScene(pos());
point2 = scene()->itemsBoundingRect().center();
drawData.iconPos = point2 - point1;

wysota
8th April 2009, 22:52
Make the coordinates of the "pixmap scene" reflect those of the scene containing the pixmap. The easiest way to do it is to use QGraphicsItem::sceneBoundingRect(). Then pass the returned rect to QGraphicsScene::setSceneRect() of the newly created scene.

Then all your calculations will be done in the same coordinate space.

stevel
8th April 2009, 23:39
wysota,

Thanks for your help. I don't think I have fully managed to communicate what I am doing, because part of what you were saying really didn't apply, but your mention of the sceneBoundingRect function helped, because this code:



QRectF rect;
QPointF point1, point2;

// JSL - Calculate the location of the position of the item on the icon
rect = sceneBoundingRect();
point1 = rect.topLeft();
point2 = scene()->itemsBoundingRect().topLeft();
drawData.iconPos= point1 - point2;


seems to be doing the trick. Thanks again. I think I can move on from here.