首页 > 其他分享 >QML动态创建组件

QML动态创建组件

时间:2023-01-05 13:33:55浏览次数:58  
标签:anchors parent 动态创建 height width QML 组件 id row


​​QML 中如何动态创建组件_billy的博客

​​qml动态创建模型及代理中的代理_蓝博皙的博客-

​QML ListView 拖拽移动,代理和模型 - Cpper - C++博客​

​20.Quick QML-Flickable滑动窗口 - 云+社区 - 腾讯云​

​​Qml Flickable_quietbxj的博客


MyItem.qml

import QtQuick 2.15
import QtQuick.Controls 2.15

Rectangle {
id: rec

property alias textInformation: myText.text
signal destroyMyself(var object)

Text {
id: myText
anchors.left: parent.left
anchors.leftMargin: 10
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 24
font.family: "微软雅黑"
color: "black"
}

Button {
anchors.right: parent.right
anchors.rightMargin: 10
anchors.verticalCenter: parent.verticalCenter
text: "delete"

onClicked: {
destroyMyself(rec)
}
}
}

model.qml

import QtQuick 2.2

Item {
width: 200
height: 50

ListModel {
id: myModel
ListElement {
type: "Dog"
age: 8
cnt: 3
}
ListElement {
type: "Cat"
age: 5
cnt: 5
}
}

Component {
id: myDelegate

Flickable {
width: listView.width
height: row.height
contentWidth: row.width
contentHeight: row.height

Row {
id: row
spacing: 2

Text {
text: type + ", " + age
font.pointSize: 12
}
Component.onCompleted: {
for (var i = 0; i < cnt; ++i) {
createObject(row)
}
}
}
}
}

ListView {
id: listView

spacing: 5
anchors.fill: parent
model: myModel
delegate: myDelegate
}

Component.onCompleted: {
console.log("ListModel", myModel)
console.log("Component", myDelegate)
}

function componentDestroy(object) {
object.destroy()
}

function createObject(parent) {
// createComponent from external file "MyItem.qml"
var component = Qt.createComponent("MyItem.qml")
if (component.status === Component.Ready) {
var obj = component.createObject(parent, {
"color": "yellow",
"width": 150,
"height": 150,
"textInformation": 'x'
})
}
}

function createQmlObject(parent) {
var newObject = Qt.createQmlObject(
'import QtQuick 2.0; Rectangle {color: "blue"; width: 1024; height: 50}',
parent, "")
}
}

标签:anchors,parent,动态创建,height,width,QML,组件,id,row
From: https://blog.51cto.com/u_15930680/5990926

相关文章

  • QML/JS/C++
    ​​【QML快速入门】QML类型-fengMisaka​​在Qt C++中调用QML中的Javascript Function_青山绿水北京爷_新浪博客​​​​C++调用QML中的函数-xianyongchao​​C++......
  • Vue3中操作子组件实例
    子组件Child.vue<template><hr/>{{INFO}}<hr/><button@click="changeInfo">changeInfo</button></template><scriptsetuplang="ts">import{ref,r......
  • 那些年我们用过的组件-结构化日志组件 Serilog
    什么是结构化日志我们记录日志惯常使用log4j2、NLog等日志组件,这些组件提供了输出到多种终端的能力,但是大部分时候我们选择将日志输出到操作系统的文件系统中,为什么呢?至......
  • ExtJS-UI组件-TreePanel
    ExtJS教程汇总:https://www.cnblogs.com/cqpanda/p/16328016.html转载请注明出处:https://www.cnblogs.com/cqpanda/p/16587500.html更新记录2023年1月2日从笔记迁移到......
  • jango框架:forms组件渲染标签、forms组件展示信息、forms组件校验补充、forms组件参数
    目录forms组件渲染标签forms组件展示信息forms组件校验补充钩子函数forms组件参数补充forms组件源码剖析modelform组件基本使用classMeta下常用参数save()方法forms组件......
  • 组件,父子组件的传值
    组件化开发组件化开发,指的是,根据封装的思想,把页面上,可以重用的部分分装成为组件,从而更方便项目开发和维护一个页面,可以拆分成一个个组件,一个个组件是一个整体,每个组件可......
  • Vue学习-基础-尚硅谷vue-todolist学习-组件开发入门
    1,编写静态组件 :抽取组件,使用组件实现静态页面效果   这里组件中的component   style  部分使用的静态页面粘贴过来的  , 这里的MyItem是MyList......
  • MySQL event事件,定时按年份动态创建表
    参考资料:1、MySQL事件(定时任务):https://blog.51cto.com/u_15549234/5138457;2、mysql创建存储过程语法(MySQL创建存储过程sql语句):https://www.gaojipro.com/a/108616;3、my......
  • uniapp 用 uView 组件库中的u-picker 实现地区的 省-市-区 三级联动
    组件的引入就不多赘述了直接看使用方法地址我是引入的json文件数据结构大概是这个样子例1 例2   1,先做个按钮做弹窗显示<u-cell:border="true">......
  • 小程序组件和小程序插件的差别是什么?
    一直以为小程序组件和小程序插件是一回事,只是措辞不一样,导致造成乌龙,其实完全是两回事,插件是可以直接提供服务的,组件是给开发者提供的轮子,不能直接提供服务。先看看微信是如......