diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-06-08 07:12:53 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-06-13 13:17:54 +0000 |
commit | 0673dd4ff18bbc8d530a4dd08e394cc04c6711ca (patch) | |
tree | 19f46ee5d6074c2aade189c1042a2f00d7a10166 /src/quicktemplates2/qquickmenu.cpp | |
parent | 0a62e444e822bebc48076208451c1eaa2e8cac0c (diff) |
QQuickMenu: add a little delay for opening sub-menus on hover
This improves the usability on desktop. When a menu has multiple sub-
menu items, they don't all trigger a sub-menu (and close immediately)
while moving mouse over the items.
Change-Id: Ie4c9e409da8d6877e35506bffb94ed57f5985dcd
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickmenu.cpp')
-rw-r--r-- | src/quicktemplates2/qquickmenu.cpp | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/quicktemplates2/qquickmenu.cpp b/src/quicktemplates2/qquickmenu.cpp index 37d314a8..1b2ed841 100644 --- a/src/quicktemplates2/qquickmenu.cpp +++ b/src/quicktemplates2/qquickmenu.cpp @@ -59,6 +59,9 @@ QT_BEGIN_NAMESPACE +// copied from qfusionstyle.cpp +static const int SUBMENU_DELAY = 225; + /*! \qmltype Menu \inherits Popup @@ -183,6 +186,7 @@ static bool shouldCascade() QQuickMenuPrivate::QQuickMenuPrivate() : cascade(shouldCascade()), + hoverTimer(0), overlap(0), contentItem(nullptr), contentModel(nullptr), @@ -371,7 +375,7 @@ void QQuickMenuPrivate::onItemHovered() if (currentItem) { QQuickMenu *subMenu = currentItem->menu(); if (subMenu && subMenu->cascade()) - openSubMenu(currentItem, false); + startHoverTimer(); } } } @@ -468,6 +472,23 @@ void QQuickMenuPrivate::closeSubMenu(QQuickMenu *subMenu) } } +void QQuickMenuPrivate::startHoverTimer() +{ + Q_Q(QQuickMenu); + stopHoverTimer(); + hoverTimer = q->startTimer(SUBMENU_DELAY); +} + +void QQuickMenuPrivate::stopHoverTimer() +{ + Q_Q(QQuickMenu); + if (!hoverTimer) + return; + + q->killTimer(hoverTimer); + hoverTimer = 0; +} + int QQuickMenuPrivate::currentIndex() const { QVariant index = contentItem->property("currentIndex"); @@ -483,6 +504,7 @@ void QQuickMenuPrivate::setCurrentIndex(int index) QQuickMenuItem *newCurrentItem = contentItem->property("currentItem").value<QQuickMenuItem *>(); if (currentItem != newCurrentItem) { + stopHoverTimer(); if (currentItem) currentItem->setHighlighted(false); if (newCurrentItem) @@ -1013,6 +1035,15 @@ void QQuickMenu::keyReleaseEvent(QKeyEvent *event) item->forceActiveFocus(); } +void QQuickMenu::timerEvent(QTimerEvent *event) +{ + Q_D(QQuickMenu); + if (event->timerId() == d->hoverTimer) { + d->openSubMenu(d->currentItem, false); + d->stopHoverTimer(); + } +} + QFont QQuickMenu::defaultFont() const { return QQuickControlPrivate::themeFont(QPlatformTheme::MenuFont); |