Qt/QML学习-GridView

QML学习

main.qml

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("GridView")

    ListModel {
        id: model
        ListElement { color: "red"; url: "MyCompnent1.qml" }
        ListElement { color: "yellow"; url: "MyCompnent2.qml" }
        ListElement { color: "green"; url: "MyCompnent3.qml" }
        ListElement { color: "red"; url: "MyCompnent1.qml" }
        ListElement { color: "yellow"; url: "MyCompnent2.qml" }
        ListElement { color: "green"; url: "MyCompnent3.qml" }
        ListElement { color: "red"; url: "MyCompnent1.qml" }
        ListElement { color: "yellow"; url: "MyCompnent2.qml" }
        ListElement { color: "green"; url: "MyCompnent3.qml" }
    }

    Component {
        id: delegate
        Item {
            width: gridView.cellWidth - 20
            height: gridView.cellHeight - 20
            Component.onCompleted: createQml(this, model.url, model.color)
        }
    }

    function createQml(parent, url, color) {
        var myComponent = Qt.createComponent(url)
        if (myComponent.status === Component.Ready) {
            myComponent.createObject(parent, {
                color: color
            });
        }
    }

    GridView {
        id: gridView
        anchors.fill: parent
        model: model
        delegate: delegate
        cellWidth: height / 2
        cellHeight: height / 2
        flow: GridView.FlowTopToBottom
    }
}

MyCompnent.qml

import QtQuick 2.15

Rectangle {
    anchors.fill: parent
    Text {
        text: "GridView"
        anchors.centerIn: parent
        font.pointSize: 20
    }
}

演示

相关推荐

  1. Qt/QML学习-GridView

    2024-07-12 11:36:06       19 阅读
  2. Android studio GridView应用设计

    2024-07-12 11:36:06       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 11:36:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 11:36:06       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 11:36:06       58 阅读
  4. Python语言-面向对象

    2024-07-12 11:36:06       69 阅读

热门阅读

  1. bug定位策略

    2024-07-12 11:36:06       21 阅读
  2. 【React】监听浏览器返回事件

    2024-07-12 11:36:06       26 阅读
  3. 【C语言】高低字节的分分合合 !

    2024-07-12 11:36:06       32 阅读
  4. Pip: Python的包管理器

    2024-07-12 11:36:06       26 阅读
  5. spring 中的路径匹配

    2024-07-12 11:36:06       15 阅读
  6. 【linux服务器ssl证书过期替换】

    2024-07-12 11:36:06       16 阅读
  7. python使用python-docx库处理图片白框问题

    2024-07-12 11:36:06       21 阅读
  8. 力扣刷题35.搜索查找位置

    2024-07-12 11:36:06       17 阅读
  9. C#面 :请列举官方常用的中间件?

    2024-07-12 11:36:06       22 阅读