当前位置: 首页 > news >正文

Qml学习——动态加载控件

最近在学习Qml,但对Qml的各种用法都不太熟悉,总是会搞忘,所以写几篇文章对学习过程中的遇到的东西做一个记录。
学习参考视频:https://www.bilibili.com/video/BV1Ay4y1W7xd?p=1&vd_source=0b527ff208c63f0b1150450fd7023fd8

目录

  • 1 动态加载控件
    • 1.1 用Component加载
      • 1.1.1 使用方法
  • 2 用Loader加载
    • 2.1 用法
      • 加载qml文件
      • 加载Component


1 动态加载控件

1.1 用Component加载

Component提供了createObject方法,可以在程序运行时调用,以添加控件,它的官方例程如下。

var component = Qt.createComponent("Button.qml");
if (component.status == Component.Ready)component.createObject(parent, {x: 100, y: 100});

该方法需要先把加载的控件封装到一个qml文件中,然后通过createComponent加载qml文件,如果qml文件是可用的(component.status == Component.Ready),那就用createObject来创建一个实例,参数1是实例的父控件id,参数2是实例的初始属性。

1.1.1 使用方法

下面举例说明使用流程。
创建一个Rect.qml文件。
在这里插入图片描述
在这里插入图片描述
在Rect.qml填入以下内容,定义一个长宽为30的蓝色矩形。

import QtQuick 2.0Rectangle { width: 30; height: 30; color: 'blue' }

在main,qml填入一下内容,用按钮来动态添加Rect控件到网格布局。

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12Window {visible: true; width: 200; height: 120GridLayout {id: layoutcolumns: 4}Button {anchors.bottom: parent.bottomonClicked: {let component = Qt.createComponent("Rect.qml");if (component.status == Component.Ready)component.createObject(layout);}}
}

效果:
请添加图片描述

2 用Loader加载

Qml提供的Loader类动态加载控件,以下是官方说明:

Loader可以加载QML文件(使用source属性)或Component对象(使用sourceComponent属性)。这对于将组件的创建延迟到需要时非常有用:例如,当应按需创建组件时,或者出于性能原因不应不必要地创建组件时。

它与Component加载有以下两处不一样。
1、Loader是延迟加载预先设定好的控件,并不是像Component那样可以加载任意数量的控件。
2、Loader加载的可以是Component对象或者qml文件,Component只能加载qml文件。

2.1 用法

加载qml文件

以上一节的Rect,qml为例。

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12Window {visible: true; width: 200; height: 120Loader { id: loader}Button {anchors.bottom: parent.bottomonClicked: loader.setSource("Rect.qml")}
}

请添加图片描述

加载Component

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12Window {visible: true; width: 200; height: 120Loader { id: loader }Component {id: componentRect {}}Button {anchors.bottom: parent.bottomonClicked: loader.sourceComponent = component}
}

请添加图片描述

http://www.lryc.cn/news/604.html

相关文章:

  • 设计模式之职责链模式
  • MySQL入门篇-MySQL 8.0 延迟复制
  • FPGA时序约束与分析 --- 实例教程(1)
  • go深拷贝和浅拷贝
  • linux网络系统层面的配置、管理及操作命令汇总
  • R数据分析:孟德尔随机化中介的原理和实操
  • 【C++】 类和对象 (下)
  • asp获取毫秒时间戳的方法 asp获取13位时间戳的方案
  • Python基础篇(十五)-- Python程序接入MySQL数据库
  • 程序员不得不知道的 API 接口常识
  • 【项目精选】基于Java的银行排号系统的设计与实现
  • 前端 基于 vue-simple-uploader 实现大文件断点续传和分片上传
  • 解决报错: ERR! code 128npm ERR! An unknown git error occurred
  • 聊城高新技术企业认定7项需要注意的问题 山东同邦科技分享
  • 菊乐食品更新IPO招股书:收入依赖单一地区,规模不及认养一头牛
  • Elasticsearch安装IK分词器、配置自定义分词词库
  • Linux嵌入式开发——shell脚本
  • CV【5】:Layer normalization
  • 跳跃游戏 II 解析
  • 易基因|猪肠道组织的表观基因组功能注释增强对复杂性状和人类疾病的生物学解释:Nature子刊
  • 01- NumPy 数据库 (机器学习)
  • RapperBot僵尸网络最新进化:删除恶意软件后仍能访问主机
  • 拦截器interceptor总结
  • 轻松实现微信小程序上传多文件/图片到腾讯云对象存储COS(免费额度)
  • Golang中defer和return的执行顺序 + 相关测试题(面试常考)
  • 谁说菜鸟不会数据分析,不用Python,不用代码也轻松搞定
  • php mysql保健品购物商城系统
  • Vue3电商项目实战-首页模块6【22-首页主体-补充-vue动画、23-首页主体-面板骨架效果、4-首页主体-组件数据懒加载、25-首页主体-热门品牌】
  • linux 使用
  • 基于遗传算法的微电网调度(风、光、蓄电池、微型燃气轮机)(Matlab代码实现)