diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2018-02-13 16:18:26 +0100 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2018-02-16 15:17:30 +0000 |
commit | f6eb6a3661f5c8c47c539d38b5bcb0a33c22d904 (patch) | |
tree | 72dbe00758ea1565f554834d6a71d86984891e85 /src/quicktemplates2/qquicktumbler.cpp | |
parent | acfe7fc8bb19c591a4c44d88aac95b3caa3d445f (diff) |
Tumbler: add positionViewAtIndex() function
This calls the respective PathView/ListView function (depending on
the value of wrap), and allows users to change the current index
without animations.
[ChangeLog][Controls][Tumbler] Added positionViewAtIndex() function
that calls the respective PathView/ListView function, depending on
the value of wrap. This allows changing currentIndex without
animations.
Task-number: QTBUG-66358
Change-Id: I7fe73bd3e53548ef41a165b68637d99f3171e02e
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquicktumbler.cpp')
-rw-r--r-- | src/quicktemplates2/qquicktumbler.cpp | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/src/quicktemplates2/qquicktumbler.cpp b/src/quicktemplates2/qquicktumbler.cpp index 7e9d70be..fa3a5ce4 100644 --- a/src/quicktemplates2/qquicktumbler.cpp +++ b/src/quicktemplates2/qquicktumbler.cpp @@ -70,7 +70,8 @@ QT_BEGIN_NAMESPACE The API is similar to that of views like \l ListView and \l PathView; a \l model and \l delegate can be set, and the \l count and \l currentItem - properties provide read-only access to information about the view. + properties provide read-only access to information about the view. To + position the view at a certain index, use \l positionViewAtIndex(). Unlike views like \l PathView and \l ListView, however, there is always a current item (when the model isn't empty). This means that when \l count is @@ -350,6 +351,8 @@ int QQuickTumbler::count() const The value of this property is \c -1 when \l count is equal to \c 0. In all other cases, it will be greater than or equal to \c 0. + + \sa currentItem, positionViewAtIndex() */ int QQuickTumbler::currentIndex() const { @@ -410,6 +413,8 @@ void QQuickTumbler::setCurrentIndex(int currentIndex) \readonly This property holds the item at the current index. + + \sa currentIndex, positionViewAtIndex() */ QQuickItem *QQuickTumbler::currentItem() const { @@ -511,6 +516,41 @@ bool QQuickTumbler::isMoving() const return d->view && d->view->property("moving").toBool(); } +/*! + \qmlmethod void QtQuick.Controls::Tumbler::positionViewAtIndex(int index, PositionMode mode) + \since QtQuick.Controls 2.5 (Qt 5.12) + + Positions the view so that the \a index is at the position specified by \a mode. + + For example: + + \code + positionViewAtIndex(10, Tumbler.Center) + \endcode + + If \l wrap is true (the default), the modes available to \l {PathView}'s + \l {PathView::}{positionViewAtIndex()} function + are available, otherwise the modes available to \l {ListView}'s + \l {ListView::}{positionViewAtIndex()} function + are available. + + \note There is a known limitation that using \c Tumbler.Beginning when \l + wrap is \c true will result in the wrong item being positioned at the top + of view. As a workaround, pass \c {index - 1}. + + \sa currentIndex +*/ +void QQuickTumbler::positionViewAtIndex(int index, QQuickTumbler::PositionMode mode) +{ + Q_D(QQuickTumbler); + if (!d->view) { + d->warnAboutIncorrectContentItem(); + return; + } + + QMetaObject::invokeMethod(d->view, "positionViewAtIndex", Q_ARG(int, index), Q_ARG(int, mode)); +} + void QQuickTumbler::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) { Q_D(QQuickTumbler); @@ -609,7 +649,7 @@ void QQuickTumblerPrivate::setupViewData(QQuickItem *newControlContentItem) return; if (viewContentItemType == QQuickTumblerPrivate::UnsupportedContentItemType) { - qWarning() << "Tumbler: contentItem must contain either a PathView or a ListView"; + warnAboutIncorrectContentItem(); return; } @@ -636,6 +676,12 @@ void QQuickTumblerPrivate::setupViewData(QQuickItem *newControlContentItem) calculateDisplacements(); } +void QQuickTumblerPrivate::warnAboutIncorrectContentItem() +{ + Q_Q(QQuickTumbler); + qmlWarning(q) << "Tumbler: contentItem must contain either a PathView or a ListView"; +} + void QQuickTumblerPrivate::syncCurrentIndex() { const int actualViewIndex = view->property("currentIndex").toInt(); |