qml鼠标拖动_QML学习笔记(七)— 实现可拖拽、编辑、选中的ListView

ListView{

id:thelist

property bool isClicked:false //初始化没有点击事件

anchors.fill: parent

clip:trueinteractive:!isClicked

focus:trueflickableDirection: Flickable.VerticalFlick

boundsBehavior: Flickable.StopAtBounds

ScrollBar.vertical: ScrollBar {id:scrollBar;active:true;}

Keys.onUpPressed: scrollBar.decrease()

Keys.onDownPressed: scrollBar.increase()

move:Transition {

NumberAnimation{

properties:"x,y";

duration:300}

}

anchors {

left: parent.left; top: parent.top; right: parent.right;

margins:2}

spacing:4cacheBuffer:50

//model: anAirModel

model: ListModel{

id:sstpModel;

ListElement{

air_iden:"CES5401"//呼号 航班号

type:"A319"//机型

el:"07056"//机场标高 ALT-高度

status:"TAI"//管制按钮 HANDOVER-移交 CLR-放行 TAI-滑行 OFF-起飞 DESCENT-降落 DISENGAGE-脱离

etd:"1215"//预计离港时间

eta:"1413"//预计到达时间

departureTime:"1215"//departureTime 起飞时间 离港时间

arrivalTime:"1423"//arrivalTime到达时间 到港时间

ades:"ZSPD"//到达机场 目的地

takeOffRunway:"xx"//takeOffRunway 起飞跑道

landingRunway:"xx"//landingRunway 降落跑道

procedure:"xxx"//进离场程序

//tas:"K0860"//巡航速度 860km/h

//waypoint1:"TEBUN";//航路点

//assr:"6255"//应答机编码

//cvsm:"S0820"//对应高度层 82km

}

ListElement{

air_iden:"CES5401"//呼号 航班号

type:"A319"//机型

el:"07056"//机场标高

status:"HANDOVER"//管制按钮 HANDOVER-移交 CLR-放行 TAI-滑行 OFF-起飞 DESCENT-降落 DISENGAGE-脱离

etd:"1215"//预计离港时间

eta:"1413"//预计到达时间

departureTime:"1215"//起飞时间 离港时间

arrivalTime:"1423"//到达时间 到港时间

ades:"ZSPD"//到达机场 目的地

takeOffRunway:"xx"//起飞跑道

landingRunway:"xx"//降落跑道

procedure:"xxx"//进离场程序

//tas:"K0860"//巡航速度 860km/h

//waypoint1:"TEBUN";//航路点

//assr:"6255"//应答机编码

//cvsm:"S0820"//对应高度层 82km

}

ListElement{

air_iden:"CES5401"//呼号 航班号

type:"A319"//机型

el:"07056"//机场标高

status:"CLR"//管制按钮 HANDOVER-移交 CLR-放行 TAI-滑行 OFF-起飞 DESCENT-降落 DISENGAGE-脱离

etd:"1215"//预计离港时间

eta:"1413"//预计到达时间

departureTime:"1215"//起飞时间 离港时间

arrivalTime:"1423"//到达时间 到港时间

ades:"ZSPD"//到达机场 目的地

takeOffRunway:"xx"//起飞跑道

landingRunway:"xx"//降落跑道

procedure:"xxx"//进离场程序

//tas:"K0860"//巡航速度 860km/h

//waypoint1:"TEBUN";//航路点

//assr:"6255"//应答机编码

//cvsm:"S0820"//对应高度层 82km

}

ListElement{

air_iden:"CES5401"//呼号 航班号

type:"A319"//机型

el:"07056"//机场标高

status:"OFF"//管制按钮 HANDOVER-移交 CLR-放行 TAI-滑行 OFF-起飞 DESCENT-降落 DISENGAGE-脱离

etd:"1215"//预计离港时间

eta:"1413"//预计到达时间

departureTime:"1215"//起飞时间 离港时间

arrivalTime:"1423"//到达时间 到港时间

ades:"ZSPD"//到达机场 目的地

takeOffRunway:"xx"//起飞跑道

landingRunway:"xx"//降落跑道

procedure:"xxx"//进离场程序

//tas:"K0860"//巡航速度 860km/h

//waypoint1:"TEBUN";//航路点

//assr:"6255"//应答机编码

//cvsm:"S0820"//对应高度层 82km

}

ListElement{

air_iden:"CES5401"//呼号 航班号

type:"A319"//机型

el:"07056"//机场标高

status:"DESCENT"//管制按钮 HANDOVER-移交 CLR-放行 TAI-滑行 OFF-起飞 DESCENT-降落 DISENGAGE-脱离

etd:"1215"//预计离港时间

eta:"1413"//预计到达时间

departureTime:"1215"//起飞时间 离港时间

arrivalTime:"1423"//到达时间 到港时间

ades:"ZSPD"//到达机场 目的地

takeOffRunway:"xx"//起飞跑道

landingRunway:"xx"//降落跑道

procedure:"xxx"//进离场程序

//tas:"K0860"//巡航速度 860km/h

//waypoint1:"TEBUN";//航路点

//assr:"6255"//应答机编码

//cvsm:"S0820"//对应高度层 82km

}

ListElement{

air_iden:"CES5401"//呼号 航班号

type:"A319"//机型

el:"07056"//机场标高

status:"DISENGAGE"//管制按钮 HANDOVER-移交 CLR-放行 TAI-滑行 OFF-起飞 DESCENT-降落 DISENGAGE-脱离

etd:"1215"//预计离港时间

eta:"1413"//预计到达时间

departureTime:"1215"//起飞时间 离港时间

arrivalTime:"1423"//到达时间 到港时间

ades:"ZSPD"//到达机场 目的地

takeOffRunway:"xx"//起飞跑道

landingRunway:"xx"//降落跑道

procedure:"xxx"//进离场程序

//tas:"K0860"//巡航速度 860km/h

//waypoint1:"TEBUN";//航路点

//assr:"6255"//应答机编码

//cvsm:"S0820"//对应高度层 82km

}

ListElement{

air_iden:"CES5401"//呼号 航班号

type:"A319"//机型

el:"07056"//机场标高

status:"TAI"//管制按钮 HANDOVER-移交 CLR-放行 TAI-滑行 OFF-起飞 DESCENT-降落 DISENGAGE-脱离

etd:"1215"//预计离港时间

eta:"1413"//预计到达时间

departureTime:"1215"//起飞时间 离港时间

arrivalTime:"1423"//到达时间 到港时间

ades:"ZSPD"//到达机场 目的地

takeOffRunway:"xx"//起飞跑道

landingRunway:"xx"//降落跑道

procedure:"xxx"//进离场程序

//tas:"K0860"//巡航速度 860km/h

//waypoint1:"TEBUN";//航路点

//assr:"6255"//应答机编码

//cvsm:"S0820"//对应高度层 82km

}

}

delegate:Rectangle{

id:sstpDelegate

propertyint fromIndex:0propertyint toIndex:0width: parent.width

height:80MouseArea {

id:mousearea

anchors.fill: parent

onClicked: {

thelist.currentIndex=index

}

onPressed: {

thelist.currentIndex=index

sstpDelegate.fromIndex=index

thelist.isClicked= true //每项按钮点击就true

}

onReleased: {

thelist.isClicked= false //每项按钮点击就false

console.log("fromIndex:",sstpDelegate.fromIndex,"toIndex:",sstpDelegate.toIndex)

}

onPositionChanged: {var lastIndex = thelist.indexAt(mousearea.mouseX + sstpDelegate.x,mousearea.mouseY +sstpDelegate.y);if ((lastIndex < 0) || (lastIndex >sstpModel.rowCount()))return;if (index !==lastIndex){

sstpModel.move(index, lastIndex,1);

}

sstpDelegate.toIndex=lastIndex;

}

}

Row{

Rectangle{

id:curRect

width:5height: sstpDelegate.height

color: index===thelist.currentIndex ? Global.GlobalVar.sstpCurIndex : Global.GlobalVar.mainFontColor//选中颜色设置

}

Rectangle{//info

id:infoRect

width: sstpDelegate.width- controlRect.width - 5height: sstpDelegate.height

RowLayout{

spacing: Global.GlobalVar.space*4anchors.left: parent.left

anchors.leftMargin: Global.GlobalVar.space

ColumnLayout{

spacing: Global.GlobalVar.space

MyText{//航班呼号

text: air_iden

fontColor: b1

fontSize: xl

}

TextInput{//预计起飞时间

text: etd

color: Global.GlobalVar.mainFontColor

font.pointSize:12onEditingFinished: {

}

}

TextInput{//起飞时间

text: departureTime

color: Global.GlobalVar.mainFontColor

font.pointSize:12onEditingFinished: {

}

}

}

ColumnLayout{

spacing: Global.GlobalVar.space

MyText{//飞行机型

text: type

fontColor: b1

fontSize: m

}

TextInput{//预计到达时间

text: eta

color: Global.GlobalVar.mainFontColor

font.pointSize:12onEditingFinished: {

}

}

TextInput{//到达时间

text: arrivalTime

color: Global.GlobalVar.mainFontColor

font.pointSize:12onEditingFinished: {

}

}

}

ColumnLayout{

spacing: Global.GlobalVar.space*4MyText{//机场标高

text: el

fontColor: b1

fontSize: m

}

TextInput{//目的机场

text: ades

color: Global.GlobalVar.mainFontColor

font.pointSize:12onEditingFinished: {

}

}

}

ColumnLayout{

spacing: Global.GlobalVar.space*4TextInput{//起飞跑道

text: takeOffRunway

color: Global.GlobalVar.mainFontColor

font.pointSize:12}

TextInput{//进离场程序

text: procedure

color: Global.GlobalVar.mainFontColor

font.pointSize:12onEditingFinished: {

}

}

}

TextInput{//降落跑道

anchors.top: parent.top

text: takeOffRunway

color: Global.GlobalVar.mainFontColor

font.pointSize:12}

}

}

RecLine{id:recLine;direction:false}

Rectangle{

id:controlRect

width:100height: sstpDelegate.height

ColumnLayout{

anchors.horizontalCenter: parent.horizontalCenter

anchors.verticalCenter: parent.verticalCenter

BorderButton{

implicitWidth:80implicitHeight:30borderbtnText: status

fontSize:10MouseArea{

anchors.fill: parent

}

}

MyText{

anchors.horizontalCenter: parent.horizontalCenter

font.family:"FontAwesome"text:'\uf014'fontSize: xl

MouseArea{

anchors.fill: parent

onClicked: an_del.open()

}

}

}

}

}

}

IsDelDialog{id:an_del;x:SSTPWind.width/2;y:SSTPWind.height/2;}

Connections{

target: an_del.del_area;

onClicked:{//anAirModel.remove(thelist.currentIndex)

sstpModel.remove(thelist.currentIndex)

an_del.close()

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现拖拽编辑选中ListView可以提升用户交互体验,下面介绍一种实现方法。 1. 定义ListView 首先,我们需要定义一个ListView,用于显示列表项。在ListView中,我们需要指定model、delegate、highlight、onPressed、onPositionChanged等属性。 ``` ListView { id: listView model: ListModel { id: listModel } delegate: Component { id: delegate Rectangle { id: item width: listView.width height: 50 color: "white" border.width: 1 border.color: "black" Text { id: text anchors.centerIn: parent text: model.text } MouseArea { id: mouseArea anchors.fill: parent drag.target: item drag.axis: Drag.XAxis drag.minimumX: -listView.width drag.maximumX: listView.width drag.active: listView.currentIndex === index onPressed: { listView.currentIndex = index listView.positionViewAtIndex(index, ListView.Center) } } Rectangle { id: indicator width: 10 height: 10 color: "green" radius: width / 2 visible: listView.currentIndex === index anchors { right: parent.right verticalCenter: parent.verticalCenter margins: 10 } } } } highlight: Rectangle { color: "lightgray" width: listView.width height: 50 border.width: 1 border.color: "black" } onPositionChanged: { if (listView.moving) return; var index = listView.indexAt(listView.contentItem.x, listView.contentItem.y) if (index !== -1 && index !== listView.currentIndex) { listView.currentIndex = index } } } ``` 在上面的代码中,ListView中的delegate定义了每个列表项的展示方式。我们在delegate中定义了一个MouseArea,用于实现拖拽的功能。在MouseArea的drag.target属性中,我们指定了可拖拽的目标为列表项item。设置drag.axis为Drag.XAxis,表示只能在水平方向上拖拽。设置drag.minimumX为-listView.width,drag.maximumX为listView.width,表示只能在ListView的宽度范围内拖拽。在onPressed事件中,我们设置了当前列表项为选中状态,并将其居中展示。 2. 定义编辑界面 为了实现编辑功能,我们需要定义一个编辑界面。在编辑界面中,用户可以修改列表项的文本内容。我们可以使用一个Dialog来实现编辑界面。 ``` Dialog { id: editDialog title: "Edit Item" modal: true width: 400 height: 200 visible: false TextField { id: textField anchors.centerIn: parent width: parent.width - 40 placeholderText: "Enter item text" } Button { text: "OK" onClicked: { var index = listView.currentIndex if (index !== -1) { listModel.setProperty(index, "text", textField.text) } editDialog.close() } enabled: textField.text !== "" anchors { right: parent.right bottom: parent.bottom margins: 10 } } Button { text: "Cancel" onClicked: editDialog.close() anchors { left: parent.left bottom: parent.bottom margins: 10 } } } ``` 在上面的代码中,我们定义了一个Dialog,其中包含一个TextField和两个Button。用户可以在TextField中输入新的文本内容,然后点击OK按钮保存修改,或点击Cancel按钮取消修改。 3. 实现选中效果 为了让用户能够明显地看到当前选中的列表项,我们可以在每个列表项上添加一个指示器。在ListView的delegate中,我们定义了一个Rectangle,用于显示当前选中的列表项。在Rectangle的visible属性中,我们使用listView.currentIndex === index来判断当前列表项是否被选中。 4. 实现编辑功能 在ListView中,我们可以通过双击或长按来触发编辑功能。在ListView的delegate中,我们为item和mouseArea分别定义了onDoubleClicked和onLongPressed事件。在onDoubleClicked事件中,我们弹出编辑界面,让用户可以修改当前选中的列表项的文本内容。在onLongPressed事件中,我们弹出上下文菜单,让用户可以选择删除当前选中的列表项。 ``` MouseArea { id: mouseArea anchors.fill: parent drag.target: item drag.axis: Drag.XAxis drag.minimumX: -listView.width drag.maximumX: listView.width drag.active: listView.currentIndex === index onPressed: { listView.currentIndex = index listView.positionViewAtIndex(index, ListView.Center) } onDoubleClicked: { editDialog.visible = true textField.text = model.text textField.selectAll() } onLongPressed: { var menu = Menu { MenuItem { text: "Delete" onTriggered: listModel.remove(index) } } menu.popup() } } ``` 在上面的代码中,我们为mouseArea分别定义了onDoubleClicked和onLongPressed事件。在onDoubleClicked事件中,我们弹出编辑界面editDialog,并将当前选中的列表项的文本内容显示在TextField中。在onLongPressed事件中,我们弹出上下文菜单Menu,其中包含一个MenuItem,用于删除当前选中的列表项。 5. 实现拖拽功能 在ListView中,我们可以通过拖拽来调整列表项的顺序。在ListView的delegate中,我们为item定义了onDropped事件。在onDropped事件中,我们交换拖拽的两个列表项的位置。 ``` Rectangle { id: item width: listView.width height: 50 color: "white" border.width: 1 border.color: "black" Text { id: text anchors.centerIn: parent text: model.text } MouseArea { id: mouseArea anchors.fill: parent drag.target: item drag.axis: Drag.XAxis drag.minimumX: -listView.width drag.maximumX: listView.width drag.active: listView.currentIndex === index onPressed: { listView.currentIndex = index listView.positionViewAtIndex(index, ListView.Center) } onDropped: { if (drag.source !== drag.target) { var fromIndex = drag.source.index var toIndex = drag.target.index listModel.move(fromIndex, toIndex, 1) } } } Rectangle { id: indicator width: 10 height: 10 color: "green" radius: width / 2 visible: listView.currentIndex === index anchors { right: parent.right verticalCenter: parent.verticalCenter margins: 10 } } } ``` 在上面的代码中,我们为mouseArea定义了onDropped事件。在onDropped事件中,我们使用ListModel的move方法来交换拖拽的两个列表项的位置。 至此,我们已经实现了可拖拽编辑选中ListView。这个ListView可以让用户方便地调整列表项的顺序,同时也可以让用户快速修改列表项的文本内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值