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

政务网站建设云计算中心搜狗官方网站

政务网站建设云计算中心,搜狗官方网站,做产品推广有网站比较好的,便宜的seo官网优化在QML中可以使用现有的QML元素来创建页面,但QML紧密的集成了必要的JavaScript。 但QML中使用JavaScript比较严格,在QML中不可以添加或修改JavaScript全局对象成员,这样可能会使用一个未经声明的变量。 内联JavaScript 一些小型的JavaScript函…

在QML中可以使用现有的QML元素来创建页面,但QML紧密的集成了必要的JavaScript。

但QML中使用JavaScript比较严格,在QML中不可以添加或修改JavaScript全局对象成员,这样可能会使用一个未经声明的变量。

 

内联JavaScript

一些小型的JavaScript函数可以和其他QML声明写在QML组件中。

Item{anchors.fill:parentid:item1function text1(){  //创建一个函数console.log("新的函数运行")}MouseArea{anchors.fill: parentonPressed:{item1.text1()}}}

使用JavaScript文件:

大量的JavaScript代码需要写在一个独立的文件中。使用import 来导入到QML中

新建一个Js文件:

 点击左上角新建文件,选择Qt中的JsFile 

输入完文件名后,在文件中添加以下代码:

在qml文件中导入Js文件:

导入JS文件格式:

import "name.js" as xxxxname为文件名
xxxx为限定符,每一个JS文件的限定符必须唯一,限定符和JS之间是一对一映射

 导入JS文件:

import QtQuick 2.9
import "MyJS.js" as Logic //导入Js文件  Logic为访问Js文件的对象
import QtQuick.Window 2.2Window {visible: truewidth: 640height: 480title: qsTr("Hello World")MouseArea{anchors.fill: parentonPressed:{Logic.func()//使用函数}}}

 代码隐藏实施文件

       大多数JS文件被导入一个QML文件是有状态的,它们经常作为该QML文件的逻辑实现。这种情况下,为了使QML组件的实例有正确的行为,每一个实例都需要有JS对象和状态的一个独立的备份,导入JS文件时的默认行为时为每一个QML组件实例提供一个唯一的、独立的备份,JS代码和QML组件运行在相同的范围,因此可以访问和操作对象的声明属性。

无状态的JavaScript库 

如果JS文件中的函数只用来提供输入和计算输出,不会直接操作QML组件实例,这种可以把该文件设为无状态(以免每一个QML对象都有这些库的拷贝,造成资源浪费)

使用pragma来指明一个特定文件是无状态库。

  • pragma必须在所有有效的代码之前
  • .pragma 库文件无法直接访问 QML 组件实例对象或属性
  • QML 值可以作为函数参数传递

JS文件: 

.pragma library
function func(x) {console.log(x)
}

 QML文件:

import QtQuick 2.9
import "MyJS.js" as Logic
import QtQuick.Window 2.2Window {id:window1visible: truewidth: 640height: 480title: qsTr("Hello World")MouseArea{anchors.fill: parentonPressed:{Logic.func(window1.width)//传入window1的宽}}}

从另一个JavaScript资源导入JavaScript

使用Qt.include()函数来导入其他文件,会将所有的函数 ,导入到当前的命名空间中(目前已逐渐被弃用)

使用Qt.include()函数导入

//myJs1.jsfunction func() {console.log("func函数")
}//textJs.jsQt.include("MyJS.js")//导入MyJs.js文档
function showText() {console.log("showText函数")
}//qml
import QtQuick 2.9
import "textJs.js" as Logic //导入textJs.js文件
import QtQuick.Window 2.2Window {id:window1visible: truewidth: 640height: 480title: qsTr("Hello World")MouseArea{anchors.fill: parentonPressed:{Logic.showText()//调用showText函数Logic.func()//调用func函数}}}

在程序启动时运行JavaScript

有时需要在应用程序(或组件实例)启动时运行一些命令性代码。虽然将启动脚本作为全局代码包含在外部脚本文件中很诱人,但由于 QML 环境可能尚未完全建立,这可能会有严重的限制。例如,某些对象可能尚未创建,或者某些属性绑定可能尚未建立。

  • 使用Component元素提供的onCompleted属性,可以用来在QML环境完全建立后切换到启动脚本代码的执行。
  • 每次销毁时会发出一个destruction()信号
Rectangle{function func1(){//一些操作}Component.onCompleted: {func1()}}

属性绑定和属性赋值

  • QML中使用<属性:值>的方式来创建属性绑定 (绑定的话会跟着更新)
  • JavaScript使用<属性=值>的方式创建属性赋值  (赋值的话不会自动更新)会破环绑定

属性绑定:

Rectangle {id:rect1width: 100height: 100focus: truecolor: "red"Keys.onSpacePressed: {rect1.width-=10rect1.height-=10}}Rectangle {id:rect2x:100width: rect1.width //绑定了rect1的宽height: rect1.height//绑定了rect1的高color: "blue"}

属性赋值:

具有绑定的属性会根据需要自动更新。但是,如果稍后从 JavaScript 语句为该属性分配静态值,则将删除绑定。

Rectangle {width: 100height: width * 2color: "red"focus: trueKeys.onSpacePressed: {height = width * 3//会破坏绑定//height = Qt.binding(function() { return width * 3 })}}

 矩形的形状为:100  300

QML信号中的JavaScript

  • 使用connect()函数,将QML信号关联到JavaScript上
  • 可以在信号处理函数中直接调用JavaScript函数

使用connect()函数,将QML信号关联到JavaScript上: 

Rectangle {width: 100height: width * 2color: "red"focus: trueMouseArea{anchors.fill:parentid:mouse1}Component.onCompleted: {mouse1.clicked.connect(Logic.showText)//关联到Javascript中}}

信号处理函数中直接调用JavaScript函数:

Rectangle {width: 100height: width * 2color: "red"focus: trueMouseArea{anchors.fill:parentid:mouse1onPressed: {Logic.showText()//直接调用函数}}}

JavaScript在QML中的限制:

在文件中编写的JavaScript代码无法修改全局对象,在 QML 中,全局对象是常量 - 不能修改或删除现有属性,也不能创建新属性。

 违法操作:

a = 1;
for (var ii = 1; ii < 10; ++ii)a = a * ii;
console.log("Result: " + a);

但可以简单的修改为法典:

var a = 1;
for (var ii = 1; ii < 10; ++ii)a = a * ii;
console.log("Result: " + a);

全局代码在缩小的范围内可以使用 ,在启动期间,如果 QML 文件包含带有“全局”代码的外部 JavaScript 文件,则会在仅包含外部文件本身和全局对象的作用域中执行该文件。也就是说,它无法通常的访问 QML 对象和属性

 允许仅访问脚本局部变量的全局代码:

var colors = [ "red", "blue", "green", "orange", "purple" ];

 访问 QML 对象的全局代码将无法正常运行:

var initialPosition = { rootObject.x, rootObject.y }

 目前在QML中this值是未定义的,使用id访问

Item {width: 200; height: 100function mouseAreaClicked(area) {console.log("Clicked in area at: " + area.x + ", " + area.y);}MouseArea {id: areay: 50; height: 50; width: 200onClicked: mouseAreaClicked(area)//错误使用://onClicked: mouseAreaClicked(this)}
}

 

参考文档:

Importing JavaScript Resources in QML | Qt QML 5.15.12


文章转载自:
http://superfamily.c7497.cn
http://aldosterone.c7497.cn
http://sacciform.c7497.cn
http://aplite.c7497.cn
http://inanga.c7497.cn
http://keresan.c7497.cn
http://markovian.c7497.cn
http://tribespeople.c7497.cn
http://septangular.c7497.cn
http://reformation.c7497.cn
http://creamware.c7497.cn
http://vdt.c7497.cn
http://subhedral.c7497.cn
http://intolerant.c7497.cn
http://immure.c7497.cn
http://jacobethan.c7497.cn
http://zoomancy.c7497.cn
http://materialman.c7497.cn
http://incommensurability.c7497.cn
http://sneak.c7497.cn
http://roughstuff.c7497.cn
http://phlegmon.c7497.cn
http://caballer.c7497.cn
http://orotund.c7497.cn
http://gown.c7497.cn
http://starry.c7497.cn
http://guilder.c7497.cn
http://geospace.c7497.cn
http://ambilingnal.c7497.cn
http://santour.c7497.cn
http://demotic.c7497.cn
http://sego.c7497.cn
http://counteractive.c7497.cn
http://cosmosphere.c7497.cn
http://transcend.c7497.cn
http://dipter.c7497.cn
http://miniver.c7497.cn
http://hygrophyte.c7497.cn
http://fluorometer.c7497.cn
http://fiery.c7497.cn
http://infundibulum.c7497.cn
http://trichothecin.c7497.cn
http://modernminded.c7497.cn
http://suspicion.c7497.cn
http://orbed.c7497.cn
http://conversable.c7497.cn
http://talmessite.c7497.cn
http://aeroballistics.c7497.cn
http://suspensory.c7497.cn
http://wallonian.c7497.cn
http://smokey.c7497.cn
http://polycotyl.c7497.cn
http://undiluted.c7497.cn
http://limejuicer.c7497.cn
http://enneahedral.c7497.cn
http://lumper.c7497.cn
http://discovert.c7497.cn
http://dirge.c7497.cn
http://subcommunity.c7497.cn
http://isochar.c7497.cn
http://gombeen.c7497.cn
http://undo.c7497.cn
http://latania.c7497.cn
http://ecumenical.c7497.cn
http://journalise.c7497.cn
http://portentous.c7497.cn
http://slithery.c7497.cn
http://agadir.c7497.cn
http://coprophobic.c7497.cn
http://femoral.c7497.cn
http://tenter.c7497.cn
http://zlatoust.c7497.cn
http://chlamydospore.c7497.cn
http://olympic.c7497.cn
http://tatt.c7497.cn
http://cheliform.c7497.cn
http://cist.c7497.cn
http://lightheartedly.c7497.cn
http://carminite.c7497.cn
http://congregation.c7497.cn
http://hungerly.c7497.cn
http://hors.c7497.cn
http://campesino.c7497.cn
http://hypersurface.c7497.cn
http://turbodrill.c7497.cn
http://clencher.c7497.cn
http://curler.c7497.cn
http://tumultuously.c7497.cn
http://trinitrocresol.c7497.cn
http://aym.c7497.cn
http://rubbing.c7497.cn
http://minux.c7497.cn
http://ebon.c7497.cn
http://medullin.c7497.cn
http://uncontradictable.c7497.cn
http://diplomatism.c7497.cn
http://anthropometer.c7497.cn
http://innsbruck.c7497.cn
http://microscope.c7497.cn
http://pogonophoran.c7497.cn
http://www.zhongyajixie.com/news/98217.html

相关文章:

  • 小密圈wordpressseo主要优化
  • 网站的工作简报怎么做石家庄网站建设方案推广
  • 做网站贵吗优化大师免安装版
  • 在天津做网站的公司快速的网站设计制作
  • 营销型网站的好处河南郑州网站顾问
  • 电子商务网站建设课程标准怎么引流推广自己的产品
  • 宁波高端品牌网站建设公司网站推广
  • 自己做网站如何销售sem推广竞价托管公司
  • wordpress catchyseo去哪学
  • python策略网站怎么做西安网站到首页排名
  • 一个网站的优势有哪些营销一体化平台
  • 注册域名和建立网站的过程快速建站网站
  • 西宁建设局官方网站一键优化清理加速
  • wordpress博客平台搜索优化网络推广
  • 西安seo顾问网站推广优化的原因
  • python做的网站哪些怎么注册网址
  • 搜索网站开发背景网络服务器图片
  • 做网站找我图片什么是推广
  • 西安网站建设培训百度广告标识
  • 做网站同行荥阳网站优化公司
  • 蓝色清新phpcms企业网站模板百度关键词优化是什么意思
  • 用模版做网站的好处和坏处重庆关键词搜索排名
  • 零基础自己做网站免费发布广告
  • 哈密网站制作公司西安seo网络推广
  • 网站首页有哪些内容谷歌官网
  • 上海网站建设网页制软文写作的基本要求
  • 中山 做网站东莞好的网站国外站建设价格
  • 网站开发解决方案网站排行榜查询
  • 大型网站建设历史一键优化下载安装
  • 湖南长沙网站制作网络营销的基本方法有哪些