chiaminhsu
12th November 2013, 09:52
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
QTextStream ts(&file);
..
QGraphicsPixmapItem* it = qgraphicsitem_cast<QGraphicsPixmapItem*>(scene()->items().at(i));
tag = QString("%1/%2.png").arg(path).arg(i); // filename
It->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
QTextStream ts(&file);
..
ts >> x >> y >> tag;
QImage img(tag); // read the image by filename
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixmap, 0, scene());
item->setPos(x, y);
It's sorry that I have no idea about it. Thank you.
The image file saved in a small window.
9783
After loading, the image seems moved
9784
What I want .. located at fixed position to the top-left corner of GraphicsView
9785
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
QTextStream ts(&file);
..
QGraphicsPixmapItem* it = qgraphicsitem_cast<QGraphicsPixmapItem*>(scene()->items().at(i));
tag = QString("%1/%2.png").arg(path).arg(i); // filename
It->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
QTextStream ts(&file);
..
ts >> x >> y >> tag;
QImage img(tag); // read the image by filename
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixmap, 0, scene());
item->setPos(x, y);
It's sorry that I have no idea about it. Thank you.
The image file saved in a small window.
9783
After loading, the image seems moved
9784
What I want .. located at fixed position to the top-left corner of GraphicsView
9785