Qt 一种结构化布局的QML解决方案,不使用GRIEVIEW或ListVIEW,而是使用C++模型

Qt 一种结构化布局的QML解决方案,不使用GRIEVIEW或ListVIEW,而是使用C++模型,qt,qml,qt5,qtquick2,Qt,Qml,Qt5,Qtquick2,我试图想出一种方法,通过仅使用行和列,以非常结构化的方式表示某些项。我是从Qt小部件中得到这个想法的,在那里你可以用水平和垂直布局做任何事情,并且有一个漂亮的UI。问题是,我仍然希望创建我的QML项目的数据来自C++,这需要某种类型的模型。据我所知,模型只能绑定到PathView、GridView或ListView。此外,数据将根据QML UI发出的一些添加和删除操作动态更改。有没有办法在模型中使用行和列?假设您的模型将填充网格、行和列: 您可以使用这样的中继器: Repeater {

我试图想出一种方法,通过仅使用行和列,以非常结构化的方式表示某些项。我是从Qt小部件中得到这个想法的,在那里你可以用水平和垂直布局做任何事情,并且有一个漂亮的UI。问题是,我仍然希望创建我的QML项目的数据来自C++,这需要某种类型的模型。据我所知,模型只能绑定到PathView、GridView或ListView。此外,数据将根据QML UI发出的一些添加和删除操作动态更改。有没有办法在模型中使用行和列?

假设您的模型将填充网格、行和列:

您可以使用这样的中继器:

Repeater {
    model: lm
    delegate:  Rectangle {
        width: 100
        height: 100
        parent: (target === "Grid" ? grid :
                   (target === "Row" ? row :
                      (target === "Column" ? column :
                         null)))
        color: content
    }
}
这可能会给你一个警告:

或者您可以使用实例化器,它需要创建一个额外的QtObject,但不会得到任何警告

Instantiator {
    model: lm
    delegate: QtObject {
        property Item child: Rectangle {
        width: 100
        height: 100
        parent: (target === "Grid" ? grid :
                   (target === "Row" ? row :
                      (target === "Column" ? column :
                         null)))
        color: content
    }}
}
您甚至可以为一个模型条目创建多个对象

Instantiator {
    model: lm
    delegate: QtObject {
        property Item child1: Rectangle {
        width: 100
        height: 100
        parent: (target === "Row" ? row :
                   (target === "Column" ? column :
                      null)) // If "Grid" this thing will be created but not shown.
        color: content
        }

        property Item gridItem: Button {
            width: 100
            height: 30
            text: content
            onClicked: rootWin.color = content
            parent: grid
        }
    }
}

行{Repeater{model:myModel;delegate:Rectangle{width:10;height:10}}}}如果您只想在行或列中显示您的模型。如果同一个模型需要在不同的行和列下表示,该怎么办。在这一点上需要多个模型吗?是的,但这是一个重要的添加到您的列表中的东西模型只能绑定。实例化器也同样重要。感谢您花时间回复。我需要一些时间来将它整合到我的解决方案中,我会在完成后马上回到这里
Instantiator {
    model: lm
    delegate: QtObject {
        property Item child: Rectangle {
        width: 100
        height: 100
        parent: (target === "Grid" ? grid :
                   (target === "Row" ? row :
                      (target === "Column" ? column :
                         null)))
        color: content
    }}
}
Instantiator {
    model: lm
    delegate: QtObject {
        property Item child1: Rectangle {
        width: 100
        height: 100
        parent: (target === "Row" ? row :
                   (target === "Column" ? column :
                      null)) // If "Grid" this thing will be created but not shown.
        color: content
        }

        property Item gridItem: Button {
            width: 100
            height: 30
            text: content
            onClicked: rootWin.color = content
            parent: grid
        }
    }
}