import QtQuick 2.10 import QtTest 1.2 Item { id: rootItem width: 300 height: 300 focus: true property bool itemReady: false TestCase { id: test_grabImage name: "test_grabImage" when: windowShown && rootItem.itemReady function test_01_grabImage() { // This works because the testRectangle's parent is the root item var obj = findChild(rootItem, "testRectangle") verify(obj != null) var image = grabImage(obj) image.save("testRectangle.png")// This is the blue rectangle containing the yellow one var color = image.pixel(obj.width/2,obj.height/2) verify(color.r === 0 && color.g === 0 && color.b === 1) // This do not works because testRectangle's parent is not the root item obj = findChild(rootItem, "testSubRectangle") verify(obj != null) image = grabImage(obj) image.save("testSubRectangle.png")// This is a white rectangle because the top left corner of the window is grabbed instead of the actual yellow rectangle color = image.pixel(obj.width/2,obj.height/2) verify(color.r === 1 && color.g === 1 && color.b === 0) } } Rectangle { id: testRect objectName: "testRectangle" color: "blue" width: 100 height: 100 anchors.centerIn: parent Rectangle { id: testSubRect objectName: "testSubRectangle" color: "yellow" width: 10 height: 10 anchors{ top: parent.top left: parent.left } } } Component.onCompleted: { rootItem.itemReady = true } }