我们在非常多的系统中看见能够在屏幕的一个地方长按,然后就能够依据当前显示的上下文弹出一个菜单。

菜单中能够有一些选项,比方删除,改动该项。这样的一般在ListViewGridView中常见。今天,我们就在这个例程中具体介绍怎样实现这个功能。

对ListView来说,我们仅仅须要对它的delegate做一些改动:

        Component {
id: listDelegate ListItem {
id: delegateItem
width: listView.width; height: units.gu(10)
onPressAndHold: ListView.view.ViewItems.dragMode =
!ListView.view.ViewItems.dragMode Image {
id: pic
height: parent.height - units.gu(1)
width: height
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.leftMargin: units.gu(0.5)
source: image
} Column {
id: content
anchors.top: parent.top
anchors.left: pic.right
anchors.leftMargin: units.gu(2)
anchors.topMargin: units.gu(1)
width: parent.width - pic.width - units.gu(1)
height: parent.height
spacing: units.gu(1) Label {
text: name
} Label { text: description } Label {
text: '$' + Number(cost).toFixed(2)
font.bold: true
}
} ListView.onAdd: SequentialAnimation {
PropertyAction { target: delegateItem; property: "height"; value: 0 }
NumberAnimation { target: delegateItem; property: "height"; to: delegateItem.height; duration: 250; easing.type: Easing.InOutQuad }
} ListView.onRemove: SequentialAnimation {
PropertyAction { target: delegateItem; property: "ListView.delayRemove"; value: true }
NumberAnimation { target: delegateItem; property: "height"; to: 0; duration: 250; easing.type: Easing.InOutQuad } // Make sure delayRemove is set back to false so that the item can be destroyed
PropertyAction { target: delegateItem; property: "ListView.delayRemove"; value: false }
} /* create an empty item centered in the image to align the popover to */
Item {
id: emptyItemForCaller
anchors.centerIn: parent
z: 100 } Component {
id: actPopComp ActionSelectionPopover {
id: actPop
delegate: ListItems.Standard {
text: action.text
} actions: ActionList {
Action {
text: "Add 1 dollar"
iconName: "add"
onTriggered: {
PopupUtils.close(actPop);
console.log("Add 1 dollar");
fruitModel.setProperty(index, "cost", cost + 1.0);
}
}
Action {
text: "Deduct 1 dollar"
iconName: "remove"
onTriggered: {
PopupUtils.close(actPop);
console.log("Deduct 1 dollar");
fruitModel.setProperty(index, "cost", Math.max(0,cost-1.0));
}
}
Action {
text: "delete"
iconName: "delete" onTriggered: {
console.log("delete the item!");
fruitModel.remove(index)
}
}
}
}
} MouseArea {
anchors.fill: parent
onPressAndHold: {
PopupUtils.open(actPopComp, emptyItemForCaller);
} onClicked: {
console.log("we can do something else!");
}
}
}
}

从上面的代码中能够看出:

  /* create an empty item centered in the image to align the popover to */
Item {
id: emptyItemForCaller
anchors.centerIn: parent
z: 100 }

我们使用了一个空的Item来作为一个placeholder。

这个是为了给我们在长按ListView中项时,来提供一个位置显示我们的Popup menu。

当我们长按我们的ListView中的项时,我们能够通过例如以下的方法来得到事件并弹出我们所须要的Popup:

               MouseArea {
anchors.fill: parent
onPressAndHold: {
PopupUtils.open(actPopComp, emptyItemForCaller);
} onClicked: {
console.log("we can do something else!");
}
}

这里,我们的actPopComp的设计为:

                Component {
id: actPopComp ActionSelectionPopover {
id: actPop
delegate: ListItems.Standard {
text: action.text
} actions: ActionList {
Action {
text: "Add 1 dollar"
iconName: "add"
onTriggered: {
PopupUtils.close(actPop);
console.log("Add 1 dollar");
fruitModel.setProperty(index, "cost", cost + 1.0);
}
}
Action {
text: "Deduct 1 dollar"
iconName: "remove"
onTriggered: {
PopupUtils.close(actPop);
console.log("Deduct 1 dollar");
fruitModel.setProperty(index, "cost", Math.max(0,cost-1.0));
}
}
Action {
text: "delete"
iconName: "delete" onTriggered: {
console.log("delete the item!");
fruitModel.remove(index)
}
}
}
}
}

在这里。我们有三个Action的菜单。

执行我们的应用:

 

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="200" height="300" alt=""> 

整个项目的源代码在:https://github.com/liu-xiao-guo/contextmenu

怎样在QML应用中创建一个Context Menu的更多相关文章

  1. 创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来

    /*4.创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来*/ #import <Foundation/Foundation.h>#defin ...

  2. Ionic 2 中创建一个照片倾斜浏览组件

    内容简介 今天介绍一个新的UI元素,就是当我们改变设备的方向时,我们可以看到照片的不同部分,有一种身临其境的感觉,类似于360全景视图在移动设备上的应用. 倾斜照片浏览 Ionic 2 实例开发 新增 ...

  3. iOS9中如何在日历App中创建一个任意时间之前开始的提醒(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 四.创建任意时间之前开始的提醒 现在我们找到了指定源中的指定日 ...

  4. 在C#/.NET应用程序开发中创建一个基于Topshelf的应用程序守护进程(服务)

    本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...

  5. Eclipse中创建一个新的SpringBoot项目

    在Eclipse中创建一个新的spring Boot项目: 1. 首先在Eclipse中安装STS插件:在Eclipse主窗口中点击 Help -> Eclipse Marketplace... ...

  6. 在存放源程序的文件夹中建立一个子文件夹 myPackage。例如,在“D:\java”文件夹之中创建一个与包同名的子文件夹 myPackage(D:\java\myPackage)。在 myPackage 包中创建一个YMD类,该类具有计算今年的年份、可以输出一个带有年月日的字符串的功能。设计程序SY31.java,给定某人姓名和出生日期,计算该人年龄,并输出该人姓名、年龄、出生日期。程序使用YM

    题目补充: 在存放源程序的文件夹中建立一个子文件夹 myPackage.例如,在“D:\java”文件夹之中创建一个与包同名的子文件夹 myPackage(D:\java\myPackage).在 m ...

  7. 父类是在子类创建对象时候 在子类中创建一个super内存空间

    父类是在子类创建对象时候 在子类中创建一个super内存空间

  8. iOS9中怎样在日历App中创建一个随意时间之前開始的提醒(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) 四.创建随意时间之前開始的提醒 如今我们找到了指定源中的指定日 ...

  9. [Xcode 实际操作]九、实用进阶-(28)在iTunes Connect(苹果商店的管理后台)中创建一个新的新的APP

    目录:[Swift]Xcode实际操作 本文将演示如何在iTunes Connect(苹果商店的管理后台)中创建一个新的新的APP. 首先要做的是打开浏览器,并进入[iTunesConnect网站], ...

随机推荐

  1. ubuntu 12.04下apache 配置家目录地址

    apache2 最在搞前端相关的东西,上一次也记录了 Linux 下 LAMP环境的搭建,现在记录一下如果改变 apache2 的家目录地址该怎么做,改那个配置文件 修改配置文件 /etc/apach ...

  2. Android——Activity恢复用户用EditText输入的数据

    说明: 在横屏输入的内容,在Activity销毁后,即横屏后,获取用户输入的内容 步骤: 1.在xml页面定义EditText的id 2.用onSaveInstanceState保存用户输入的数据 ( ...

  3. div随页面滚动遇顶固定的两种方法(js&jQuery)

    一.遇顶固定的例子 我一直以为是某个div或层随屏幕滚动,遇顶则固定,离开浏览器顶部又还原这样的例子其实不少,其实它的名字叫“层的智能浮动效果”.目前我们在国内的商业网站上就常常看到这样的效果了.例如 ...

  4. 使ie9以下版本支持canvas,css3等主流html5技术的方法

    1.前言.   ie6,7,8支持html5,看起来比较难,其实有一种方法很通用,就是引入js和css,这种可插拔的引入对开发很有帮助.比如,下面是一个让网页支持canvas和css3的例子. 2.例 ...

  5. 解决mac休眠睡眠异常耗电方法

    备忘

  6. PyCharm中设置console端的字体和大小

    file--->setting,选择console Font,右侧primary font即设置console端的字体和大小

  7. adb shell error: more than one device and emulator

    adb shell error: more than one device and emulator 本文转载出处: http://blog.sina.com.cn/s/blog_7ffb8dd501 ...

  8. Android Studio 编写 JNI

    之前一直都不知怎么编写JNI,今天刚好学习一下,感谢梦真的指教,以及提供的文档. 参考链接 http://blog.csdn.net/u011168565/article/details/518781 ...

  9. SQL还可以这么玩儿

    对于数据库的增删改查,我们都再熟悉不过了,今天,将和您一起探讨几种不一样的SQL用法,原来,SQL还可以这么玩儿. 以下是数据库的原表,如图-1. 1.快速复制表结构 这时,如果我们需要复制一份一模一 ...

  10. IPL和SPL的区别

    IPL是英文Initial Program Loader的简称,意为初始程序的装入程序,其主要功能为负责主板.电源.硬件初始化程序.并把SPL装入RAM空间中,当IPL损坏则只能更换字库解决否则只能换 ...