From 31ba4de15b38813ed7f5ff7967ca127865c013be Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen <richard.gustavsen@nokia.com> Date: Wed, 15 Jun 2011 13:50:47 +0200 Subject: [PATCH] ComboBox: Make implementation more fault tolerant --- components/ComboBox.qml | 9 +++++---- components/Menu.qml | 6 ++---- components/styleitem/qtmenu.cpp | 13 +++++++++++++ components/styleitem/qtmenu.h | 1 + 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/components/ComboBox.qml b/components/ComboBox.qml index 62c4e2108..82ee2b962 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 b30121f46..f8dd2ee85 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 15233f60e..e7fa3706a 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 23543e61c..17abc383c 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(); -- GitLab