Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickitemspy.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtQuick/private/qquickitem_p.h>
7#include <QtGui/rhi/qrhi.h>
8
10
11/*!
12 \qmltype ItemSpy
13 \inqmlmodule QtQuick.VectorImage.Helpers
14 \brief Provides a way to track the world size of an item and bind to it.
15 \since 6.11
16
17 This type is used by VectorImage. It enables adjusting a post-processing effect's texture
18 size to automatically match the on-screen size of an item subtree.
19*/
20QQuickItemSpy::QQuickItemSpy(QQuickItem *parent)
21 : QQuickItem(parent)
22{
23 setFlag(ItemObservesViewport);
24}
25
26QQuickItemSpy::~QQuickItemSpy()
27{
28}
29
30/*!
31 \qmlproperty size QtQuick.VectorImage.Helpers::ItemType::requiredTextureSize
32
33 The texture size needed to render this item's width and height without scaling artifacts.
34 This accounts for the current transform as well as high-dpi scaling.
35
36 \note The returned size will never exceed the maximum supported texture size, and it will
37 never be smaller than \c{1x1}.
38*/
39QSizeF QQuickItemSpy::requiredTextureSize() const
40{
41 QSizeF sz = mapRectToScene(QRectF(0, 0, width(), height())).size();
42
43 const qreal dpr = window() != nullptr ? window()->devicePixelRatio() : 1.0;
44
45 sz.setWidth(std::max(1.0, sz.width() * dpr));
46 sz.setHeight(std::max(1.0, sz.height() * dpr));
47
48 if (window() != nullptr && window()->rhi() != nullptr) {
49 QRhi *rhi = window()->rhi();
50 const int textureSizeMax = rhi->resourceLimit(QRhi::TextureSizeMax);
51
52 if (sz.width() > textureSizeMax || sz.height() > textureSizeMax) {
53 if (sz.width() > sz.height())
54 sz = QSizeF(textureSizeMax, sz.height() * (textureSizeMax / sz.width()));
55 else
56 sz = QSizeF(sz.width() * (textureSizeMax / sz.height()), textureSizeMax);
57 }
58 }
59
60 return sz;
61}
62
63void QQuickItemSpy::itemChange(ItemChange change, const ItemChangeData &value)
64{
65 if (change == ItemTransformHasChanged || change == ItemDevicePixelRatioHasChanged)
66 emit requiredTextureSizeChanged();
67
68 QQuickItem::itemChange(change, value);
69}
70
71
72QT_END_NAMESPACE