QML屬性值設置(初始化)方式

QML屬性值設置(初始化)方式有很多種,下面列出四種(常量、表達式、變量或者函數方式賦值)方式:
第一種方式–常量:

//常量方式賦值
Rectangle{
	id: rect1
	x:0
	y:0
	width:10
	height:30
}

第二種方式–變量:

//變量方式賦值,變量可以來自他上層(上司)的。
Rectangle{
	id: rect2
	x: rect1.width
	y: rect1.height
	width: rect1.width*2
	height: rect1.width*2
}

第三種方式–表達式:

//表達式方式賦值,可以是算術表達式、判斷表達式、三目運算符等等,根據不同類型運用不同的表達式,這樣就可以做到任意控制了。
Rectangle{
	id: rect3
	x: rect1.x + rect2.x
	y: rect1.y + rect2.y
	width: rect1.width > rect2.width ? rect1.width : rect2.width
	height: rect1.height> rect2.height? rect1.height: rect2.height
	visible:  rect1.width + rect2.width === 30
}

第四種方式–函數:

//函數方式賦值。書寫格式{ 代碼塊;  return 返回值;}。
/*這種方式就可以用在當你需要某個變量改變的時候,然後根據變量值去改變他的行爲的時就可以用這種方式了*/
Rectangle{
	id: rect4
	x: {
		if(rect3.visible)
			return 0;
		else
			return rect3.x;
	}
	y: {
		if(rect3.visible)
			return 0;
		else
			return rect3.y;
	}
	width:  {
		if(rect3.visible)
			return rect3.x + rect3.width
		else
			return rect3.width;
	}
	height: {
		if(rect3.visible)
			return rect3.y + rect3.height
		else
			return rect3.height;
	}
	color:{
		if(rect3.visible)
			return "#80808080";
		else
			return "#000000";
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章