【问题标题】:How to know when the selected item has been changed in a QML QListView?如何知道 QML QListView 中所选项目何时更改?
【发布时间】:2014-05-19 00:49:13
【问题描述】:

我正在使用 QtQuick 2.0 和 QML ListView 来显示一些项目,我需要知道用户何时选择了不同的项目。当用户单击委托中的鼠标区域时发出信号,即

MouseArea{
    onClicked: {
                 controller.itemChanged(model.item);
                 someList.currentIndex = index;
   }
}

但仅当用户使用鼠标选择项目时,如果用户使用箭头键则不起作用。

我一直在查看文档以查找更改 currentIndex 时发出的信号,但我似乎找不到任何信号。我正在寻找类似于 QListWidget::itemSelectionChanged() 的东西,但似乎 QML ListView 没有。

【问题讨论】:

  • 你的意思是onCurrentIndexChanged 信号?所有应通知的属性都有其on<name>Changed 属性。
  • 哇.. 不敢相信我错过了,而是绕过街区。

标签: qt listview qml pyqt5 qlistview


【解决方案1】:

您只需要在您的ListView 中添加onCurrentItemChanged:{}

【讨论】:

    【解决方案2】:

    this question。您可以采取两种方法

    1. Connect 到另一个组件的事件
    2. Handle 该组件中的事件

    signal handler 被命名为on<SignalName>,信号的第一个字母为大写。

    【讨论】:

      【解决方案3】:

      我最终不得不重新实现键盘行为并从委托中公开模型数据,以便在按下键时触发信号。

      ListView {
          id: myList
          focus: true
          orientation: "Horizontal" //This is a horizontal list
          signal itemChanged(var item)
          interactive: false //Disable interactive so we can re-implement key behaviour
          Keys.onPressed: {
                      if (event.key == Qt.Key_Left){
                          myList.decrementCurrentIndex(); //Change the current list selection
                          itemChanged(myList.currentItem.selectedItem.data); //Fire signal notifying that the selectedItem has changed
                      }
                      else if (event.key == Qt.Key_Right){
                          myList.incrementCurrentIndex(); //Change the current list selection
                          itemChanged(myList.currentItem.selectedItem.data); //Fire signal notifying that the selectedItem has changed
                      }
                  }
      
          delegate: Component {
              Rectangle {
                  id: myItem
                  property variant selectedItem: model //expose the model object
      
                  MouseArea {
                      anchors.fill: parent
                      onClicked: {
                          myList.currentIndex = index; //Change the current selected item to the clicked item
                          itemChanged(model.data); //Fire signal notifying that the selectedItem has changed
                      }
                  }
              }
          }
      }
      

      使用此解决方案,您必须在用户单击项目或按键时手动更改 QML 中的项目。我不确定这是否是 GridView 的最佳解决方案,但它适用于 ListView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多