【问题标题】:QML ListView won't respond on mouse clickQML ListView 不会在鼠标单击时响应
【发布时间】:2012-03-12 05:52:09
【问题描述】:

大家好,我已经尝试了几件事,但我的 ListView 无法响应鼠标点击。

这是我的 ListView 代码:

 ListView {
         id: listview1
         x: 0
         y: 82
        // width: 574
        // height: 967
         width: window.width
         height: window.height
         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: 0
         anchors.topMargin: 82
              anchors.fill: parent
              model: myModel
              delegate:Component {
                  //id: contactDelegate
                  Item {
                      property variant myData: model
                      width: 574; height: 90
                      Column {
                          x: 12
                          y: 0
                          width: 562
                          height: 90
                          anchors.rightMargin: 0
                          anchors.bottomMargin: 0
                          anchors.leftMargin: 12
                          anchors.topMargin: 0
                          anchors.fill: parent
                          spacing: 2
                          Text { text: '<b>ID: </b> ' + id_korisnika ; verticalAlignment: Text.AlignTop; wrapMode: Text.NoWrap; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
                          Text { text: '<b>Ime: </b> ' + ime_korisnika ; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
                          Text { text: '<b>Prezime: </b> ' + prezime_korisnika; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
                          Text { height: 16; text: '<b>Broj telefona: </b> ' + broj_korisnika ; verticalAlignment: Text.AlignVCenter; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }
                          Text { text: '<b>Adresa: </b> ' + adresa_korisnika ; horizontalAlignment: Text.AlignHCenter; color:"steelblue"; font.family: "Helvetica"; font.pointSize: 10 }

                          MouseArea {
                              id: mouse_area1
                              z: 1
                              hoverEnabled: false
                              anchors.fill: parent
                          }
                      }
                      }
              }

              //delegate: contactDelegate
              highlight: Rectangle {color:"black"; radius: 5; opacity: 0.7

              }
              focus: true

          }

我试图将我的代码放在所有领域,但我无法使其工作。任何建议都很好。

【问题讨论】:

    标签: qt listview qml


    【解决方案1】:

    参见来自 QtCreator 的 KeyInterction 示例。有你的答案:)
    您可以看到示例委托如下:


    ListViewDelegate:

    Item {
        id: container
        width: ListView.view.width; height: 60; anchors.leftMargin: 10; anchors.rightMargin: 10
    
        Rectangle {
            id: content
            anchors.centerIn: parent; width: container.width - 40; height: container.height - 10
            color: "transparent"
            antialiasing: true
            radius: 10
    
            Rectangle { anchors.fill: parent; anchors.margins: 3; color: "#91AA9D"; antialiasing: true; radius: 8 }
        }
    
        Text {
            id: label
            anchors.centerIn: content
            text: "List element " + (index + 1)
            color: "#193441"
            font.pixelSize: 14
        }
    
        MouseArea {
            id: mouseArea
            anchors.fill: parent
            hoverEnabled: true
    
            onClicked: {
                container.ListView.view.currentIndex = index
                container.forceActiveFocus()
            }
        }
    }
    

    关键是MouseArea 部分。 祝你好运 - S.M.Mousavi

    【讨论】:

    • index 来自哪里?
    • 这是一个代表。 index 是在委托上预定义的。
    • container 如何访问ListView?即使我的委托位于单独的 qml 文件中,我也使用它使我的 ListView 工作,但这仍然有效!
    • 您可以使用ListView从委托访问ListView(例如ListView.view.width
    【解决方案2】:

    我没有在您的 MouseArea (mouse_area1) 中看到 onClicked 处理程序代码。您如何尝试捕捉/响应鼠标点击。

    试试下面的代码,看看会发生什么。

    MouseArea {
        id: mouse_area1
        z: 1
        hoverEnabled: false
        anchors.fill: parent
    
        onClicked:{
            console.log("test");
        }
    }
    

    【讨论】:

    • 这似乎有效,我如何将这个突出显示部分放在 onclick 处理程序中?
    • 我不确定,但我认为您需要设置 currentIndex。并且还需要设置 highlightFollowsCurrentItem: true
    • 嘿@user123_456,如果您无法获得正确和充分的信息,则不应将响应签署为正确答案。我有同样的问题。经过多次研究,我找到了解决方案,并与您分享。请参阅我的回复帖子。
    • 致投反对票的人,你能评论一下为什么我的回答被否决了吗?
    猜你喜欢
    • 2021-04-02
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    相关资源
    最近更新 更多