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. Reduce the right space in a context menu
Qt 6.11 is out! See what's new in the release blog

Reduce the right space in a context menu

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 838 Views 1 Watching
  • 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.
  • franco.amatoF Offline
    franco.amatoF Offline
    franco.amato
    wrote on last edited by franco.amato
    #1

    I created the following custom element to personalize a context menu.

    // LogoMenuItem.qml
    
    import QtQuick
    import QtQuick.Controls
    import QtQuick.Layouts
    
    MenuItem {
        id: menuItem
        verticalPadding: 2
    
        background: Rectangle {
            color: "transparent"
        }
    
        // Icon sources
        property string defaultIconSource: ""
        property string hoverIconSource: ""
    
        // Text colors
        property color defaultTextColor: "white"
        property color hoverColor: "gray"
    
        // Icon size
        property int iconWidth: 24
        property int iconHeight: 24
    
        // Custom contentItem for the MenuItem
        contentItem: Row {
            spacing: 8
            padding: 5
    
            Image {
                id: icon
                source: menuItem.hovered ? menuItem.hoverIconSource : menuItem.defaultIconSource
                width: menuItem.iconWidth
                height: menuItem.iconHeight
            }
            Text {
                id: textLabel
                text: menuItem.text
                color: menuItem.hovered ? menuItem.hoverColor : menuItem.defaultTextColor
                font.family: "Poppins"
                font.pixelSize: 17
                font.styleName: "normal"
                font.weight: Font.Light
                lineHeight: 1.0
                verticalAlignment: Text.AlignVCenter
            }
        }
    }
    

    and an usage example:

    // LeftToolBar.qml
    
    LogoMenuItem {
             text: qsTr("Exit")
             defaultIconSource: "qrc:/Resources/images/Launcher/ContextMenu/exit_default.png"
             hoverIconSource: "qrc:/Resources/images/Launcher/ContextMenu/exit_hover.png"
             iconWidth: 21
             iconHeight: 22
             onTriggered: Qt.quit()
    }
    

    What I wanted to achieve is to show a hover icon when the mouse is hover the item and also avoid showing the classic highlight (the gray rectangle).

    It works well, below an image containing 5 LogoMenuItem items :

    1. "General Settings"
    2. "Support"
    3. "Log Out"
    4. "Mobile App:
    5. "Exit"

    afe61a50-ce02-4739-b01f-bfaa48bf003a-image.png

    I would like to reduce the right spacing: the distance between the right side of the text (in this case the "General Settings" text) and the right border of the context menu and do it like the left spacing.
    Any ideas?
    Thanx

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      What's the parent of your LogoMenuItems and how do you lay them out? Some code showing that would help.

      Have you tried GammaRay? It's useful for debugging items positioning.

      1 Reply Last reply
      0
      • franco.amatoF Offline
        franco.amatoF Offline
        franco.amato
        wrote on last edited by
        #3

        @GrecKo below the code showing how the LogoMenuItem is used:

        Menu {
             id: logoMenu
             x: parent.width - logoMenu.width
             y: parent.height
        
             LogoMenuItem {
                           text: qsTr("General Settings")
                           defaultIconSource: "qrc:/Resources/images/Launcher/ContextMenu/settings_default.png"
                           hoverIconSource: "qrc:/Resources/images/Launcher/ContextMenu/settings_hover.png"
                           iconWidth: 21
                           iconHeight: 23
             }
             LogoMenuItem {
                           text: qsTr("Support")
                           defaultIconSource: "qrc:/Resources/images/Launcher/ContextMenu/support_default.png"
                           hoverIconSource: "qrc:/Resources/images/Launcher/ContextMenu/support_hover.png"
                           iconWidth: 21
                           iconHeight: 16
             }
              LogoMenuItem {
                           text: qsTr("Log Out")
                           defaultIconSource: "qrc:/Resources/images/Launcher/ContextMenu/logout_default.png"
                           hoverIconSource: "qrc:/Resources/images/Launcher/ContextMenu/logout_hover.png"
                           iconWidth: 21
                           iconHeight: 19
              }
              LogoMenuItem {
                           text: qsTr("Mobile App")
                           defaultIconSource: "qrc:/Resources/images/Launcher/ContextMenu/mobile_default.png"
                           hoverIconSource: "qrc:/Resources/images/Launcher/ContextMenu/mobile_hover.png"
                           iconWidth: 17
                           iconHeight: 24
              }
              LogoMenuItem {
                           text: qsTr("Exit")
                           defaultIconSource: "qrc:/Resources/images/Launcher/ContextMenu/exit_default.png"
                           hoverIconSource: "qrc:/Resources/images/Launcher/ContextMenu/exit_hover.png"
                           iconWidth: 21
                           iconHeight: 22
                           onTriggered: Qt.quit()
              }
        
              background: Rectangle {
                           implicitWidth: 220 // Changing this value has no effect
                           color: "#21212D"
              }
        }
        
        onClicked: logoMenu.open()
        

        The parent is Menu {}

        1 Reply Last reply
        0
        • franco.amatoF franco.amato has marked this topic as solved on

        • Login

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