【问题标题】:QML listview reading current and touch areaQML listview读取当前和触摸区域
【发布时间】:2012-03-11 03:46:22
【问题描述】:

我有这个问题。我有从 QT 代码填充的列表视图,但我希望列表视图是可触摸的,所以当我单击时,我实际上可以选择某些项目。现在我可以使用上下箭头进行导航,但是当我将它移植到 Android 时它不起作用。我尝试添加鼠标区域,但无法使其与 listview 一起使用。所以这是第一个问题,第二个问题是关于获得选定项目。因此,当我单击列表中的某个项目时,我想从中读取数据。

这是我的一段代码,如果您需要更多,请告诉我:

ListView {
         id: listview1
         x: 0
         y: 82
         width: 395
         height: 942
         visible: true
         keyNavigationWraps: false
         boundsBehavior: Flickable.DragAndOvershootBounds
         opacity: 1
         maximumFlickVelocity: 2500
         anchors.leftMargin: 0
         highlightMoveSpeed: 489
         contentWidth: 0
         preferredHighlightEnd: 2
         spacing: 5
         highlightRangeMode: ListView.NoHighlightRange
         snapMode: ListView.SnapToItem
         anchors.bottomMargin: 0
         anchors.rightMargin: 179
         anchors.topMargin: 82
              anchors.fill: parent
              model: myModel
              delegate:Component {
                 // id: contactDelegate
                  Item {
                      property variant myData: model
                      width: 450; height: 90
                      Column {
                          Text { text: '<b>ID: </b> ' + id_korisnika ; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 8 }
                          Text { text: '<b>Ime: </b> ' + ime_korisnika ; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 8 }
                          Text { text: '<b>Prezime: </b> ' + prezime_korisnika; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 8 }
                          Text { text: '<b>Broj telefona: </b> ' + broj_korisnika ; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 8 }
                          Text { text: '<b>Adresa: </b> ' + adresa_korisnika ; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 8 }
                      }
                  }
              }
             // delegate: contactDelegate
              highlight: Rectangle {color:"black"; radius: 5; opacity: 0.7 }
              focus: true

          }

          BorderImage {
              id: border_image1
              x: 0
              y: 0
              width: 501
              height: 81
              source: "slike/naslov.png"

              MouseArea {
                  id: mouse_area1
                  x: 0
                  y: 81
                  anchors.fill: parent
              }
          }

          Rectangle {
              id: rectangle1
              x: 395
              y: 82
              width: 100
              height: 718
              gradient: Gradient {
                  GradientStop {
                      position: 0
                      color: "#f3eeee"
                  }

                  GradientStop {
                      position: 1
                      color: "#621717"
                  }
              }

              Image {
                  id: image1
                  x: 2
                  y: 314
                  width: 100
                  height: 100
                  source: "slike/Button-Next-icon.png"

              }
          }

          //dodaj korisnika
          Flipable {
              id: flipable
              x: 401
              y: 98
              width: 87
              height: 70
              property bool flipped: false

                   front: Image { x: 0; y: 0; width: 60; height: 58;  source: "slike/dodaj_korisnika.png"; anchors.centerIn: parent }
                   back: Image { x: 0; y: 0; width: 60; height: 58; source: "slike/accept.png"; anchors.centerIn: parent }

                   transform: Rotation {
                       id: rotation
                       origin.x: flipable.width/2
                       origin.y: flipable.height/2
                       axis.x: 1; axis.y: 0; axis.z: 0     // set axis.y to 1 to rotate around y-axis
                       angle: 0    // the default angle
                   }

                   states: State {
                       name: "back"
                       PropertyChanges { target: rotation; angle: 180 }
                       when: flipable.flipped
                   }

                   transitions: Transition {
                       NumberAnimation { target: rotation; property: "angle"; duration: 500 }
                   }

                   MouseArea {
                       x: 7
                       y: 9
                       width: 73
                       height: 76
                       anchors.rightMargin: 7
                       anchors.leftMargin: 7
                       anchors.topMargin: 9
                       anchors.bottomMargin: 9
                       hoverEnabled: false
                       anchors.fill: parent
                       onClicked: {
                           if(Varijabla.unos_korisnika==1)
                           {   window.state == 'State1' ? window.state = "" : window.state = 'State1'; flipable.flipped = !flipable.flipped; Varijabla.povecaj() }
                           else if(Varijabla.unos_korisnika==2)
                           {
                               window.state == "" ? window.state = 'State1' : window.state = ""; flipable.flipped = !flipable.flipped; Varijabla.reset();

                              funkcije.dodaj_korisnika(text_input1.text,text_input2.text,text_input3.text,text_input4.text);
                           }
                   }
                }

这些是可用的文本变量:

    TextInput {
        id: text_input1
        x: 59
        y: 131
        width: 80
        height: 20
        font.pixelSize: 12
        opacity: 0
    }

    TextInput {
        id: text_input3
        x: 59
        y: 246
        width: 80
        height: 20
        font.pixelSize: 12
        opacity: 0
    }

    TextInput {
        id: text_input4
        x: 59
        y: 305
        width: 80
        height: 20
        font.pixelSize: 12
        opacity: 0
    }

    TextInput {
        id: text_input2
        x: 59
        y: 190
        width: 80
        height: 20
        font.pixelSize

: 12
    opacity: 0
}

【问题讨论】:

    标签: c++ qt list qml


    【解决方案1】:

    委托内部的MouseArea 应该可以正常工作,在您的ListView 中声明一个信号并从委托/MouseArea 发出它。您可以发出任意数据,但在大多数情况下,模型索引就足够了:

    ListView {
        signal itemSelected(int index)
        delegate: Rectangle {
            MouseArea {
                anchors.fill: parent
                onClicked: itemSelected(model.index)
            }
        }
    }
    

    【讨论】:

    • 如何将其放入我的代码中?我的鼠标区域应该在列表视图中的项目区域上吗?你能告诉我如何填充数据吗? text_input1 与所选项目?我的意思是你给我展示了很好的例子,但你能在上面的例子中给我展示吗?
    • 您可以在Component.onCompleted 中连接list1.itemSelected.connect(fillTextInput1),其中function fillTextInput1(index) 将具有text_input1.text = list1.model.get(index).name。或者你可以把你的代码放在ListView{onItemSelected:{ ... }}
    猜你喜欢
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 2017-12-24
    • 2016-03-12
    相关资源
    最近更新 更多