4#include <private/qtquickglobal_p.h>
10#if QT_CONFIG(quick_shadereffect)
11#include "qquickshadereffectsource_p.h"
14#include <QtQml/QQmlEngine>
15#include <QtQml/QQmlInfo>
17#include <private/qquickpixmap_p.h>
18#include <private/qquickitem_p.h>
19#include <private/qsgcontext_p.h>
20#include <private/qsgadaptationlayer_p.h>
22#include <QtCore/qpointer.h>
45 if (url.isEmpty() && !image.isNull()) {
46 url.setScheme(QQuickPixmap::itemGrabberScheme);
47 url.setPath(QVariant::fromValue(item.data()).toString());
48 static uint counter = 0;
49 url.setFragment(QString::number(++counter));
50 cacheEntry =
new QQuickPixmap(url, image);
54 static QQuickItemGrabResult *
create(QQuickItem *item,
const QSize &size);
73
74
75
76
77
78
79
80
81
82
83
84
87
88
89
90
91
92
93
94
95
96
99
100
101
102
103
106
107
108
109
110
111
112
115
116
117
118
119
120
123
124
125
126
129
130
131
132
133
134
135
136
137
138
139
140
142QQuickItemGrabResult::QQuickItemGrabResult(QObject *parent)
143 : QObject(*
new QQuickItemGrabResultPrivate, parent)
148
149
150
151
152
156
157
158
159
160
161bool QQuickItemGrabResult::saveToFile(
const QString &fileName)
const
163 Q_D(
const QQuickItemGrabResult);
164 if (fileName.startsWith(QLatin1String(
"file:/")))
165 return saveToFile(QUrl(fileName));
166 return d->image.save(fileName);
170
171
172
173
174
175
176bool QQuickItemGrabResult::saveToFile(
const QUrl &filePath)
const
178 Q_D(
const QQuickItemGrabResult);
179 if (!filePath.isLocalFile()) {
180 qWarning() <<
"saveToFile can only save to a file on the local filesystem";
183 return d->image.save(filePath.toLocalFile());
186#if QT_VERSION < QT_VERSION_CHECK(6
, 0
, 0
)
187#if QT_DEPRECATED_SINCE(5
, 15
)
189
190
191
192bool QQuickItemGrabResult::saveToFile(
const QString &fileName)
194 return std::as_const(*
this).saveToFile(fileName);
199QUrl QQuickItemGrabResult::url()
const
201 Q_D(
const QQuickItemGrabResult);
202 d->ensureImageInCache();
206QImage QQuickItemGrabResult::image()
const
208 Q_D(
const QQuickItemGrabResult);
213
214
215bool QQuickItemGrabResult::event(QEvent *e)
217 Q_D(QQuickItemGrabResult);
218 if (e->type() == Event_Grab_Completed) {
220 if (d->qmlEngine && d->callback.isCallable()) {
221 d->callback.call(QJSValueList() << d->qmlEngine->newQObject(
this));
228 return QObject::event(e);
231void QQuickItemGrabResult::setup()
233 Q_D(QQuickItemGrabResult);
235 disconnect(d->window.data(), &QQuickWindow::beforeSynchronizing,
this, &QQuickItemGrabResult::setup);
236 disconnect(d->window.data(), &QQuickWindow::afterRendering,
this, &QQuickItemGrabResult::render);
237 QCoreApplication::postEvent(
this,
new QEvent(Event_Grab_Completed));
241 QSGRenderContext *rc = QQuickWindowPrivate::get(d->window.data())->context;
242 d->devicePixelRatio = d->window->effectiveDevicePixelRatio();
243 d->texture = rc->sceneGraphContext()->createLayer(rc);
244 d->texture->setDevicePixelRatio(d->devicePixelRatio);
245 d->texture->setItem(QQuickItemPrivate::get(d->item)->itemNode());
246 d->itemSize = QSizeF(d->item->width(), d->item->height());
249void QQuickItemGrabResult::render()
251 Q_D(QQuickItemGrabResult);
255 d->texture->setRect(QRectF(0, d->itemSize.height(), d->itemSize.width(), -d->itemSize.height()));
256 const QSize minSize = QQuickWindowPrivate::get(d->window.data())->context->sceneGraphContext()->minimumFBOSize();
257 const QSize effectiveTextureSize = d->textureSize * d->devicePixelRatio;
258 d->texture->setSize(QSize(qMax(minSize.width(), effectiveTextureSize.width()),
259 qMax(minSize.height(), effectiveTextureSize.height())));
260 d->texture->scheduleUpdate();
261 d->texture->updateTexture();
262 d->image = d->texture->toImage();
263 d->image.setDevicePixelRatio(d->devicePixelRatio);
266 d->texture =
nullptr;
268 disconnect(d->window.data(), &QQuickWindow::beforeSynchronizing,
this, &QQuickItemGrabResult::setup);
269 disconnect(d->window.data(), &QQuickWindow::afterRendering,
this, &QQuickItemGrabResult::render);
270 QCoreApplication::postEvent(
this,
new QEvent(Event_Grab_Completed));
275 QSize size = targetSize;
277 size = QSize(item->width(), item->height());
279 if (size.width() < 1 || size.height() < 1) {
280 qmlWarning(item) <<
"grabToImage: item has invalid dimensions";
284 if (!item->window()) {
285 qmlWarning(item) <<
"grabToImage: item is not attached to a window";
289 QWindow *effectiveWindow = item->window();
290 if (QWindow *renderWindow = QQuickRenderControl::renderWindowFor(item->window()))
291 effectiveWindow = renderWindow;
293 if (!effectiveWindow->isVisible()) {
294 qmlWarning(item) <<
"grabToImage: item's window is not visible";
298 QQuickItemGrabResult *result =
new QQuickItemGrabResult();
301 d->window = item->window();
302 d->textureSize = size;
304 QQuickItemPrivate::get(item)->refFromEffectItem(
false);
307 item->window()->update();
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330QSharedPointer<QQuickItemGrabResult> QQuickItem::grabToImage(
const QSize &targetSize)
332 QQuickItemGrabResult *result = QQuickItemGrabResultPrivate::create(
this, targetSize);
334 return QSharedPointer<QQuickItemGrabResult>();
336 connect(window(), &QQuickWindow::beforeSynchronizing, result, &QQuickItemGrabResult::setup, Qt::DirectConnection);
337 connect(window(), &QQuickWindow::afterRendering, result, &QQuickItemGrabResult::render, Qt::DirectConnection);
339 return QSharedPointer<QQuickItemGrabResult>(result);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
373
374
375
376bool QQuickItem::grabToImage(
const QJSValue &callback,
const QSize &targetSize)
378 QQmlEngine *engine = qmlEngine(
this);
380 qmlWarning(
this) <<
"grabToImage: item has no QML engine";
384 if (!callback.isCallable()) {
385 qmlWarning(
this) <<
"grabToImage: 'callback' is not a function";
389 QSize size = targetSize;
391 size = QSize(width(), height());
393 if (size.width() < 1 || size.height() < 1) {
394 qmlWarning(
this) <<
"grabToImage: item has invalid dimensions";
399 qmlWarning(
this) <<
"grabToImage: item is not attached to a window";
403 QQuickItemGrabResult *result = QQuickItemGrabResultPrivate::create(
this, size);
407 connect(window(), &QQuickWindow::beforeSynchronizing, result, &QQuickItemGrabResult::setup, Qt::DirectConnection);
408 connect(window(), &QQuickWindow::afterRendering, result, &QQuickItemGrabResult::render, Qt::DirectConnection);
410 QQuickItemGrabResultPrivate *d = result->d_func();
411 d->qmlEngine = engine;
412 d->callback = callback;
418#include "moc_qquickitemgrabresult.cpp"
QQuickItemGrabResultPrivate()
~QQuickItemGrabResultPrivate()
void ensureImageInCache() const
QPointer< QQuickWindow > window
QQuickPixmap * cacheEntry
static QQuickItemGrabResult * create(QQuickItem *item, const QSize &size)
QPointer< QQuickItem > item
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE const QEvent::Type Event_Grab_Completed