diff options
author | Laszlo Agocs <laszlo.agocs@qt.io> | 2023-08-24 13:18:03 +0200 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@qt.io> | 2023-08-28 21:14:28 +0200 |
commit | 80520c2f52aeaff4d01b2506d080770888ca8ec7 (patch) | |
tree | 26b296434de8e953e509a6b2aa19a181fbd144f5 /tests/auto/widgets/widgets | |
parent | 41f032e358e704693834a98d37331d0d63b7aab9 (diff) |
Enable QWidget::grab() with QRhiWidget in the widget tree
This involves reimplementing QWidgetPrivate::grabFramebuffer().
Widgets call this function whenever a texture-based widget is
encountered.
This implies however that we rename QRhiWidget's own, lightweight
grab function, grab(), because it kind of shadows QWidget's grab().
Switch back to grabFramebuffer() which is what QQuickWidget and
QOpenGLWidget both use.
Supporting QWidget::grab() is particularly important when grabbing
an ancestor of the QRhiWidget, because that has no alternative.
Right now, due to not reimplementing the QWidgetPrivate function,
the place of the QRhiWidget is left empty.
In addition, grabFramebuffer() is now const. This is consistent
with QQuickWidget, but not with QOpenGLWidget and QOpenGLWindow.
Change-Id: I646bd920dab7ba50415dd7ee6b63a209f5673e8f
Reviewed-by: Andy Nichols <andy.nichols@qt.io>
Diffstat (limited to 'tests/auto/widgets/widgets')
-rw-r--r-- | tests/auto/widgets/widgets/qrhiwidget/tst_qrhiwidget.cpp | 49 |
1 files changed, 41 insertions, 8 deletions
diff --git a/tests/auto/widgets/widgets/qrhiwidget/tst_qrhiwidget.cpp b/tests/auto/widgets/widgets/qrhiwidget/tst_qrhiwidget.cpp index 9593b282d9..50aef146a3 100644 --- a/tests/auto/widgets/widgets/qrhiwidget/tst_qrhiwidget.cpp +++ b/tests/auto/widgets/widgets/qrhiwidget/tst_qrhiwidget.cpp @@ -12,6 +12,7 @@ #include <QApplication> #include <QFile> #include <QVBoxLayout> +#include <QScrollArea> #if QT_CONFIG(vulkan) #include <private/qvulkandefaultinstance_p.h> @@ -36,8 +37,10 @@ private slots: void autoRt(); void reparent_data(); void reparent(); - void grab_data(); - void grab(); + void grabFramebufferWhileStillInvisible_data(); + void grabFramebufferWhileStillInvisible(); + void grabViaQWidgetGrab_data(); + void grabViaQWidgetGrab(); void mirror_data(); void mirror(); @@ -409,10 +412,10 @@ void tst_QRhiWidget::simple() QVERIFY(qBlue(c) <= maxFuzz); } - // Now through grab(). + // Now through grabFramebuffer(). QImage resultTwo; if (rhi->backend() != QRhi::Null) { - resultTwo = rhiWidget->grab(); + resultTwo = rhiWidget->grabFramebuffer(); QCOMPARE(errorSpy.count(), 0); QVERIFY(!resultTwo.isNull()); QRgb c = resultTwo.pixel(resultTwo.width() / 2, resultTwo.height() / 2); @@ -422,7 +425,7 @@ void tst_QRhiWidget::simple() } // Check we got the same result from our manual readback and when the - // texture was rendered to again and grab() was called. + // texture was rendered to again and grabFramebuffer() was called. QVERIFY(imageRGBAEquals(resultOne, resultTwo, maxFuzz)); } @@ -668,12 +671,12 @@ void tst_QRhiWidget::reparent() QCOMPARE(errorSpy.count(), 0); } -void tst_QRhiWidget::grab_data() +void tst_QRhiWidget::grabFramebufferWhileStillInvisible_data() { testData(); } -void tst_QRhiWidget::grab() +void tst_QRhiWidget::grabFramebufferWhileStillInvisible() { QFETCH(QRhiWidget::Api, api); @@ -684,7 +687,7 @@ void tst_QRhiWidget::grab() w.resize(1280, 720); QSignalSpy errorSpy(&w, &QRhiWidget::renderFailed); - QImage image = w.grab(); // creates its own QRhi just to render offscreen + QImage image = w.grabFramebuffer(); // creates its own QRhi just to render offscreen QVERIFY(!image.isNull()); QVERIFY(w.rhi()); QVERIFY(w.colorTexture()); @@ -724,6 +727,36 @@ void tst_QRhiWidget::grab() } } +void tst_QRhiWidget::grabViaQWidgetGrab_data() +{ + testData(); +} + +void tst_QRhiWidget::grabViaQWidgetGrab() +{ + QFETCH(QRhiWidget::Api, api); + + SimpleRhiWidget w; + w.setApi(api); + w.resize(1280, 720); + QSignalSpy frameSpy(&w, &QRhiWidget::frameSubmitted); + w.show(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); + QTRY_VERIFY(frameSpy.count() > 0); + + QImage image = w.grab().toImage(); + + if (w.rhi()->backend() != QRhi::Null) { + // It's upside down with Vulkan (Y is not corrected, clipSpaceCorrMatrix() is not used), + // but that won't matter for the test. + QRgb c = image.pixel(image.width() / 2, image.height() / 2); + const int maxFuzz = 1; + QVERIFY(qRed(c) >= 255 - maxFuzz); + QVERIFY(qGreen(c) <= maxFuzz); + QVERIFY(qBlue(c) <= maxFuzz); + } +} + void tst_QRhiWidget::mirror_data() { testData(); |