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

网站界面用什么做的网站创建方法

网站界面用什么做的,网站创建方法,如何做企业市场调研,什么网站可以做任务挣钱的学习过qml的都知道,在qml中是无法定义“私有变量”的; 在自定义的控件内部定义的变量,在调用者外部是可以被使用,这不符合某些情况下的项目需求,那么该如何取巧的解决这个问题呢? 有的,可通过…

学习过qml的都知道,在qml中是无法定义“私有变量”的;

在自定义的控件内部定义的变量,在调用者外部是可以被使用,这不符合某些情况下的项目需求,那么该如何取巧的解决这个问题呢?

有的,可通过QtObject控件实现。

官方文档介绍:

Detailed Description

The QtObject type is a non-visual element which contains only the objectName property.

It can be useful to create a QtObject if you need an extremely lightweight type to enclose a set of custom properties.

案例场景:

首先我们新建一个MyRectangle.qml文件,在文件内实现一个自定义的简易的Rectangle控件;

内部有定义变量供外部调用者使用;如下:

// MyRectangle.qml
import QtQuick 2.0Rectangle {id: borderRectproperty int innerTopMargin: 1        // 上边距property int innerBottomMargin: 1     // 下边距property int innerLeftMargin: 1       // 左边距property int innerRightMargin: 1      // 右边距property string innerColor: "white"   // 矩形颜色property string bodercolor: "black"   // 边框颜色width: 100height: 50color: bodercolorRectangle {id: innerRectcolor: innerColorz:1anchors.fill: parent    // 填充满父类anchors.topMargin: innerTopMarginanchors.bottomMargin: innerBottomMarginanchors.leftMargin: innerLeftMarginanchors.rightMargin: innerRightMargin}
}

然后在main.qml文件内可以直接使用:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.14Window {id: rootvisible: truewidth: SCREEN_WIDTHheight: 500title: qsTr("Hello World")color: "white"MyRectangle {x: 100y: 200width: 300height: 200// 可以在调用者外部直接使用内部定义变量innerColor: "yellow"}
}

可以看到,在外部,即调用者处,直接使用了innerColor变量对矩形的颜色做了修改;

假设我们不希望矩形的内部的颜色被修改,那么,就得将innerColor变量设置为“私有变量”;这时候,QtObject就排上用用场。

在MyRectangle.qml文件内,定义QtObject控件,并将innerColor变量的定义放入到QtObject控件内。

// MyRectangle.qml
// 可通过定义QtObject控件,在控件内部定义变量,从而实现私有变量的效果                   
QtObject {                                                 id: attributes                                         property string innerColor: "white"   // 矩形颜色          
}                                                         

此时,在调用者处,已经发生了报错:

当然,其他的变量还是可以正常使用的,例如将矩形的边框颜色修改为红色(bodercolor: "red"),然后注释掉报错代码,(// innerColor: "yellow"),运行截图:

那么,在MyRectangle.qml文件内如何使用QtObject内定义的变量呢?

其实,可以直接使用QtObject的id值去使用到其内部定义的变量!

例如,在自定义控件MyRectangle被构建之后,使用QtObject的id值调用innerColor,将矩形背景颜色修改为灰色;

// MyRectangle.qml
// 在QtObject外部如果需要使用,则可以使用id直接使用
Component.onCompleted: {attributes.innerColor = "gray"  // 修改矩形颜色为灰色console.log("innerColor:", attributes.innerColor);
}

可以看出,在MyRectangle.qml文件内,我们使用attributes.innerColor确实可以对在QtObject内定义的变量做修改;而且在调用者处,也确实是修改不了innerColor变量了,innerColor实际上变成了“私有变量”。

如果说,期望在调用者处也能使用innerColor变量,这该如何处理呢?

还是有一个办法,将id值attributes通过别名方式暴露给外部使用!

在MyRectangle.qml文件内,给attributes定义别名:

// 可通过别名的方式给外部提供内部变量的父id,就可以外部使用到内部的变量的
property alias attr: attributes        

那么在调用者处,就可以直接使用attr调用到innerColor变量了;

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.14Window {id: rootvisible: truewidth: SCREEN_WIDTHheight: 500title: qsTr("Hello World")color: "white"MyRectangle {x: 100y: 200width: 300height: 200// 可以在调用者外部直接使用内部定义变量
//        innerColor: "yellow"bodercolor: "red"// 通过别名方式可使用内部的变量Component.onCompleted: {attr.innerColor = "green"    // 修改矩形颜色为绿色console.log("innerColor:", attr.innerColor);}}
}

通过使用别名方式,真的就使用到了innerColor变量了。

上述就是介绍了在qml中如何定义“私有变量”的全部过程,通过使用QtObject可以取巧定义到“私有变量”,因为qml本身并不支持定义私有变量。

最后附上MyRectangle.qml文件全部代码:

import QtQuick 2.0Rectangle {id: borderRectproperty int innerTopMargin: 1        // 上边距property int innerBottomMargin: 1     // 下边距property int innerLeftMargin: 1       // 左边距property int innerRightMargin: 1      // 右边距property string bodercolor: "black"   // 边框颜色width: 100height: 50color: bodercolor// 可通过别名的方式给外部提供内部变量的父id,就可以外部使用到内部的变量的property alias attr: attributesRectangle {id: innerRectcolor: attributes.innerColorz:1anchors.fill: parent    // 填充满父类anchors.topMargin: innerTopMarginanchors.bottomMargin: innerBottomMarginanchors.leftMargin: innerLeftMarginanchors.rightMargin: innerRightMargin}// 可通过定义QtObject控件,在控件内部定义变量,从而实现私有变量的效果QtObject {id: attributesproperty string innerColor: "white"   // 矩形颜色}// 在QtObject外部如果需要使用,则可以使用id直接使用Component.onCompleted: {attributes.innerColor = "gray"  // 修改矩形颜色为灰色console.log("innerColor:", attributes.innerColor);}
}
http://www.dtcms.com/a/537323.html

相关文章:

  • 《自动控制原理》第 3 章 线性控制系统的运动分析:3.6、3.7
  • 特征选择中的统计思维:哪些变量真的重要?
  • 项目七 使用ODL Yang UI操作流表
  • 电子商务网站怎么建料远若近网站建设
  • [CSP-S 2024] 超速检测
  • 基于MT5的K线处理逻辑
  • 河南郑州网站建设哪家公司好免费wordpress主题下载地址
  • 低空经济网络安全的政策体系构建
  • 网页设计网站规划深圳设计网站公司哪家好
  • 【Etcd 】Etcd 详解以及安装教程
  • 文交所网站建设方案饰品企业网站建设
  • 郑州网站建设市场陕西省建设工程信息网官网
  • 中国电商网站排行榜绍兴百度推广优化排名
  • 网站 用php asp源码 比较好建设部执业考试网站
  • 宜家有做自己的网站吗眼镜厂官网
  • JAVA1027抽象类;抽象类继承
  • AD22更新网表时总是显示 net with name XXX In already exists
  • 推荐一个免费的IP地址库:纯真社区版IP库
  • 4.前缀和
  • 网站开发技术 北京国内网站建设排名
  • 南通网站建设兼职中国沈阳app在哪里下载
  • MinIo纯前端使用文件上传预览
  • 学习记录-package.json的scripts添加参数的方式有那些
  • 【前端】avue组件分页勾选
  • 个人网站主页设计模板台州建网站
  • 修改网站主目录的位置云闪付当前页面设计隐私
  • 计算机图形学:【Games101】学习笔记02——变换(二维与三维、模型、视图、投影)
  • 解码固相萃取仪:如何实现复杂样品前处理的高效与重现性
  • Easyx图形库应用(直接显存操作)
  • 网站翻书效果网站建设费用 会计分录