保存 QML 图像 [英] saving QML image
问题描述
如何将 QML Image 保存到手机内存中??
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)
推荐答案
不是直接来自Image
.QDeclarativeImage
有 pixmap
、setPixmap
和 pixmapChange
方法,但由于某种原因没有声明属性.所以你不能在 qml 中使用它.不幸的是,它也不能在 C++ 中使用——它是一个私有类.
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);
// ...
}
并在你的 qml 中使用它:
And use it in your qml:
Rectangle {
// ...
Image {
id: img
source: "/path/to/your/source"
}
MouseArea {
anchors.fill: parent
onClicked: {
capturer.save(img)
}
}
}
这篇关于保存 QML 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!