How to save an image from QML Image element?
Hi everyone. Is there a way to save cached image from Image element?
Thanks in advance.
8 replies
I found this in the doc(QDeclarativeEngine::addImageProvider):
Note that images loaded from a QDeclarativeImageProvider are cached by QPixmapCache, similar to any image loaded by QML.
But how I can get an image from QPixmapCache? It requires Key (in the find function). I tried to use URL as Key, but it failed.
Something like this:
- {
- Q_OBJECT
- public:
- public slots:
- };
- {
- QGraphicsObject *item = qobject_cast<QGraphicsObject*>(imageObj);
- if (!item) {
- qDebug() << "Item is NULL";
- return;
- }
- QStyleOptionGraphicsItem styleOption;
- item->paint(&painter, &styleOption);
- img.save(path);
- }
And then in QML:
- import Qt 4.7
- Rectangle {
- width: 640
- height: 480
- color: "white"
- Image {
- id: imgItem
- fillMode: Image.PreserveAspectFit
- source: "test.jpg"
- }
- MouseArea {
- anchors.fill: parent
- onClicked: {
- imageSaver.save(imgItem, "/home/meego/saved-test.jpg");
- }
- }
- }
with imageSaver is an instance of ImageSaver and set as context property.
Cheers
Phi
Hi
I’m have a problem with this code. I can’t get it to work… When I try to paint the QGraphicsObject onto the QImage, I end up with the color (white in this case) that was filled in the image before the paint job. I’m using the code just as shown above. I would therefore guess that the paint method does not work. Is there any difference between doing this on the Desktop platform or the Symbian platform? Is there anything else that could make my situation different?
- QGraphicsObject *item = qobject_cast<QGraphicsObject*>(imageObj);
- if (!item) {
- qDebug() << "Item is NULL";
- return false;
- }
- QStyleOptionGraphicsItem styleOption;
- item->paint(&painter, &styleOption);
- // Then I add the image to a database, but that should be irrelevant
I really, really need some help! …I’m drawing a complete blank on this one (pun intended).
Yay. I figured it out!
The problem was that I was calling this save method when Image.onProgressChanged() fired and Image.progress 1. This proved however to be before the Image was actually drawn onto the screen. The solution was to call the method when Image.onPaintedGeometryChanged() fired and Image.progress 1.
You must log in to post a reply. Not a member yet? Register here!


