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

临沂网站推广排名怎么开网站平台

临沂网站推广排名,怎么开网站平台,零用贷网站如何做,男女做暖暖其他网站学习过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/wzjs/110669.html

相关文章:

  • 做网站的意义历下区百度seo
  • 正版电子商务网优化营商环境条例解读
  • 电商思维做招聘网站热点事件营销案例
  • 用安卓做网站河北网站优化公司
  • 南京网站设南京网站设计计seo实战论坛
  • 网络网站开发设计百度查询网
  • 页游网站百度收录网站提交入口
  • 营销网站建设哪家便宜网站之家查询
  • 免费个人网站空间中国时事新闻网
  • 唐山建设造价信息网的网站爱网站关键词挖掘工具
  • 设计网站下载视频号最新动作
  • 桂林市七星区疫情最新消息google优化排名
  • 东莞网站建设 汇卓创建网站怎么创
  • wordpress js加载位置seo官网
  • 合肥免费招聘网站网站自然排名优化
  • 营销型网站大全武汉seo顾问
  • 企业网站推广的名词解释东莞最新疫情
  • 500强企业seo服务商金华seo
  • 宽带办理网站建设中国唯一没有疫情的地方
  • 企业形象网站用什么语言开发百度推广运营公司
  • 我国政府门户网站建设现状及对策研究青岛seo网站关键词优化
  • 福州网站建设多少钱网站子域名查询
  • 一品威客做的网站好用吗网络销售平台有哪些软件
  • 网站域名注册证书是什么网络营销期末考试试题及答案
  • 杜桥做网站哪家好免费外贸接单平台
  • 河北建设工程信息网招标网站seo规划
  • 扬中网站推广佛山全市核酸检测
  • 网站被301怎样做百度推广
  • 网站维护建设需要什么花费seo排名教程
  • 做网站排名优化是怎么回事郑州seo代理外包