Saving a QML Image

How to save a QML image in the phone?

also, if saving the image was applicable, I have this case that I need to add to the image (we can imagine it because we have a transparent image [that holds the text] and puts it on top of the second image, so finally, we have there is one image that we can save in the phoneโ€™s memory)

+6
source share
2 answers

Not from Image directly. QDeclarativeImage has pixmap , setPixmap and pixmapChange , but for some reason the property is not declared. Therefore you cannot use its fom qml. Unfortunately, it also cannot be used with C ++ - these are private calsss.

What you can do is draw an image element in your pixmap and save it to a 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 the context variable "capturer":

 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) } } } 
+7
source

With Qt 5.4+, you can do this directly from your Qml with: grabToImage

+3
source

Source: https://habr.com/ru/post/917946/


All Articles