3 Attachment(s)
How to show items in the same position saved in QGraphicsView/Scene ?
Hi to all,
I'm having trouble showing the image saved in a QGraphicsScene. I need to save multiple image items in QGraphicsView and reload them to do something. My solution is to save each item as an independent image file and write down each scene position into a record for loading. In my process, if the GraphicsView has the same size in saving/loading, image files can be loaded and shown in the same position as they saved. But if I use a larger/smaller window to load images, the images are not located in the same position (corresponding to the top-left corner of the view).
My saving process
Code:
..
tag
= QString("%1/%2.png").
arg(path
).
arg(i
);
// filenameIt->pixmap (). save (tag); // save image item
ts << "\t\t" << it->x() << " " << it->y() << " " << tag << "\n"; // save the scene position and the file name
My loading process
Code:
..
ts >> x >> y >> tag;
QImage img
(tag
);
// read the image by filename item->setPos(x, y);
It's sorry that I have no idea about it. Thank you.
The image file saved in a small window.
Attachment 9783
After loading, the image seems moved
Attachment 9784
What I want .. located at fixed position to the top-left corner of GraphicsView
Attachment 9785
Re: How to show items in the same position saved in QGraphicsView/Scene ?
One solution is to save relative position coordinates rather than absolute values.
What I mean is, for example, lets say we have an image of size = (100,100) placed on pos = (50,50) on a scene with scene size = (200,200).
If you save only absolute values of image size and position, you'll have a problem if the scene size changes after loading.
Instead, you can save image parameters like size = (0.5,0.5) and pos = (0.25,0.25) (or 50% and 25%, it means - how much of the whole scene size is occupied by this image, 0.5 = 100/200, 0.25 = 50/200...). Now if you load it on scene with scene size = (400,400), you simply rescale the values :
new_size = (0.5*400, 0.5*400)=(200,200), new_pos = (0.25*400,0.25*400)=(100,100). Simple, isn't it ? :)
Of course if scenes aspect ratio is changed, your image will change as well, but it's up to you how you want to deal with it.
Another solution is to save scene size as well and rescale everything after loading.
I won't give you working code as I think it's better if you work it out yourself.
Re: How to show items in the same position saved in QGraphicsView/Scene ?
Sorry Stampede, I did not explain my request clearly and you may get wrong with me. The last figure explain what I want. I dont want to enlarge the image. I hope that they can keep the same size and the position offset to the left-top corder of me
Re: How to show items in the same position saved in QGraphicsView/Scene ?
Quote:
keep the same size and the position offset to the left-top corder
Define what "same" means in term "same size and offset".
Suppose we have a scene, size = (100,100) and image placed at (10,10). Now you load it on new scene, which has size=(200,200). "Same" means to you
1) offset should still be (10,10)
or
2) offset should be (20,20) = 10% of scenes dimension as previously
or
3) <some other definition>
which one is it ?
case 1) - save absolute values of x,y and set it via setPos(x,y) after load
case 2) - use algorithm I described above only for position component
case 3) - (?)
Re: How to show items in the same position saved in QGraphicsView/Scene ?
My problem is ...
I have some early sketch data recorded in view coordinate but the image objects are saved in scene coordinate. So when I open the file in different window size, these images seems not located on the same position vs. the left-top corner of the window.
The leader said that I can fix the view resolution, so I can save/load images both in view coordinate and additem() in scene coordinate. It works...
Sorry Stampede deeply that you spend time for my question but I did not understand well my request and my problem clearly. Sorry sorry sorry ...
Re: How to show items in the same position saved in QGraphicsView/Scene ?
Ok, no problem, good that it works now :)