不是Image直接来自.QDeclarativeImage有pixmap,setPixmap而且pixmapChange方法,但由于某些原因没有申报财产.所以你不能使用它来获取qml.不幸的是它不能用于C++ - 它是一个私人的calsss.
你可以做的是将图形项绘制到pixmap并将其保存到文件中.
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");
}
Run Code Online (Sandbox Code Playgroud)
注册"capturer"上下文变量:
int main()
{
// ...
Capturer capturer;
QmlApplicationViewer viewer;
viewer.rootContext()->setContextProperty("capturer", &capturer);
// ...
}
Run Code Online (Sandbox Code Playgroud)
并在你的qml中使用它:
Rectangle {
// ...
Image {
id: img
source: "/path/to/your/source"
}
MouseArea {
anchors.fill: parent
onClicked: {
capturer.save(img)
}
}
}
Run Code Online (Sandbox Code Playgroud)