【问题标题】:QML How to dynamically access ListView itemsQML如何动态访问ListView项目
【发布时间】:2016-09-07 20:26:39
【问题描述】:

我有一个动态添加项目的 ListView,我有点想知道如何通过索引访问项目。具体来说,我希望在使用颜色对话框更改颜色时更改项目矩形的颜色。我猜应该可以在调用颜色对话框之前先为当前项目设置一个变量,然后在 onAccepted 方法中使用该变量更改该项目的颜色,但我不知道如何实现这一点QML,因为我对 QML 比较陌生。当颜色对话框关闭时(onAccepted),也许您可​​以提供一种更简洁的方法来更改项目矩形的颜色。感谢您的帮助! :)

import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Controls.Styles 1.4

Rectangle {
    id: listViewContainer
    width: parent.width/10*9;
    height: 50
    Behavior on height {
        NumberAnimation {
            duration: 100;
        }
    }

    gradient: Gradient {
        GradientStop {position: 0.0; color: "white" }
        GradientStop {position: 1.0; color: "silver" }
    }
    radius: 5
    ColorDialog {
        id: colorDialog
        title: "Please choose a color"
        onAccepted: {
            console.log("You chose: " + colorDialog.color)
            Qt.quit()
        }
        onRejected: {
            console.log("Canceled")
            Qt.quit()
        }
    }

    Component {
        id: playerDelegate
        Item {
            anchors.left: parent.left
            anchors.right: parent.right
            height: 50
            Column {
                Text { text: '<b>Name:</b> ' + name }
                Row {
                    MouseArea {
                        width: 20
                        height: 20
                        onClicked: {
                            colorDialog.visible = true;
                            playerColor = colorDialog.color;
                            //open color dialog
                        }

                        Rectangle {
                            radius: 3
                            anchors.fill: parent
                            color: playerColor
                        }
                    }
                }
            }
        }
    }

    ListView {
        id: playerListView
        anchors.fill: parent
        model:
            ListModel {
                id: playerListViewModel;
                ListElement {
                    name: "Bill Smith"
                    playerColor: "red"
                }
        }
        delegate: playerDelegate
    }
    Button {
        id: addPlayerButton
        anchors.top: playerListView.bottom
        anchors.left: playerListView.left
        anchors.right: playerListView.right
        style: ButtonStyle {
            label: Text {
                text: "Add new player"
                horizontalAlignment: Text.Center
            }
        }
        onClicked: {
            root.addnewPlayer(playerListView); //dont worry about this method
            playerListViewModel.append({name: "Billy", playerColor: "blue"});
            listViewContainer.height += 50;
        }
    }
}

【问题讨论】:

  • 运行您的示例时出现错误:file:///tmp/untitled3/main.qml:9: TypeError: Cannot read property of null 请发布一个完整的最小示例。我猜这段代码是从一段更大的代码中取出来的,因此它所指的父级是窗口?

标签: c++ qt listview qml


【解决方案1】:

这是制作工作 colorDialog 的可靠方法 --

在 playerDelegate 中

 Component {
   id: playerDelegate
   Item {
     anchors.left: parent.left
     anchors.right: parent.right
     height: 50
     Column {
       Text {
         text: '<b>Name:</b> ' + name
       }

      /* Object to store value from color selector */
       Item {
         id: colorSelector


           property color color: "#000000"
           onColorChanged: { playerColor = color; }

       }
/* box to display color */
       Rectangle {

         //Layout.fillWidth: true
         height: 120
         width: 120
         anchors.left: button.right
           //Layout.alignment: Qt.AlignHCenter
         color: colorSelector.color
         border.width: 1
         border.color: "#000000"

       }
/* button to open dialog -- can be mousearea or other clickable object */

       Button {
         id: button
         text: "Browse..."
         onClicked: {
           colorDialog.color = colorSelector.color
           colorDialog.open()
         }
       }

/* actual color dialog for this delegate */




       ColorDialog {
         id: colorDialog
         modality: Qt.ApplicationModal
         title: "Please choose a color"
         onAccepted: colorSelector.color = currentColor
       }
     }
   }
 }
 





【讨论】:

  • 非常感谢你,你是天使! :D 然而,它并没有完全工作,每当我选择一种颜色时,它都不会改变矩形颜色,而是会生成以下错误消息:qml: You selected: #fdfdfd QCoreApplication::postEvent: Unexpected null receiver 关于任何想法如何解决这个问题?您还必须在连接后添加 newDialog.visible = true 以显示对话框
  • 好的,现在颜色对话框本身工作正常,但是当我选择一种颜色时,它不会在矩形中改变它。我是否必须调用任何方法来告诉 Qt 矩形的颜色是脏的?
  • 我在调试这些行时添加了:console.log(newColor); playerListViewModel.get(index).playerColor = newColor; console.log(playerListViewModel.get(index).playerColor);现在我什么都不懂了:在控制台中它说: qml: #193ca2 qml: red 所以它只是忽略了设置颜色的行?
  • 我都试过了,但没有成功。问题似乎不在 ColorDialog 中,而在函数 changeColor() 中。正如我所说,我将这些行添加到函数中:function changeColor(newColor) { console.log(newColor); playerListViewModel.get(index).playerColor = newColor; console.log(playerListViewModel.get(index).playerColor);,第一个日志报告该函数获得了正确的颜色。然后我们设置 playerColor 并在之后报告 playerColor ,但是 playerColor 是红色的,尽管我们在之前的行中将它设置为其他东西
  • 这是一个完整的 console.log:qml: You chose: #2c07b3 qml: newColor=#2c07b3 qml: After setting playerColor = newColor, playerColor=red 正如您所见,= 运算符似乎不再起作用,因为 playerColor 仍然是红色,尽管将其设置为 #2c07b3。这个bug现在真的让我很紧张,QML怎么能忽略=操作符?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
相关资源
最近更新 更多