Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving QML image

Tags:

qt

qml

How can I save QML Image into phone memory ??

also if saving the image was applicable , I have this case which I need to add some text to the image (we can imagine it as we have a transparent image[that hold the text] and put it over the second image , so finally we have one image that we can save it into phone memory)

like image 440
Bayan Avatar asked Jun 13 '12 08:06

Bayan


2 Answers

Not from Image directly. QDeclarativeImage has pixmap, setPixmap and pixmapChange methods, but for some reason there is no property declared. So you cannot use it fom qml. Unfortunately it cannot be used from C++ either - it is a private calsss.

What you can do is paint graphics item to your pixmap and save it to file.

class Capturer : public QObject
{
    Q_OBJECT
public:
    explicit Capturer(QObject *parent = 0);
    Q_INVOKABLE void save(QDeclarativeItem *obj);
};

void Capturer::save(QDeclarativeItem *item)
{
    QPixmap pix(item->width(), item->height());
    QPainter painter(&pix);
    QStyleOptionGraphicsItem option;
    item->paint(&painter, &option, NULL);
    pix.save("/path/to/output.png");
}

Register "capturer" context variable:

int main()
{
    // ...
    Capturer capturer;
    QmlApplicationViewer viewer;
    viewer.rootContext()->setContextProperty("capturer", &capturer);
    // ...
}

And use it in your qml:

Rectangle {
    // ...
    Image {
        id: img
        source: "/path/to/your/source"
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            capturer.save(img)
        }
    }
}
like image 196
sergk Avatar answered Sep 20 '22 17:09

sergk


With Qt 5.4+ you can do it straight from your Qml with: grabToImage

like image 21
Larpon Avatar answered Sep 23 '22 17:09

Larpon