summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel de Dietrich <gabriel.dedietrich@theqtcompany.com>2015-03-30 19:15:34 +0200
committerGabriel de Dietrich <gabriel.dedietrich@theqtcompany.com>2015-04-16 12:13:17 +0000
commit13aad0cf03170cde6dd147f9d2ce460bac98d712 (patch)
tree3b5cc24d9c6e4095ce2c0d0f88fce4e04619f4dc
parent5da5925ca6c676a8b36ebd863267b04906ef57d2 (diff)
downloadqtquickcontrols-13aad0cf03170cde6dd147f9d2ce460bac98d712.tar.gz
Menu: Add aboutToShow, aboutToHide signals
These do the same as for QMenu, and are emitted right before the pop-up is shown or hidden. On Mac, these signals are emitted by the QPA plugin and relayed by QQuickMenu. Task-number: QTBUG-40576 Change-Id: I59113d8d9cc8c5b3140f4f552772d33dd0b6e138 Reviewed-by: Caroline Chao <caroline.chao@theqtcompany.com>
-rw-r--r--src/controls/Menu.qml2
-rw-r--r--src/controls/qquickmenu.cpp35
-rw-r--r--src/controls/qquickmenu_p.h5
-rw-r--r--tests/auto/controls/data/tst_menu.qml30
4 files changed, 65 insertions, 7 deletions
diff --git a/src/controls/Menu.qml b/src/controls/Menu.qml
index b85008eb..7334c28c 100644
--- a/src/controls/Menu.qml
+++ b/src/controls/Menu.qml
@@ -150,7 +150,7 @@ MenuPrivate {
/*! \internal */
property int __currentIndex: -1
/*! \internal */
- on__MenuClosed: __currentIndex = -1
+ onAboutToHide: __currentIndex = -1
on__MenuPopupDestroyed: contentLoader.active = false
onPopupVisibleChanged: {
if (__popupVisible)
diff --git a/src/controls/qquickmenu.cpp b/src/controls/qquickmenu.cpp
index c476c5f3..617a3d03 100644
--- a/src/controls/qquickmenu.cpp
+++ b/src/controls/qquickmenu.cpp
@@ -106,8 +106,12 @@ QT_BEGIN_NAMESPACE
/*!
\qmlproperty bool Menu::visible
- Whether the menu should be visible. This is only enabled when the menu is used as
- a submenu or in the menubar. Its value defaults to \c true.
+ Whether the menu should be visible as a submenu of another Menu, or as a menu on a MenuBar.
+ Its value defaults to \c true.
+
+ \note This has nothing to do with the actual menu pop-up window being visible. Use
+ \l aboutToShow() and \l aboutToHide() if you need to know when the pop-up window will
+ be shown or hidden.
*/
/*!
@@ -231,6 +235,25 @@ QT_BEGIN_NAMESPACE
\sa insertItem()
*/
+
+/*!
+ \qmlsignal Menu::aboutToShow()
+ \since QtQuick.Controls 1.4
+
+ This signal is emitted just before the menu is shown to the user.
+
+ \sa aboutToHide()
+*/
+
+/*!
+ \qmlsignal Menu::aboutToHide()
+ \since QtQuick.Controls 1.4
+
+ This signal is emitted just before the menu is hidden from the user.
+
+ \sa aboutToShow()
+*/
+
QQuickMenu::QQuickMenu(QObject *parent)
: QQuickMenuText(parent, QQuickMenuItemType::Menu),
m_itemsCount(0),
@@ -248,6 +271,7 @@ QQuickMenu::QQuickMenu(QObject *parent)
m_platformMenu = QGuiApplicationPrivate::platformTheme()->createPlatformMenu();
if (m_platformMenu) {
+ connect(m_platformMenu, SIGNAL(aboutToShow()), this, SIGNAL(aboutToShow()));
connect(m_platformMenu, SIGNAL(aboutToHide()), this, SLOT(__closeMenu()));
if (platformItem())
platformItem()->setMenu(m_platformMenu);
@@ -435,6 +459,7 @@ void QQuickMenu::__popup(const QRectF &targetRect, int atItemIndex, MenuType men
m_popupWindow->setPosition(targetRect.x() + m_xOffset + renderOffset.x(),
targetRect.y() + targetRect.height() + m_yOffset + renderOffset.y());
+ emit aboutToShow();
m_popupWindow->show();
}
}
@@ -465,11 +490,13 @@ QRect QQuickMenu::popupGeometry() const
void QQuickMenu::__closeMenu()
{
- setPopupVisible(false);
+ if (m_popupVisible) {
+ emit aboutToHide();
+ setPopupVisible(false);
+ }
if (m_popupWindow)
m_popupWindow->setVisible(false);
m_parentWindow = 0;
- emit __menuClosed();
}
QQuickMenuPopupWindow *QQuickMenu::topMenuPopup() const
diff --git a/src/controls/qquickmenu_p.h b/src/controls/qquickmenu_p.h
index b468828c..a16e720a 100644
--- a/src/controls/qquickmenu_p.h
+++ b/src/controls/qquickmenu_p.h
@@ -99,9 +99,10 @@ Q_SIGNALS:
void titleChanged();
void __selectedIndexChanged();
- void __menuClosed();
- void __menuPopupDestroyed();
+ void aboutToShow();
+ void aboutToHide();
void popupVisibleChanged();
+ void __menuPopupDestroyed();
void __popupGeometryChanged();
void menuContentItemChanged();
void minimumWidthChanged();
diff --git a/tests/auto/controls/data/tst_menu.qml b/tests/auto/controls/data/tst_menu.qml
index ca266466..83411bcf 100644
--- a/tests/auto/controls/data/tst_menu.qml
+++ b/tests/auto/controls/data/tst_menu.qml
@@ -65,6 +65,18 @@ TestCase {
signalName: "triggered"
}
+ SignalSpy {
+ id: aboutToShowSpy
+ target: testcase.menu
+ signalName: "aboutToShow"
+ }
+
+ SignalSpy {
+ id: aboutToHideSpy
+ target: testcase.menu
+ signalName: "aboutToHide"
+ }
+
Component {
id: creationComponent
Menu {
@@ -82,6 +94,8 @@ TestCase {
function cleanup() {
menuSpy.clear()
menuItemSpy.clear()
+ aboutToShowSpy.clear()
+ aboutToHideSpy.clear()
if (menu !== 0)
menu.destroy()
}
@@ -218,4 +232,20 @@ TestCase {
compare(item.text, "Item " + i)
}
}
+
+ function test_popupSignals() {
+ if (Qt.platform.os === "osx") {
+ // On Mac the popup() function blocks. The signals are still
+ // emitted by the QPA plugin and the relayed by QQuickMenu.
+ return;
+ }
+ menu.__popup(Qt.rect(50, 50, 20, 20))
+ wait(100) // Give it a chance to actually pop-up
+ compare(aboutToShowSpy.count, 1)
+ compare(aboutToHideSpy.count, 0)
+ aboutToShowSpy.clear()
+ menu.__dismissMenu()
+ compare(aboutToShowSpy.count, 0)
+ compare(aboutToHideSpy.count, 1)
+ }
}