Extend API to allow (re)positioning menu items during procedural insert.

REVIEW:127645
This commit is contained in:
Eike Hein 2016-04-14 01:15:41 +09:00
parent 553a9bfe53
commit 70301e21b9
2 changed files with 19 additions and 5 deletions

View File

@ -167,10 +167,20 @@ void QMenuProxy::addMenuItem(const QString &text)
m_items << item;
}
void QMenuProxy::addMenuItem(QMenuItem *item)
void QMenuProxy::addMenuItem(QMenuItem *item, QMenuItem *before)
{
m_menu->addAction(item->action());
m_items << item;
if (before) {
if (m_items.contains(item)) {
m_menu->removeAction(item->action());
} else {
m_items << item;
}
m_menu->insertAction(before->action(), item->action());
} else if (!m_items.contains(item)) {
m_menu->addAction(item->action());
m_items << item;
}
}
void QMenuProxy::addSection(const QString &text)

View File

@ -116,9 +116,13 @@ public:
*/
Q_INVOKABLE void addMenuItem(const QString &text);
/**
* This adds a menu item of type MenuItem
* This adds MenuItem 'item' to the menu before MenuItem 'before'.
* If MenuItem 'before' is 0 or does not exist in the menu, 'item'
* is appended to the menu instead.
* If MenuItem 'item' already exists in the menu, it is removed and
* inserted at the new position.
*/
Q_INVOKABLE void addMenuItem(QMenuItem *item);
Q_INVOKABLE void addMenuItem(QMenuItem *item, QMenuItem *before = nullptr);
/**
* This adds a section header with a string used as name for the section
*/