diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2018-03-15 09:53:48 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2018-03-22 20:41:35 +0000 |
commit | 1abc7fcbabcc5a89ceaaf69173d077e22ed2d658 (patch) | |
tree | bd8709695ede60b257adac02aa9c3f835b0d73af /src/quicktemplates2/qquickpopup.cpp | |
parent | ffc64f07ade1d3c80e7d20e4f73210cca2b1181a (diff) |
Add anchors property to Popup to allow centering in parent/window
Currently, users must manually position their Popup using x and y
bindings:
Popup {
x: (parent.width - width) / 2
y: (parent.height - height) / 2
}
This patch adds an anchors property so that you can do this instead:
Popup {
anchors.centerIn: parent
}
It's also possible to conveniently center within the window from
anywhere within the scene (106e7b63 also documents an alternative way
of doing this using Overlay):
Window {
id: window
Pane {
Popup {
anchors.centerIn: window
}
}
}
QQuickAnchors were never used with Popup, because we cannot use the
QQuickAnchors implementation as-is, as the visual QQuickItem parent
is not the actual parent item of QQuickPopupItem.
Currently just centerIn is supported, as that's the most common
use case.
[ChangeLog][Controls][Popup] Added anchors.centerIn to Popup to allow
a covenient way of centering a popup.
Task-number: QTBUG-60354
Change-Id: Ia030f812df9da646fea8f373ef6199a21205ffbd
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickpopup.cpp')
-rw-r--r-- | src/quicktemplates2/qquickpopup.cpp | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/src/quicktemplates2/qquickpopup.cpp b/src/quicktemplates2/qquickpopup.cpp index 84b17d65..03dd3dd0 100644 --- a/src/quicktemplates2/qquickpopup.cpp +++ b/src/quicktemplates2/qquickpopup.cpp @@ -36,6 +36,7 @@ #include "qquickpopup_p.h" #include "qquickpopup_p_p.h" +#include "qquickpopupanchors_p.h" #include "qquickpopupitem_p_p.h" #include "qquickpopuppositioner_p_p.h" #include "qquickapplicationwindow_p.h" @@ -178,6 +179,11 @@ QT_BEGIN_NAMESPACE \include qquickoverlay-popup-parent.qdocinc + Another way to center a popup in the window regardless of its parent item + is to use \l {anchors.centerIn}: + + \snippet qtquickcontrols2-popup.qml centerIn + \sa {Popup Controls}, {Customizing Popup}, ApplicationWindow */ @@ -259,7 +265,8 @@ QQuickPopupPrivate::QQuickPopupPrivate() exit(nullptr), popupItem(nullptr), positioner(nullptr), - transitionManager(this) + transitionManager(this), + anchors(nullptr) { } @@ -572,6 +579,48 @@ void QQuickPopupPrivate::setBottomMargin(qreal value, bool reset) } } +/*! + \since QtQuick.Controls 2.5 (Qt 5.12) + \qmlpropertygroup QtQuick.Controls::Popup::anchors + \qmlproperty Object QtQuick.Controls::Popup::anchors.centerIn + + Anchors provide a way to position an item by specifying its + relationship with other items. + + A common use case is to center a popup within its parent. One way to do + this is with the \l {Item::}{x} and \l {Item::}{y} properties. Anchors offer + a more convenient approach: + + \qml + Pane { + // ... + + Popup { + anchors.centerIn: parent + } + } + \endqml + + It is also possible to center the popup in the window by using \l Overlay: + + \snippet qtquickcontrols2-popup.qml centerIn + + This makes it easy to center a popup in the window from any component. + + \note Popups can only be centered within their immediate parent or + the window overlay; trying to center in other items will produce a warning. + + \sa {Popup Positioning}, {Item::anchors} +*/ +QQuickPopupAnchors *QQuickPopupPrivate::getAnchors() +{ + if (!anchors) { + Q_Q(QQuickPopup); + anchors = new QQuickPopupAnchors(positioner, q); + } + return anchors; +} + void QQuickPopupPrivate::setWindow(QQuickWindow *newWindow) { Q_Q(QQuickPopup); @@ -2470,3 +2519,5 @@ bool QQuickPopup::setAccessibleProperty(const char *propertyName, const QVariant } QT_END_NAMESPACE + +#include "moc_qquickpopup_p.cpp" |