support submenus by (ab)using visualParent
if visualParent is a QAction, this becomes a submenu
This commit is contained in:
parent
7191a1c271
commit
a529db32fd
@ -60,17 +60,34 @@ DialogStatus::Status QMenuProxy::status() const
|
||||
return m_status;
|
||||
}
|
||||
|
||||
QDeclarativeItem *QMenuProxy::visualParent() const
|
||||
QObject *QMenuProxy::visualParent() const
|
||||
{
|
||||
return m_visualParent.data();
|
||||
}
|
||||
|
||||
void QMenuProxy::setVisualParent(QDeclarativeItem *parent)
|
||||
void QMenuProxy::setVisualParent(QObject *parent)
|
||||
{
|
||||
if (m_visualParent.data() == parent) {
|
||||
return;
|
||||
}
|
||||
|
||||
//if the old parent was a QAction, disconnect the menu from it
|
||||
QAction *action = qobject_cast<QAction *>(m_visualParent.data());
|
||||
if (action) {
|
||||
action->setMenu(0);
|
||||
m_menu->clear();
|
||||
}
|
||||
//if parent is a QAction, become a submenu
|
||||
action = qobject_cast<QAction *>(parent);
|
||||
if (action) {
|
||||
action->setMenu(m_menu);
|
||||
m_menu->clear();
|
||||
foreach(QMenuItem* item, m_items) {
|
||||
m_menu->addAction (item);
|
||||
}
|
||||
m_menu->updateGeometry();
|
||||
}
|
||||
|
||||
m_visualParent = parent;
|
||||
emit visualParentChanged();
|
||||
}
|
||||
@ -160,7 +177,7 @@ void QMenuProxy::open()
|
||||
|
||||
QGraphicsObject *parentItem;
|
||||
if (m_visualParent) {
|
||||
parentItem = m_visualParent.data();
|
||||
parentItem = qobject_cast<QGraphicsObject *>(m_visualParent.data());
|
||||
} else {
|
||||
parentItem = qobject_cast<QGraphicsObject *>(parent());
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ class QMenuProxy : public QObject
|
||||
|
||||
Q_PROPERTY(QDeclarativeListProperty<QMenuItem> content READ content CONSTANT)
|
||||
Q_CLASSINFO("DefaultProperty", "content")
|
||||
Q_PROPERTY(QDeclarativeItem *visualParent READ visualParent WRITE setVisualParent NOTIFY visualParentChanged())
|
||||
Q_PROPERTY(QObject *visualParent READ visualParent WRITE setVisualParent NOTIFY visualParentChanged())
|
||||
Q_PROPERTY(DialogStatus::Status status READ status NOTIFY statusChanged)
|
||||
|
||||
public:
|
||||
@ -46,8 +46,8 @@ public:
|
||||
QMenuItem *action(int) const;
|
||||
DialogStatus::Status status() const;
|
||||
|
||||
QDeclarativeItem *visualParent() const;
|
||||
void setVisualParent(QDeclarativeItem *parent);
|
||||
QObject *visualParent() const;
|
||||
void setVisualParent(QObject *parent);
|
||||
|
||||
void showMenu(int x, int y);
|
||||
Q_INVOKABLE void open();
|
||||
@ -72,7 +72,7 @@ private:
|
||||
QList<QMenuItem*> m_items;
|
||||
QMenu *m_menu;
|
||||
DialogStatus::Status m_status;
|
||||
QWeakPointer<QDeclarativeItem> m_visualParent;
|
||||
QWeakPointer<QObject> m_visualParent;
|
||||
};
|
||||
|
||||
#endif //QMENU_PROXY_H
|
||||
|
Loading…
Reference in New Issue
Block a user