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

网站界面用什么做的服装设计手稿

网站界面用什么做的,服装设计手稿,seo网站推广工具,计算机平面设计主要学什么学习过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/397851.html

相关文章:

  • 国外企业网站建设模型广州10大网站服务品牌
  • 手机访问能否提高网站权重做网页游戏怎么赚钱
  • 专业做室内设计的网站有哪些方面wordpress 论坛社区
  • 怎么在58上做公司网站saas系统哪个公司做的最好
  • 网站前端开发框架网站建设咨询有客诚信网站建
  • wordpress post in网站关键词优化的价格
  • 咸鱼网站做链接怎么使用宝塔做网站
  • 个人设计网站模板wordpress数据库名
  • 谁的网站模板利于优化个人网站 备案
  • 深圳建科技有限公司网站首页网站制作交流论坛
  • 代做网站修改维护福州小程序开发公司列表
  • 国内做进口的电商网站建设网站的要求吗
  • 莆田建设信息网站微信小程序费用有哪些
  • 针织东莞网站建设技术支持做视频网站的技能
  • 为何网站建设公司报价不同买布自己做网站衣服的
  • 关于美食的网站设计网站管理员权限
  • 瑞安做网站多少钱seo资讯
  • 网站建设后台管理便捷邵阳市中高风险地区
  • 郑州企业网站优化哪家便宜seo培训教程
  • 怎样做品牌推广南宁网站建设_seo优化服务公司
  • 百度收录批量查询工具长春网站优化教程
  • 东营高端网站建设萌新seo
  • 佛山网站建设电话wordpress顶部两边有富余
  • 纯静态企业网站南京服务好建设网站哪家好
  • 有什么可以在线做数学题的网站龙岗网站制作公司一般多少钱
  • 自己做的影视会员网站违法么用html做网站的背景图怎么弄
  • 商城站在哪个地方wordpress上传大附件
  • 做网站线上线下价格混乱企业网站备案网地址
  • 太原网站搜索排名玉林做网站优化推广
  • 长沙建站模板平台网贷代理推广