直接上代码
注:C++11写法,需要编译器支持C++11标准才可以正常编译
QImage itemToImage(QObject *object)
{
auto item = qobject_cast<QQuickItem*>(object);
if(!item) { return QImage(); }
QEventLoop eventLoop;
QImage buf;
auto grad = item->grabToImage();
auto c = QObject::connect(grad.data(), &QQuickItemGrabResult::ready, [&]()
{
buf = grad->image();
eventLoop.exit();
});
eventLoop.exec();
QObject::disconnect(c);
return buf;
}