简体   繁体   English

QML 命名混乱

[英]QML naming confussion

I have a basic question about naming components in QML files.我有一个关于在 QML 文件中命名组件的基本问题。 I have read that the top element should always get the id root and parent is always the reference to the next element above it.我已经读到,顶部元素应该始终获得 id,而元素始终是对其上方下一个元素的引用。

I have a two qml files, one with a ListView and one with the listview delegate我有两个 qml 文件,一个带有ListView ,一个带有 listview 委托

Userlogon.qml用户登录.qml

Item {
    id: root
    height: parent.height
    width: parent.width
    property var password: ['0', '1', '2', '3']
    property int selectedField : mill.selectedIndex
    property int selectedUser : 0
    property string p_background: configuration.getColor(Colors.ContentBackground)

    ColumnLayout { anchors.fill: parent
        Rectangle { id: userlist; Layout.fillWidth: true; Layout.fillHeight: true; Layout.preferredHeight: 300; color: p_background
            ColumnLayout { anchors.fill: parent
                ListView { Layout.fillWidth: true;  Layout.fillHeight: true
                    model: user.model
                    currentIndex: 1
                    onCurrentIndexChanged: { console.log("currentIndex changed") }
                    header: UserItemDelegate { p_index: -1; p_name: "Benutzeranmeldung"; p_icon: "password"; p_isHeader: true }
                    delegate: UserItemDelegate { p_index: index; p_name: name; p_icon: icon; p_isHeader: false }
                    spacing: 20
                }
            }
        }

UserItemDelegate.qml UserItemDelegate.qml

Item {
    id: root
    height: configuration.getSize(Sizes.ListItemHeight)
    width: parent.width
    property int p_index
    property string p_name
    property string p_icon
    property bool p_isHeader
    property bool p_isSelected: root.ListView.view.currentIndex == p_index
    property string p_color: configuration.getColor(p_isHeader ? Colors.ListItemDisabled : (p_isSelected ? Colors.ListItemSelected : Colors.ListItemDefault)) 
    
    Rectangle { anchors.fill: parent; Layout.fillWidth: true; Layout.fillHeight: true; color: p_color
        RowLayout { anchors.verticalCenter: parent.verticalCenter; 
            Image { Layout.leftMargin: 10; sourceSize.height: root.height * 0.6; source: "image://iconprovider/" + p_icon }
            Label { Layout.leftMargin: 10; text: p_name }
        }
        MouseArea{ enabled: !p_isHeader; anchors.fill: parent; onClicked: { root.ListView.view.currentIndex = p_index; } } 
    }
}

With root.Listview.view.currentIndex I can access the listview in the parent UserLogon.qml although root is the id of the current item?使用root.Listview.view.currentIndex我可以访问父UserLogon.qml中的列表视图,尽管root是当前项目的 ID?

And is it possible to access eg a timer defined in UserLogon.qml from the delegate.是否可以从委托访问 UserLogon.qml 中定义的计时器。 If so how would the referencing be?如果是这样,引用将如何?

First of all, you don't need to name every root object "root".首先,您不需要将每个根 object 命名为“root”。 I don't know where you read that, but you can use whatever name makes sense to you.我不知道你在哪里读到的,但你可以使用任何对你有意义的名字。

When you call root.ListView.view.currentIndex , the root in that case is the id of the delegate, not the list.当您调用root.ListView.view.currentIndex时,这种情况下的root是委托的 ID,而不是列表。 QML's scoping rules guarantee that. QML 的范围规则保证了这一点。 The ListView.view attached property then allows you to reference back to the list without needing its id.然后, ListView.view附加属性允许您在不需要其 id 的情况下引用回列表。

Your timer question is unclear.您的计时器问题不清楚。 In general, yes you can have access to the ListView's properties from the delegate, but you should usually do it the other way around.一般来说,是的,您可以从委托访问 ListView 的属性,但您通常应该以相反的方式进行。 The ListView should reference the delegate's properties. ListView 应该引用委托的属性。 The reason is that you may have a need to use that delegate somewhere else and you don't necessarily want it tightly coupled with the ListView in that case.原因是您可能需要在其他地方使用该委托,并且在这种情况下您不一定希望它与 ListView 紧密耦合。 Here's an example:这是一个例子:

MyList.qml我的列表.qml

ListView {
    id: contactList

    Timer {
        id: backgroundTimer
        interval: 3000
        running: true
    }

    model: 5
    delegate: MyDelegate {
        width: contactList.width

        // ListView binds the delegate's 'timedOut' property to something
        timedOut: backgroundTimer.running
    }
}

MyDelegate.qml我的委托.qml

Rectangle {
    id: contact
    property bool timedOut: false

    height: 30
    radius: height / 2
    color: timedOut ? "red" : "blue"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM