Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Active indicator on custom menu
Forum Update on Monday, May 27th 2025

Active indicator on custom menu

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 1 Posters 330 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    ParkerInt64
    wrote on 5 Jun 2023, 02:48 last edited by
    #1

    So I've been tring to use qml to create a Material3 like NavigationRail.

    I've tried to use the FocusScope to act like a active indicator, represent which icon I selected. This works well if I only operate in the menu area, howerer once I clicked the Button that is not within its area, the indicator I wrote lose the focus.

    Is there a better way to handle such case ?

    Here are some of the qml files.

    NavigationRail.qml

    import QtQuick
    import QtQuick.Controls
    import QtQuick.Layouts
    
    /*
    *  @brief: this is the navigation rail
    *
    */
    
    Pane {
        id: navPaneContainer
        width: 80
        height: 600
        padding: 0
    
        ColumnLayout {
            id: appIconLayout
            focus: true
    
            spacing: 12
    
            Item {
                width: 24
                height: 24
    
                Layout.alignment: Qt.AlignTop
                Layout.leftMargin: 28
                Layout.topMargin: 20
                Layout.bottomMargin: 30
    
                Image {
                    id: menu
                    width: 24
                    height: 24
                    anchors.centerIn: parent
                    source: "../../assets/material-icons/menu.svg"
                    fillMode: Image.PreserveAspectFit
                    smooth: true
                    mipmap: true
    
                    ToolTip.visible: menuMA.containsMouse
                    ToolTip.text: qsTr("Menu")
    
    
                    MouseArea {
                        id: menuMA
                        anchors.fill: parent
                        cursorShape: Qt.PointingHandCursor
                        hoverEnabled: true
                    }
                }
            }
    
            AppIcons {
                id: home
                source: "\ue88a"
                text: qsTr("Home")
                focus: true
    
            }
    
            AppIcons {
                id: settings
                source: "\ue8b8"
                text: qsTr("Settings")
            }
        }
    }
    

    AppIcons.qml

    import QtQuick
    import QtQuick.Controls
    import QtQuick.Layouts
    import QtQuick.Controls.Material.impl
    
    /*
     * @brief: sidebar icons
     *         the material3 style
    */
    
    Item {
        id: iconContainer
        width: 80
        height: 56
    
        property alias source: iconText.text
        property alias text: iconLabel.text
        property alias textColor: iconLabel.color
        property alias color: bgRect.color
    
    
        FocusScope {
            id: iconScope
            width: parent.width
            height: parent.height
            focus: true
    
            ColumnLayout {
                id: iconLayout
    
                anchors.fill: parent
    
                spacing: 4
    
                Rectangle {
                    id: bgRect
                    width: 56
                    height: 32
                    radius: 16
                    Layout.alignment: Qt.AlignCenter
                    color: "transparent"
    
                    Rectangle{
                        id: hoverRect
                        anchors.fill: parent
                        color: "transparent"
                        radius: 16
                        z: -5
                        state: "mouseOut"
                    }
    
                    Ripple {
                        id: ripple
                        width: bgRect.width
                        height: bgRect.height
                        anchor: bgRect
                        clipRadius: parent.radius
                        color: "#60000000"
                        z: -20
                        pressed: iconMouseArea.pressed
                        active: enabled && (iconMouseArea.pressed)
                    }
    
                    Text {
                        id: iconText
                        anchors.centerIn: parent
                        font.family: m3icons.font.family
                        font.weight: m3icons.font.weight
                        font.styleName: m3icons.font.styleName
                        font.pixelSize: 24
                    }
    
                }
    
                Text {
                    id: iconLabel
                    Layout.alignment: Qt.AlignHCenter
                    lineHeight: 16
                    font.pixelSize: 12
                    font.weight: 500
                }
            }
        }
    
        MouseArea {
            id: iconMouseArea
            anchors.fill: parent
            focus: true
            hoverEnabled: true
            cursorShape: Qt.PointingHandCursor
            onClicked:  {
                iconLayout.forceActiveFocus()
            }
        }
    
    
    
        states: [
            State {
                name: "active"
                when: iconLayout.activeFocus
                PropertyChanges {
                    target: bgRect
                    color: "#E8DEF8"
                }
    
                PropertyChanges {
                    target: iconText
                    scale: 1.2
                }
    
            },
    
            State {
                name: "hover"
                when: iconMouseArea.containsMouse
                PropertyChanges {
                    target: hoverRect
                    color: "#CECECE"
    
                }
            }    ]
    
        transitions: Transition {
    
                NumberAnimation {
                    properties: "scale"
                    duration: 200
                }
        }
    
        // Load the material icon font
        FontLoader {
            id: m3icons
            source: "../../assets/fonts/material-icons.ttf"
        }
    }
    
    

    demo 00_00_00-00_00_30.gif

    Note: I also tried to set the Button 's focusPolicy to Qt.NoFocus. And this could work. But in that case I would have to change lots of components' focusPolicy.

    1 Reply Last reply
    0
    • P ParkerInt64
      5 Jun 2023, 03:12

      demo0.png

      demo1.png

      demo2.png

      So what's wrong with the images I upload ? :(

      P Offline
      P Offline
      ParkerInt64
      wrote on 6 Jun 2023, 03:19 last edited by
      #3

      So finally, focusScope doesn't help. Simple ListView solve the problem. Not sure what I am thinking at the start...

      1 Reply Last reply
      0
      • P Offline
        P Offline
        ParkerInt64
        wrote on 5 Jun 2023, 03:12 last edited by
        #2

        demo0.png

        demo1.png

        demo2.png

        So what's wrong with the images I upload ? :(

        P 1 Reply Last reply 6 Jun 2023, 03:19
        0
        • P ParkerInt64
          5 Jun 2023, 03:12

          demo0.png

          demo1.png

          demo2.png

          So what's wrong with the images I upload ? :(

          P Offline
          P Offline
          ParkerInt64
          wrote on 6 Jun 2023, 03:19 last edited by
          #3

          So finally, focusScope doesn't help. Simple ListView solve the problem. Not sure what I am thinking at the start...

          1 Reply Last reply
          0
          • P ParkerInt64 has marked this topic as solved on 6 Jun 2023, 03:19

          1/3

          5 Jun 2023, 02:48

          • Login

          • Login or register to search.
          1 out of 3
          • First post
            1/3
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved