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. QML controls 2 context menu
Forum Updated to NodeBB v4.3 + New Features

QML controls 2 context menu

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 2.1k Views 2 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.
  • K Offline
    K Offline
    krojew
    wrote on last edited by
    #1

    Hi,
    I'm trying to show a simple context menu for an item:

    Item {
        // bunch of children
    
        MouseArea {
                anchors.fill: parent
                onClicked: {
                    if (mouse.button === Qt.RightButton) {
                        contextMenu.x = mouse.x;
                        contextMenu.y = mouse.y;
    
                        contextMenu.open();
                    }
                }
            }
    
        Menu {
            id: contextMenu
    
            MenuItem {
                text: 'text'
            }
        }
    }
    

    Unfortunately, nothing is shown after the right-click. The documentation for Menu is really lacking any kind of info. Debug shows the handler for mouse click is working. What could be wrong?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! See the two comments inside the code:

          Item {
              anchors.fill: parent // make sure mouse area's parent is bigger than zero
      
              MouseArea {
                  anchors.fill: parent
                  acceptedButtons: Qt.LeftButton | Qt.RightButton // default is Qt.LeftButton only
                  onClicked: {
                      if (mouse.button === Qt.RightButton) {
                          contextMenu.x = mouse.x;
                          contextMenu.y = mouse.y;
                          contextMenu.open()
                      }
                  }
              }
      
              Menu {
                  id: contextMenu
                  MenuItem {
                      text: 'text'
                  }
              }
          }
      
      K 1 Reply Last reply
      0
      • ? A Former User

        Hi! See the two comments inside the code:

            Item {
                anchors.fill: parent // make sure mouse area's parent is bigger than zero
        
                MouseArea {
                    anchors.fill: parent
                    acceptedButtons: Qt.LeftButton | Qt.RightButton // default is Qt.LeftButton only
                    onClicked: {
                        if (mouse.button === Qt.RightButton) {
                            contextMenu.x = mouse.x;
                            contextMenu.y = mouse.y;
                            contextMenu.open()
                        }
                    }
                }
        
                Menu {
                    id: contextMenu
                    MenuItem {
                        text: 'text'
                    }
                }
            }
        
        K Offline
        K Offline
        krojew
        wrote on last edited by
        #3

        @Wieland said in QML controls 2 context menu:

        acceptedButtons: Qt.LeftButton | Qt.RightButton

        Thanks - setting acceptedButton solved the problem.

        1 Reply Last reply
        0

        • Login

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