找回密码
 立即注册
收起左侧

如何利用qml实现文本的选择剪切复制的功能

1
回复
7469
查看
[复制链接]

尚未签到

来源: 2015-10-21 15:37:44 显示全部楼层 |阅读模式
10Qter豆
大家好!
       目前遇到一个问题,需要利用Qml的控件来实现短信的编辑功能(主要是如何对文本内容的选择)。比如像android短信的编辑功能一样,长按弹出菜单,然后通过控制光标来选择内容。
  1. Flickable {        id: flick        anchors.left: addinfo.right        anchors.leftMargin: 20        anchors.right: parent.right        anchors.rightMargin: 20        anchors.bottom: infofriengandcommon.top        anchors.bottomMargin: 10        width: parent.width        height: textInput.height        contentWidth: textInput.contentWidth        contentHeight: textInput.contentHeight        clip: true        boundsBehavior:Flickable.StopAtBounds        flickableDirection: Flickable.VerticalFlick        onMovementStarted:start()        onMovementEnded:end()
  2.         function ensureVisible(r)        {            if (contentX >= r.x)                contentX = r.x;            else if (contentX+width <= r.x+r.width)                contentX = r.x+r.width-width;            if (contentY >= r.y)                contentY = r.y;            else if (contentY+height <= r.y+r.height)                contentY = r.y+r.height-height;        }
  3.         function start(){            pressormove = false;            console.log("==1======start============")
  4.         }
  5.         function end(){            console.log("==2======end==============")
  6.         }
  7.         TextEdit {            id: textInput//            anchors.top: flick.top//            anchors.topMargin: 10//            anchors.left: parent.left//            anchors.leftMargin: 20//            anchors.right: parent.right//            anchors.rightMargin: 20//            anchors.bottom: parent.bottom//            anchors.bottomMargin: 10            height: if( textInput.lineCount < 3) 100                    else if( textInput.lineCount*50 == 150) 150                    else 200            width: flick.width            font.pixelSize: 32            focus: false            wrapMode: Text.Wrap            onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
  8.         }
  9.         MouseArea{            id: mouid            enabled: textInput.focus === true            anchors.fill: parent            onPressed: {
  10.                 pressorhold = false;                console.log("==1======onPressed========" + onPressed)                if( Qt.inputMethod.visible === false)                {                    textInput.focus = false;                    textInput.focus =  true;                }                if(pressormove){                    textInput.cursorPosition = textInput.positionAt( mouid.mou**, mouid.mouseY)                }

  11.                 mpressx = mouid.mou**                mpressy = mouid.mouseY            }
  12.             onPositionChanged: {                if(pressorhold)                    textInput.select(textInput.positionAt(mpressx,mpressy),textInput.positionAt(mou**,mouseY))
  13.             }
  14.             onPressAndHold: {                pressorhold = true;                //toast.setMessage(qsTr("安全短信密码设置成功!"))                console.log("==1======pressorhold========" + pressorhold)
  15.                 pressandhold()            }

  16.             onCanceled:{                console.log("===2=====onCanceled========" + onCanceled)            }
  17.             onReleased: {                pressormove = true;                console.log("===2=====pressormove========" + pressormove)//                if(pressorhold){//                    pressandhold()//                }            }        }
  18.         Keys.onReleased: {            if( event.key === Qt.Key_Menu)            {                menuSignal();            }        }    }
复制代码

最佳答案

查看完整内容

帮你换了个发帖格式,代码要用这个框框起来就不会位置错乱了,不过你贴上来时候已经乱了,我就不帮你整理,你看到这里再重新编辑一下吧。 另外,看起来你的qml基础还可以的,textinput或textarea熟悉一下自带的api,你只需要多发挥一些想象力,再参考一下别的手机是怎么做的就好。
回复

使用道具 举报

尚未签到

2015-10-21 15:37:45 显示全部楼层
帮你换了个发帖格式,代码要用这个框框起来就不会位置错乱了,不过你贴上来时候已经乱了,我就不帮你整理,你看到这里再重新编辑一下吧。


另外,看起来你的qml基础还可以的,textinput或textarea熟悉一下自带的api,你只需要多发挥一些想象力,再参考一下别的手机是怎么做的就好。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了