Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-141195

Improving TestCase::grabToImage documentation: making it clear that it is item window instead of item itself gets grabbed

XMLWordPrintable

      Consider this test case:

      TestCase {
          id: testCase
          visible: true
      
          Rectangle {
              id: redRectangle
      
              color: Qt.rgba(1, 0, 0, 255)
              height: 100
              width: 100
          }
      
          // the greenRectangle is in front of the redRectangle
          Rectangle {
              id: greenRectangle
      
              color: Qt.rgba(0, 1, 0, 255)
              height: 100
              width: 100
          }
      
          function test_saveImage() {
              waitForRendering(redRectangle, 1000);
              let image = grabImage(redRectangle);
      
              // this test fails because the returned color is GREEN
              // this is because it is the window that is grabbed, not the item - the item is only used for its geometry to trim the image.
              // See the actual code here: https://code.qt.io/cgit/qt/qtdeclarative.git/tree/src/qmltest/quicktestresult.cpp#n758
      
              compare(image.pixel(0, 0), Qt.rgba(1, 0, 0, 1));
          }
      }
      

      All is explained in comments. Since green rectangle comes later in order so it is stacked on top of red one. So what actually gets grabbed by "grabImage" is a green pixel.

      By "grabImage", user expects the item to be grabbed. That is what documentatiob suggests:
      "Returns a snapshot image object of the given item".

      But no, it is the section of containing Window that intersects with the item. Item itself is not directly grabbed. Z order matters.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            docteam Qt Documentation Team
            luqiaochen Luqiao Chen
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes