QT_BEGIN_NAMESPACE
+extern Q_GUI_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format, bool include_alpha);
+
void QQuickWindowPrivate::updateFocusItemTransform()
{
Q_Q(QQuickWindow);
/*!
Grabs the contents of the window and returns it as an image.
- This function might not work if the window is not visible.
+ It is possible to call the grabWindow() function when the window is not
+ visible. This requires that the window is \l{QWindow::create} {created}
+ and has a valid size and that no other QQuickWindow instances are rendering
+ in the same process.
\warning Calling this function will cause performance problems.
QImage QQuickWindow::grabWindow()
{
Q_D(QQuickWindow);
+ if (!isVisible()) {
+
+ if (d->context->isReady()) {
+ qWarning("QQuickWindow::grabWindow: scene graph already in use");
+ return QImage();
+ }
+
+ if (!handle() || !size().isValid()) {
+ qWarning("QQuickWindow::grabWindow: window must be created and have a valid size");
+ return QImage();
+ }
+
+ QOpenGLContext context;
+ context.setFormat(requestedFormat());
+ context.create();
+ context.makeCurrent(this);
+ d->context->initialize(&context);
+
+ d->polishItems();
+ d->syncSceneGraph();
+ d->renderSceneGraph(size());
+
+ QImage image = qt_gl_read_framebuffer(size(), false, false);
+ d->cleanupNodesOnShutdown();
+ d->context->invalidate();
+ context.doneCurrent();
+
+ return image;
+ }
+
return d->windowManager->grab(this);
}
void qmlCreation();
void clearColor();
+ void grab_data();
void grab();
void multipleWindows();
QCOMPARE(window->color(), QColor(Qt::blue));
}
+void tst_qquickwindow::grab_data()
+{
+ QTest::addColumn<bool>("visible");
+ QTest::newRow("visible") << true;
+ QTest::newRow("invisible") << false;
+}
+
void tst_qquickwindow::grab()
{
+ QFETCH(bool, visible);
+
QQuickWindow window;
window.setColor(Qt::red);
window.resize(250, 250);
- window.show();
- QVERIFY(QTest::qWaitForWindowExposed(&window));
+ if (visible) {
+ window.show();
+ QVERIFY(QTest::qWaitForWindowExposed(&window));
+ } else {
+ window.create();
+ }
QImage content = window.grabWindow();
QCOMPARE(content.width(), window.width());