2

我在使用加载器加载带有动态模型的动态委托时遇到问题。我能够看到相应模型的委托,但该委托中的数据未显示。单击该按钮时,收到错误为“ ReferenceError:”:名称未定义“我在下面发布了完整的示例。任何人都可以建议我是否遗漏任何东西。

import QtQuick 2.3
import QtQuick.Window 2.2

Window {
    id: mainWindow

    property int value: 0
    property variant m_Model: modelData

    visible: true
    height: 600
    width: 700


    Row{
        id: mainButtons
        spacing: 5
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.margins: 6


        Rectangle{
            id: b1
            height: 50
            width: 90
            border.width: 1
            radius: 3
            color: "skyblue"
            Text{
                anchors.centerIn: parent
                text: "Button1"
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                onClicked:{
                    listView.model=button1Model
                    value=1
                }
            }
        }
        Rectangle{
            id: b2
            height: 50
            width: 90
            border.width: 1
            radius: 3
            color: "skyblue"
            Text{
                anchors.centerIn: parent
                text: "Button2"
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true

                onClicked: {
                    listView.model=button2Model
                    //listView.delegate=b2Delegate
                    //loader.sourceComponent=b2Delegate
                    value=2
                }
            }
        }
        Rectangle{
            id: b3
            height: 50
            width: 90
            border.width: 1
            radius: 3
            color: "skyblue"
            Text{
                anchors.centerIn: parent
                text: "Button3"
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true

                onClicked: {
                    listView.model=button3Model
                    value = 3
                }
            }
        }
    }

    ListView{
        id: listView

        anchors.top: mainButtons.bottom
        anchors.left:parent.left
        anchors.margins: 3

        height: 500
        width: 600

        model: modelData

        delegate: Component{
            id: loader
            Loader{
                id: loader

                sourceComponent: {
                    switch(value)
                    {
                    case 1: {
                        return b1Delegate
                    }
                    case 2:{
                        return b2Delegate
                    }
                    case 3:{
                        return b3Delegate
                    }
                    default:
                        return listDelegate

                    }

                }
            }
        }
    }

    Component{
        id: listDelegate
        Row{

            spacing: 5
            Rectangle{
                width: 100
                height: 50
                border.width: 1
                Text{
                    anchors.centerIn: parent
                    text: name
                }
            }

            Rectangle{
                width: 100
                height: 50
                radius: 50
                border.width: 1
                Text{
                    anchors.centerIn: parent
                    text: number
                }
            }
        }

    }


    ListModel{
        id: modelData

        ListElement{
            name: "abc"
            number: "1"
        }
        ListElement{
            name: "def"
            number: "2"
        }
        ListElement{
            name: "ghi"
            number: "3"
        }
        ListElement{
            name: "jkl"
            number: "4"
        }
    }

    Component{
        id: b1Delegate

        Row{
            spacing: 5
            anchors.margins: 5

            Rectangle{
                height: 50
                width: 100
                border.width: 1
                Text{
                    anchors.centerIn: parent
                    text: SerialNO
                }

            }
            Rectangle{
                height: 50
                width: 100
                border.width: 1
                Text{
                    anchors.centerIn: parent
                    text: Name
                }

            }
            Rectangle{
                height: 50
                width: 100
                border.width: 1
                Text{
                    anchors.centerIn: parent
                    text: Marks
                }

            }
        }
    }

    ListModel{
        id: button1Model
        ListElement{
            SerialNO: "1"
            Name: "Mathametics"
            Marks: "100"
        }
        ListElement{
            SerialNO: "2"
            Name: "Science"
            Marks: "75"
        }
        ListElement{
            SerialNO: "3"
            Name: "Social"
            Marks: "80"
        }
        ListElement{
            SerialNO: "4"
            Name: "Hindi"
            Marks: "80"
        }
        ListElement{
            SerialNO: "5"
            Name: "English"
            Marks: "75"
        }

    }

    ListModel{
        id: button2Model
        ListElement{
            Name: "ABC"
        }
        ListElement{
            Name: "XYZ"
        }
        ListElement{
            Name: "PQR"
        }
        ListElement{
            Name: "IJK"
        }
    }
    ListModel{
        id: button3Model
        ListElement{
            University: "AAAAAA"
            Address: "111111"
        }
        ListElement{
            University: "BBBBBB"
            Address: "111111"
        }
        ListElement{
            University: "CCCCCC"
            Address: "111111"
        }
        ListElement{
            University: "DDDDDD"
            Address: "111111"
        }
    }

    Component
    {
        id: b2Delegate
        Row{
            spacing: 5
            anchors.margins: 5
            Rectangle{
                height: 50
                width: 100
                border.width: 1
                Text{
                    anchors.centerIn: parent
                    text: Name
                }

            }
        }
    }

    Component
    {
        id: b3Delegate
        Row{
            spacing: 5
            anchors.margins: 5

            Rectangle{
                height: 50
                width: 100
                border.width: 1
                Text{
                    anchors.centerIn: parent
                    text: University
                }

            }
            Rectangle{
                height: 50
                width: 100
                border.width: 1
                Text{
                    anchors.centerIn: parent
                    text: Address
                }

            }
        }
    }
}
4

2 回答 2

1

我认为使用一段Loader时间设置 adelegate不是一个好主意。尝试改变这个:

delegate: Component{
    id: loader
    Loader{
        id: loader

        sourceComponent: {
            switch(value)
            {
            case 1: {
                return b1Delegate
            }
            case 2:{
                return b2Delegate
            }
            case 3:{
                return b3Delegate
            }
            default:
                return listDelegate

            }

        }
    }
}

对此:

delegate: {
    switch(value)
    {
    case 1: {
        return b1Delegate
    }
    case 2:{
        return b2Delegate
    }
    case 3:{
        return b3Delegate
    }
    default:
        return listDelegate
    }
}

如果这对您没有帮助,请编辑您的答案并附上完整的示例代码。您提供的示例包含错误,我不知道由于示例太短而发生了哪些错误,哪些实际上是错误。

于 2016-05-13T06:34:10.253 回答
0

模型中的数据需要对委托的范围可见。您可以采用与 Qt Quick Controls 类似的方式执行此操作

delegate: Loader {
    property string nameData: name
    property int numberData: number

    sourceComponent: {
        switch (value) {
        case 1:
            return b1Delegate
        case 2:
            return b2Delegate
        case 3:
            return b3Delegate
        default:
            return listDelegate
        }
    }
}

那么,listDelegate会变成:

Component {
    id: listDelegate
    Row {
        spacing: 5

        Rectangle {
            width: 100
            height: 50
            border.width: 1
            Text {
                anchors.centerIn: parent
                text: nameData
            }
        }

        Rectangle {
            width: 100
            height: 50
            radius: 50
            border.width: 1
            Text {
                anchors.centerIn: parent
                text: numberData
            }
        }
    }
}

这是正确的代码(我简化了一些):

import QtQuick 2.3
import QtQuick.Window 2.2

Window {
    id: mainWindow

    property int value: 0
    property variant m_Model: modelData

    visible: true
    height: 600
    width: 700

    Row {
        id: mainButtons
        spacing: 5
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.margins: 6

        Rectangle {
            id: b1
            height: 50
            width: 90
            border.width: 1
            radius: 3
            color: "skyblue"
            Text {
                anchors.centerIn: parent
                text: "Button1"
            }
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onClicked: {
                    listView.model = button1Model
                    value = 1
                }
            }
        }
        Rectangle {
            id: b2
            height: 50
            width: 90
            border.width: 1
            radius: 3
            color: "skyblue"
            Text {
                anchors.centerIn: parent
                text: "Button2"
            }
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true

                onClicked: {
                    listView.model = button2Model
                    //listView.delegate=b2Delegate
                    //loader.sourceComponent=b2Delegate
                    value = 2
                }
            }
        }
        Rectangle {
            id: b3
            height: 50
            width: 90
            border.width: 1
            radius: 3
            color: "skyblue"
            Text {
                anchors.centerIn: parent
                text: "Button3"
            }
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true

                onClicked: {
                    listView.model = button3Model
                    value = 3
                }
            }
        }
    }

    ListView {
        id: listView

        anchors.top: mainButtons.bottom
        anchors.left: parent.left
        anchors.margins: 3

        height: 500
        width: 600

        model: modelData

        delegate: Loader {
            property string nameData: name
            property int numberData: number

            sourceComponent: {
                switch (value) {
                case 1:
                    return b1Delegate
                case 2:
                    return b2Delegate
                case 3:
                    return b3Delegate
                default:
                    return listDelegate
                }
            }
        }
    }

    Component {
        id: listDelegate
        Row {
            spacing: 5

            Rectangle {
                width: 100
                height: 50
                border.width: 1
                Text {
                    anchors.centerIn: parent
                    text: nameData
                }
            }

            Rectangle {
                width: 100
                height: 50
                radius: 50
                border.width: 1
                Text {
                    anchors.centerIn: parent
                    text: numberData
                }
            }
        }
    }

    ListModel {
        id: modelData

        ListElement {
            name: "abc"
            number: "1"
        }
        ListElement {
            name: "def"
            number: "2"
        }
        ListElement {
            name: "ghi"
            number: "3"
        }
        ListElement {
            name: "jkl"
            number: "4"
        }
    }

    Component {
        id: b1Delegate

        Row {
            spacing: 5
            anchors.margins: 5

            Rectangle {
                height: 50
                width: 100
                border.width: 1
                Text {
                    anchors.centerIn: parent
                    text: SerialNO
                }
            }
            Rectangle {
                height: 50
                width: 100
                border.width: 1
                Text {
                    anchors.centerIn: parent
                    text: Name
                }
            }
            Rectangle {
                height: 50
                width: 100
                border.width: 1
                Text {
                    anchors.centerIn: parent
                    text: Marks
                }
            }
        }
    }

    ListModel {
        id: button1Model
        ListElement {
            SerialNO: "1"
            Name: "Mathametics"
            Marks: "100"
        }
        ListElement {
            SerialNO: "2"
            Name: "Science"
            Marks: "75"
        }
        ListElement {
            SerialNO: "3"
            Name: "Social"
            Marks: "80"
        }
        ListElement {
            SerialNO: "4"
            Name: "Hindi"
            Marks: "80"
        }
        ListElement {
            SerialNO: "5"
            Name: "English"
            Marks: "75"
        }
    }

    ListModel {
        id: button2Model
        ListElement {
            Name: "ABC"
        }
        ListElement {
            Name: "XYZ"
        }
        ListElement {
            Name: "PQR"
        }
        ListElement {
            Name: "IJK"
        }
    }
    ListModel {
        id: button3Model
        ListElement {
            University: "AAAAAA"
            Address: "111111"
        }
        ListElement {
            University: "BBBBBB"
            Address: "111111"
        }
        ListElement {
            University: "CCCCCC"
            Address: "111111"
        }
        ListElement {
            University: "DDDDDD"
            Address: "111111"
        }
    }

    Component {
        id: b2Delegate
        Row {
            spacing: 5
            anchors.margins: 5
            Rectangle {
                height: 50
                width: 100
                border.width: 1
                Text {
                    anchors.centerIn: parent
                    text: Name
                }
            }
        }
    }

    Component {
        id: b3Delegate
        Row {
            spacing: 5
            anchors.margins: 5

            Rectangle {
                height: 50
                width: 100
                border.width: 1
                Text {
                    anchors.centerIn: parent
                    text: University
                }
            }
            Rectangle {
                height: 50
                width: 100
                border.width: 1
                Text {
                    anchors.centerIn: parent
                    text: Address
                }
            }
        }
    }
}
于 2016-05-13T11:51:23.713 回答