API Documentation

Documentation for developing SailfishOS applications

ContextMenu QML Type

Provides a context-specific menu More...

Import Statement: import Sailfish.Silica 1.0

Properties

Signals

Methods

Detailed Description

The ContextMenu type provides a menu for use in context-specific situations.

A ContextMenu is a pop-up menu that shows beneath a specified item when activated with the show() function. Menu options are specified through MenuItem children.

Context menus are commonly used to display menu options for individual entries within a list. Here is an example that uses the ListItem menu property to show a context menu for each individual list item when the item's press-and-hold gesture is activated:

 import QtQuick 2.2
 import Sailfish.Silica 1.0

 Page {
     SilicaListView {
         anchors.fill: parent

         model: ListModel {
             id: listModel
             Component.onCompleted: {
                 for (var i=0; i<10; i++) {
                     append({"name": "Item " + i})
                 }
             }
         }

         delegate: ListItem {
             width: ListView.view.width

             Label {
                 id: label
                 text: model.name
                 anchors.centerIn: parent
             }

             menu: ContextMenu {
                 MenuItem {
                     text: "Toggle bold font"
                     onClicked: label.font.bold = !label.font.bold
                 }
                 MenuItem {
                     text: "Remove"
                     onClicked: listModel.remove(model.index)
                 }
             }
         }
     }
 }

Warning: The ContextMenu internally adjusts its height whenever it is opened or closed. Therefore, the height should not be explicitly set or changed, and the menu should not be anchored at both its top and bottom edges, else the menu will no longer open and close correctly.

See also MenuItem, PullDownMenu, and PushUpMenu.

Property Documentation

active : bool

This property holds whether the menu is currently shown.


closeOnActivation : bool

If set to true, the menu will automatically close if a child MenuItem is clicked.

The default value is true.


container : Item

This property holds a parent container to be used for calculating how the flickable's contentY should be changed.

Normally, when the menu is opened within a vertically-scrolling flickable, the flickable's contentY moves upwards if this is necessary to make the whole menu visible within the flickable. If the container is set, then its bounds are also taken into account when determining whether the contentY should be moved. This useful if the menu should extend beyond the bounds of the flickable and into the parent container.

The default value is null.


hasContent : bool

This property holds whether the menu has content to be shown when opened; if false, the context menu will not open.

The default value is true if the menu item has any child items. Note that this heuristic will be incorrect if there are only non-visible children in the menu, such as an empty Repeater.


Signal Documentation

onActivated(int index)

Called when a child MenuItem is clicked. The index value is the index of the menu item within this menu.


Method Documentation

close()

Closes the menu.

See also open.


open(Item item)

Shows the menu, anchored to the bottom of the specified parent item. If the ContextMenu is within a flickable view it will adjust the view position to ensure that the menu is completely visible.

See also close.


We use cookies to improve your user experience and to help us to develop our services. By continuing to browse the site, you approve of our use of cookies.