qml学习记录

qml学习记录

  • 信号

    • 在qml中信号的使用
    •   signal func(string keyName,int index)   //在一个组件中创建一个信号
        id.func(xx,xx)  //调用
        onfunc:{ } //处理
      
        property //用关键字修饰,如果要给别的组件使用的话   
        
      
  • 布局方式

    • 网格 Grid
      •      []   []   []
            
             []   []   []             //即元素按网格状排列
        
             []   []   []
        
    • 行 Row
      •   [] [] [] [] []    //即元素按行排列
        
    • 列 column
      •   []
          []
          []                       //元素按列排列
          []
        
    • 锚 anchors
      在这里插入图片描述
      在这里插入图片描述
  • Component QML全局的组件属性.它是相对的.即在那个组件内就用于设置那个组件

    • 常用场景
      • Component.onCompleted:{ 处理 } //它在组件实列化的时候触发.

  • SwipeView 可拖动页面容器

    • 需要分页面显示时
      • interactive:false //禁止拖动

      • orientation:Qt.Vertical //设置页面拖动为垂直

  • QLatin1String(str); 构造一个QString*

    • 构造一个qlatin1string对象,该对象以长度(last-first)存储first. 范围[第一个,最后一个]必须在此Latin-1字符串对象的生存期内保持有效.如果last也是nullptr, 那么将nullptr作为first传递是安全的, 并且会产生一个空的拉丁-1字符串,如果last先于first,first是nullptr而last不是, 或者如果last-first>int_max, 则行为未定义.
  • MouseArea 鼠标进入指定区域的处理

    • 同时使用 Pressed > Released > Clicked 大于表示先触发
      • onPressed 按下就触发
      • onReleased 抬起就触发kv
      • onClicked 按下并抬起才触发
      • onPressAndHold 长按鼠标触发,未测试
    • hoverEnabled: true; 开启指针悬浮检测
      • onEntered 鼠标进入指定区域就触发
      • onExited 鼠标离开指定区域就触发
  • Rectangle 矩形

    • Color 颜色
      • color : “transparent” //透明的

      • color : “red” //红

      • color : “#00FF00” //RGB表示

    • Scale 比例.一般相对于父级
      • 默认为1.0,即不变
      • 小于1.0, 变小
      • 大于1.0, 变大
  • checkBox 按钮样式

    • checkBox 文档
    •   CheckBox {
            id: control
            text: qsTr("CheckBox")
            checked: true
      
            contentItem: Text {
                text: control.text
                font: control.font
                opacity: enabled ? 1.0 : 0.3
                color: control.down ? "#17a81a" : "#21be2b"
                verticalAlignment: Text.AlignVCenter
                leftPadding: control.indicator.width + control.spacing
            }
      
            indicator: Rectangle {
                implicitWidth: 26
                implicitHeight: 26
                x: control.leftPadding
                y: parent.height / 2 - height / 2
                radius: 3
                border.color: control.down ? "#17a81a" : "#21be2b"
      
                Rectangle {
                    width: 14
                    height: 14
                    x: 6
                    y: 6
                    radius: 2
                    color: control.down ? "#17a81a" : "#21be2b"
                    visible: control.checked
                }
            }
        }
      
  • model

    • model简介. 可以把他看作一个容器. 可以装任何数据
    • model 相关博客
    •    //数据
        model:["K1","K2","K3","K4","K5","pad","K6","K7","K8","K9","K10"]   
        
        //代理  循环处理每个数据
        delegate:  Item{                                           
                    keyName: modelData
        }
      
  • Diglog 对话框

  • qml与c++通信

                  keyName: modelData
      }
    
  • Diglog 对话框

  • qml与c++通信
    *

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现可拖拽、编辑、选中的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可以让用户方便地调整列表项的顺序,同时也可以让用户快速修改列表项的文本内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值