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
qquickstyleitemsearchfield.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// Qt-Security score:significant reason:default
4
6#include <QtQuickTemplates2/private/qquickindicatorbutton_p.h>
7
9
10QFont QQuickStyleItemSearchField::styleFont(QQuickItem *control) const
11{
12 return style()->font(QStyle::CE_PushButtonLabel, controlSize(control));
13}
14
15void QQuickStyleItemSearchField::connectToControl() const
16{
17 QQuickStyleItem::connectToControl();
18 auto searchField = control<QQuickSearchField>();
19 connect(searchField->searchIndicator(), &QQuickIndicatorButton::pressedChanged, this, &QQuickStyleItem::markImageDirty);
20 connect(searchField->clearIndicator(), &QQuickIndicatorButton::pressedChanged, this, &QQuickStyleItem::markImageDirty);
21 connect(searchField, &QQuickSearchField::textChanged, this, &QQuickStyleItem::markImageDirty);
22
23}
24
25void QQuickStyleItemSearchField::paintEvent(QPainter *painter) const
26{
27 QStyleOptionSearchField styleOption;
28 initStyleOption(styleOption);
29 style()->drawComplexControl(QStyle::CC_SearchField, &styleOption, painter);
30}
31
32StyleItemGeometry QQuickStyleItemSearchField::calculateGeometry() {
33 QStyleOptionSearchField styleOption;
34 initStyleOption(styleOption);
35 StyleItemGeometry geometry;
36
37 geometry.minimumSize = style()->sizeFromContents(QStyle::CT_SearchField, &styleOption, QSize(0, 0));
38
39 if (styleOption.subControls == QStyle::SC_SearchFieldFrame) {
40 geometry.implicitSize = style()->sizeFromContents(QStyle::CT_SearchField, &styleOption, contentSize());
41 styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize);
42 geometry.contentRect = style()->subControlRect(QStyle::CC_SearchField, &styleOption, QStyle::SC_SearchFieldEditField);
43 geometry.layoutRect = style()->subElementRect(QStyle::SE_SearchFieldLayoutItem, &styleOption);
44 geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CC_SearchField, &styleOption, geometry.minimumSize);
45 geometry.focusFrameRadius = style()->pixelMetric(QStyle::PM_SearchFieldFocusFrameRadius, &styleOption);
46 } else {
47 geometry.implicitSize = geometry.minimumSize;
48 }
49
50 return geometry;
51}
52
53void QQuickStyleItemSearchField::initStyleOption(QStyleOptionSearchField &styleOption) const {
54 initStyleOptionBase(styleOption);
55 auto searchField = control<QQuickSearchField>();
56
57 styleOption.text = searchField->text();
58
59 switch (m_subControl) {
60 case Frame:
61 styleOption.subControls = QStyle::SC_SearchFieldFrame;
62 styleOption.frame = true;
63 break;
64 case Search:
65 styleOption.subControls = QStyle::SC_SearchFieldSearch;
66 break;
67 case Clear:
68 styleOption.subControls = QStyle::SC_SearchFieldClear;
69 break;
70 }
71
72 if (searchField->searchIndicator()->isPressed()) {
73 styleOption.activeSubControls = QStyle::SC_SearchFieldSearch;
74 styleOption.state |= QStyle::State_Sunken;
75 } else if (searchField->clearIndicator()->isPressed()) {
76 styleOption.activeSubControls = QStyle::SC_SearchFieldClear;
77 styleOption.state |= QStyle::State_Sunken;
78 }
79}
80
81QT_END_NAMESPACE
82
83#include "moc_qquickstyleitemsearchfield.cpp"