qt - 列数可变的 QML Repeater 和 QML Grid Layout

标签 qt qml qtquick2 qtquickcontrols2

我有一个模型,它由具有某些属性的对象组成,例如:

ListModel {
    id: fruitModel

    ListElement {
        name: "Apple"
        color: "green"
        cost: 2.45
    }
    ListElement {
        name: "Orange"
        color: "orange"
        cost: 3.25
    }
    ListElement {
        name: "Banana"
        color: "yellow"
        cost: 1.95
    }
}

现在我想使用 GridLayout 显示这个模型。对于每个属性,我想在 GridLayout 中使用一个元素,例如:

GridLayout {
    columns: 3

    Text { text: "Apple" }
    Rectangle { color: "green" }
    SpinBox { value: 2.45 }

    Text { text: "Orange" }
    Rectangle { color: "orange" }
    SpinBox { value: 3.25 }

    Text { text: "Banana" }
    Rectangle { color: "yellow" }
    SpinBox { value: 1.95 }
}

关键是我可以轻松更改 GridLayoutcolumns 属性并使我的布局更窄(例如,以适应小屏幕)。我可以使用 Repeater 来填充 GridLayout。然而,这种方法将以错误的顺序填充我的 GridLayout:

GridLayout {
    columns: 3

    Repeater {
        model: fruitModel
        Text { text: name }
    }

    Repeater {
        model: fruitModel
        Rectangle { color: color }
    }

    Repeater {
        model: fruitModel
        SpinBox { value: value }
    }
}

并且使用 Layout.columnLayout.row 附加属性是一种浪费,因为我想轻松更改 GridLayout 中的列数.

有没有办法用模型中的数据逐行填充GridLayout

UPD1:

我想要得到的行为:

enter image description here

GridLayout {
    columns: parent.width > 235 ? 3 : 1

    Text { text: "Apple" }
    Rectangle { color: "green"; width: 40; height: 40 }
    SpinBox { value: 2 }

    Text { text: "Orange" }
    Rectangle { color: "orange"; width: 40; height: 40 }
    SpinBox { value: 3 }

    Text { text: "Banana" }
    Rectangle { color: "yellow"; width: 40; height: 40 }
    SpinBox { value: 1 }
}

UPD2: 来自@m7913d 的修改变体:

GridLayout {
    id: layout
    property int maxColumns: 3
    columns: parent.width > 235 ? maxColumns : 1

    Repeater {
        model: fruitModel
        Text {
            text: name
            Layout.row: layout.columns == maxColumns ? index : (maxColumns * index)
            Layout.column: 0
        }
    }

    Repeater {
        model: fruitModel
        Rectangle {
            Layout.preferredWidth: 30
            Layout.preferredHeight: 30
            color: col
            Layout.row: layout.columns == maxColumns ? index : (maxColumns * index + 1)
            Layout.column: layout.columns == maxColumns ? 1 : 0
        }
    }

    Repeater {
        model: fruitModel
        SpinBox {
            value: val
            Layout.row: layout.columns == maxColumns ? index : (maxColumns * index + 2)
            Layout.column: layout.columns == maxColumns ? 2 : 0
        }
    }
}

它的工作但不容易修改的解决方案加上有时在布局调整大小时有消息 QGridLayoutEngine::addItem: Cell (1, 0) already taken

最佳答案

我会使用由 RowColumn(或任何其他安排)填充的 Column 作为委托(delegate)。

Column {
    id: rootCol
    anchors.fill: parent
    Repeater {
        model: fruitModel
        delegate: rootCol.width > 300 ? rowDel : colDel
    }

    Component {
        id: rowDel
        Row {
            Text { width: 100; height: 50; text: model.name }
            Rectangle { width: 50; height: 50; color: model.color }
            SpinBox { width: 150; height: 50; value: model.cost }
        }
    }

    Component {
        id: colDel
        Column {
            Text { width: 100; height: 50; text: model.name }
            Rectangle { width: 50; height: 50; color: model.color }
            SpinBox { width: 150; height: 50; value: model.cost}
        }
    }
}

或者也许:

Column {
    id: rootCol
    anchors.fill: parent
    Repeater {
        model: fruitModel
        delegate: Flow {
            anchors {
                left: parent.left
                right: parent.right
            }

            Text { width: 100; height: 50; text: model.name }
            Rectangle { width: 50; height: 50; color: model.color }
            SpinBox { width: 150; height: 50; value: model.value }
        }
    }
}

关于qt - 列数可变的 QML Repeater 和 QML Grid Layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47638503/

相关文章:

qt - QPlainTextEdit - 更改 shift+return 行为

c++ - macOS 上的 Qt GUI 应用程序 : how to find the currently active screen?

python - 中央小部件的 pyqt 设计器和布局

qt - 如何在滚动时向上/向下滑动工具栏?

c++ - 如何从 QML 访问 C++ 枚举?

qt - 在 Qt Quick 中将 SVG 图像的 Canvas 大小减小到实际绘制的大小

qt - 如何在 QWidget 中插入 QML View

c++ - Qt QSplitter 窗口调整大小的固定高度小部件

javascript - for 循环中的 Qt.bound

qt - 保存 QML 图像