<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[Solved] QML, how to make the contents of one menu dynamically depend on the contents of another]]></title><description><![CDATA[<p dir="auto">I am trying to understand how best to use QML by implementing my own Android action bar.<br />
The action bar has the traditional app icon and name, then some action buttons representing common actions, then an 'action overflow' button for further actions.  When the action bar is narrow, there is less room for the action buttons and I want them to overflow to the secondary menu under the action overflow button.</p>
<p dir="auto">I have an action bar with 4 action buttons that can resize, so buttons that overlap with the app name are not showing, and I have a secondary menu that shows zero or all of the actions depending on an int property.  My problem is how do I set that int dynamically depending on which action buttons are showing.</p>
<p dir="auto">If I set the visible property of the action buttons (so as to use visibleChildren.length of the action bar) then the QML seems to loop.  If I don't use the visible property but instead set the icon source to an empty string, then how can I dynamically determine how many action buttons have an empty string?</p>
<p dir="auto">I would appreciate suggestions on what might be the recommended way to do this in QML.</p>
<p dir="auto">Thank you.</p>
<p dir="auto">main.qml:</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/import">@<bdi>import</bdi></a> QtQuick 2.1<br />
import QtQuick.Controls 1.0<br />
import QtGraphicalEffects 1.0<br />
import QtQuick.Window 2.1</p>
<p dir="auto">ApplicationWindow {<br />
property real scaleFactor: Screen.pixelDensity / 5.0<br />
property int intScaleFactor: Math.max(1, scaleFactor)</p>
<pre><code>width: 640; height: 480

property list&lt;Action&gt; actionsList: [
    Action { text: "Sightings"; iconSource: "qrc:/sightings"; onTriggered: image1.visible = !image1.visible },
    Action { text: "Alerts";    iconSource: "qrc:/alerts";    onTriggered: image1.visible = !image1.visible },
    Action { text: "Search";    iconSource: "qrc:/search";    onTriggered: image1.visible = !image1.visible }
]
property list&lt;Action&gt; moreActionsList: [
    Action { text: "About"; onTriggered: image1.visible = !image1.visible }
]

ActionBar {
    id: actionBar
    barHeight: slider.value
    actions: actionsList
    moreActions: moreActionsList
}

Image {
    id: image1 x: 270 y: 257
    width: 100 height: 100 source: "qrc:/drs"
}
Slider {
    id: slider
    anchors { topMargin: 150; top: parent.top; }
    minimumValue: 30 maximumValue : 150 value: 80
}
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto">ActionBar.qml</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/import">@<bdi>import</bdi></a> QtQuick 2.1<br />
import QtQuick.Controls 1.0<br />
import QtGraphicalEffects 1.0</p>
<p dir="auto">Rectangle {<br />
// Android-style Action bar<br />
property alias barHeight: actionBar.height<br />
property list&lt;Action&gt; actions<br />
property list&lt;Action&gt; moreActions</p>
<pre><code>id: actionBar
height: 80
anchors { right: parent.right; left: parent.left; top: parent.top; }
Image {
    id: image2
    height: barHeight * 0.9; width: height
    anchors { leftMargin: 15; left: parent.left; verticalCenter: parent.verticalCenter; }
    source: "qrc:/drs"
}
Text {
    id: titletext
    text: "ApplicationName" color: "white" font.bold: true
    font.pixelSize: barHeight * 0.33
    anchors { leftMargin: barHeight / 5; left: image2.right; verticalCenter: parent.verticalCenter; }
}
property int actCount: 0
Row {
    id: actionsRow
    anchors { right: parent.right; verticalCenter: parent.verticalCenter; }
    Repeater {
        id: repeat
        model: actions
        ActionBarActionDelegate {
            // Does action icon overlap with the title text
            property bool isNoOverlap: ((titletext.x + titletext.width) &lt; (actionsRow.x + actionsRow.children[index].x))
            iconHeight: barHeight * 0.9
            action: Action {
                // Have an enpty icon if it would overlap the title text
                iconSource: isNoOverlap ? modelData.iconSource : ""
                text: modelData.text
                onTriggered: { modelData.triggered(); }
            }
            // Disable actions that would overlap the title text
            isEnabled: isNoOverlap
        }
    }
    // Now the optional More options icon
    ActionBarActionDelegate {
        iconHeight: barHeight * 0.9
        action: Action { iconSource: "qrc:/more"; onTriggered: { console.log(actCount);
                overflowMenu.popup() }
        }
    }
}

Rectangle {
    // Action bar shadow
    id: actionBarShadow
    height: barHeight &gt; 60 ? 5 : 2
    width: parent.width color: "darkgray" anchors { top: parent.bottom; }
}
property int overflowCount: 1 //actionsRow.visibleChildren.length

Menu {
    id: overflowMenu
    title: "Edit"

    Instantiator {
        id: inst
        model: actionsList
        MenuItem {
            text: modelData.text
            visible: (index &lt; overflowCount)
            onTriggered: modelData.triggered()
        }
        onObjectAdded: overflowMenu.insertItem(index, object)
        onObjectRemoved: overflowMenu.removeItem(object)
    }

    MenuSeparator { visible: (overflowCount &gt; 0) }

    MenuItem { text: "About"
        onTriggered: console.log("overflowCount =", overflowCount); }
}
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto">ActionBarActionDelegate.qml</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/import">@<bdi>import</bdi></a> QtQuick 2.0<br />
import QtQuick.Controls 1.1</p>
<p dir="auto">Item {<br />
property Action action<br />
property alias iconHeight: actionButton.height<br />
property alias isEnabled: actionButton.enabled</p>
<pre><code>id: actionButton height: 70 width: height

Image {
    id: iconImage
    anchors.fill: parent
    source: action ? action.iconSource : ""
}
Rectangle {
    // Hint to show when icon is clicked
    id: iconClickedArea
    anchors.fill: parent
    color: "darkgray" opacity: 0
}
MouseArea {
    anchors.fill: parent
    onClicked: action.triggered()
    onEntered: iconClickedArea.opacity = action ? 0.5 : 0
    onExited:  iconClickedArea.opacity = 0
}
</code></pre>
<p dir="auto">}<br />
@</p>
]]></description><link>https://forum.qt.io/topic/38866/solved-qml-how-to-make-the-contents-of-one-menu-dynamically-depend-on-the-contents-of-another</link><generator>RSS for Node</generator><lastBuildDate>Fri, 08 May 2026 13:23:08 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/38866.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 11 Mar 2014 14:10:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Solved] QML, how to make the contents of one menu dynamically depend on the contents of another on Tue, 11 Mar 2014 20:09:08 GMT]]></title><description><![CDATA[<p dir="auto">I think I may have finally worked out a way to do this. In the ActionBarActionDelegate I need to latch onto the enabled property and then effectively export this by emitting a signal for the ActionBar to handle.  It can handle it by setting the int property to the appropriate action button count.</p>
<p dir="auto">in ActionBarActionDelegate:<br />
@Item {<br />
property int actionIndex: 0    // Index of the action in the action bar list (if set)<br />
property Action action<br />
property alias iconHeight: actionButton.height<br />
signal activationChanged(int actionIndex, bool active)</p>
<pre><code>id: actionButton
height: 70
width: height

onEnabledChanged: activationChanged(actionIndex, enabled)
</code></pre>
<p dir="auto">...@</p>
<p dir="auto">and in ActionBar<br />
@            ActionBarActionDelegate {<br />
iconHeight: barHeight * 0.9<br />
actionIndex: index<br />
action: Action {<br />
iconSource: modelData.iconSource<br />
text: modelData.text<br />
onTriggered: { modelData.triggered(); }<br />
}<br />
// Does action icon overlap with the title text?<br />
// Disable (and implicitly hide) actions that would overlap the title text<br />
enabled: ((titletext.x + titletext.width) &lt; (actionsRow.x + actionsRow.children[index].x))<br />
onActivationChanged: {<br />
if (active &amp;&amp; actionIndex &lt; overflowCount)<br />
overflowCount = actionIndex<br />
else if (!active &amp;&amp; actionIndex &gt;= overflowCount)<br />
overflowCount = actionIndex + 1<br />
console.log("Activated on index:", actionIndex, "as", active, "overflowCount:", overflowCount)<br />
}<br />
}<br />
@</p>
]]></description><link>https://forum.qt.io/post/218129</link><guid isPermaLink="true">https://forum.qt.io/post/218129</guid><dc:creator><![CDATA[MartynW]]></dc:creator><pubDate>Tue, 11 Mar 2014 20:09:08 GMT</pubDate></item></channel></rss>