diff --git a/components/ComboBox.qml b/components/ComboBox.qml index 62c4e2108700668c5be622c8791b057132949817..82ee2b962395f5a7a312fc8cb3008b87ae510a17 100644 --- a/components/ComboBox.qml +++ b/components/ComboBox.qml @@ -24,14 +24,15 @@ import "custom" as Custom * * ListModel { * id: menuItems -* ListElement { text: "Banana" } -* ListElement { text: "Apple" } -* ListElement { text: "Coconut" } +* ListElement { text: "Banana"; color: "Yellow" } +* ListElement { text: "Apple"; color: "Green" } +* ListElement { text: "Coconut"; color: "Brown" } * } * ComboBox { * model: menuItems * width: 200 -* onSelectedTextChanged: console.debug("You selected:" + selectedText) +* onSelectedIndexChanged: console.debug(selectedText + ", " + menuItems.get(selectedIndex).color) +* * } * * Example 2: diff --git a/components/Menu.qml b/components/Menu.qml index b30121f460baa0022f06e72905316af1f6f53bdb..f8dd2ee854c5e77ce7f79bbf776be473d9ce8ab2 100644 --- a/components/Menu.qml +++ b/components/Menu.qml @@ -4,10 +4,8 @@ import "../components/plugin" MenuBase { property ListModel model - property string selectedText: (selectedIndex < menuItems.length) ? - menuItems[selectedIndex].text : model.get(selectedIndex - menuItems.length).text - property string hoveredText: (hoveredIndex < menuItems.length) ? - menuItems[hoveredIndex].text : model.get(hoveredIndex - menuItems.length).text + property string selectedText: itemTextAt(selectedIndex) + property string hoveredText: itemTextAt(hoveredIndex) // 'centerSelectedText' means that the menu will be positioned // so that the selected text' top left corner will be at x, y. diff --git a/components/styleitem/qtmenu.cpp b/components/styleitem/qtmenu.cpp index 15233f60ef642c0a0dbffbd9f1f5812a918b3d24..e7fa3706a78ed678a47f2b43ef67c92c2b53b5bd 100644 --- a/components/styleitem/qtmenu.cpp +++ b/components/styleitem/qtmenu.cpp @@ -108,6 +108,19 @@ void QtMenu::addMenuItem(const QString &text) connect(action, SIGNAL(triggered()), this, SLOT(emitSelected())); connect(action, SIGNAL(hovered()), this, SLOT(emitHovered())); m_menu->insertAction(0, action); + + if (m_menu->actions().size() == 1) + // Inform QML that the selected action (0) now has changed contents: + emit selectedIndexChanged(); +} + +QString QtMenu::itemTextAt(int index) const +{ + QList<QAction *> actionList = m_menu->actions(); + if (index >= 0 && index < actionList.size()) + return actionList[index]->text(); + else + return ""; } void QtMenu::emitSelected() diff --git a/components/styleitem/qtmenu.h b/components/styleitem/qtmenu.h index 23543e61cf5bfbd3d8fab650a4438054e086d237..17abc383c31cb90c40e11c517c378d09468e6e70 100644 --- a/components/styleitem/qtmenu.h +++ b/components/styleitem/qtmenu.h @@ -62,6 +62,7 @@ public: Q_INVOKABLE void closePopup(); Q_INVOKABLE void clearMenuItems(); Q_INVOKABLE void addMenuItem(const QString &text); + Q_INVOKABLE QString itemTextAt(int index) const; Q_SIGNALS: void selectedIndexChanged();