Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
前面介紹的Row、Column和Grid等外,QML還提供了一種使用Anchor(錨)來進(jìn)行元素布局的方法。每個(gè)元素都可被認(rèn)為有一組無形的“錨線”:left、horizontalCenter、right、top、verticalCenter和bottom,如下圖所示。Text元素還有一個(gè)baseline錨線(對(duì)于沒有文本的元素,它與top相同)。
這些錨線分別對(duì)應(yīng)元素中的anchors.left、anchors.horizontalCenter等屬性,所有的可視元素都可以使用錨來布局。錨系統(tǒng)還允許為一個(gè)元素的錨指定邊距(margin)和偏移(offset)。邊距指定了元素錨到外邊界的空間量,而偏移允許使用中心錨線來定位。一個(gè)元素可以通過leftMargin、rightMargin、topMargin和bottomMargin來獨(dú)立地指定錯(cuò)邊距,如下圖所示,也可以使用anchor.margins來為所有的4個(gè)鋪指定相同的邊距。
錨偏移使用horizontalCenterOffset、verticalCenterOffset和baselineOffset來指定。編程中還經(jīng)常用anchors.fill將一個(gè)元素充滿另一個(gè)元素,這等價(jià)于使用了4個(gè)直接的錨。但要注意,只能在父子或兄弟元素之間使用錨,而且基于錨的布局不能與絕對(duì)的位置定義(如直接設(shè)置x和y屬性值)混合使用,否則會(huì)出現(xiàn)不確定的結(jié)果。
使用Anchor布局一組矩形元素,并測試錨的特性,布局運(yùn)行效果如圖所示。
具體實(shí)現(xiàn)步驟如下。
將前面文章實(shí)例中的源文件“Button.qml”、“RedRectangle.qml”、“GreenRectangle.qml”及“BlueRectangle.qml”復(fù)制到本項(xiàng)目目錄下。右擊項(xiàng)目視圖“Resources” 一 “qml.qrc”下的“/’節(jié)點(diǎn),選擇“Add Existing Files...”項(xiàng),彈出“Add Existing Files”對(duì)話框,選中上述幾個(gè).qml文件,單擊“打開”按鈕將它們添加到當(dāng)前項(xiàng)目中。
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
width: 320
height: 240
visible: true
title: qsTr("Anchor")
Rectangle {
id: windowRect
/*定義屬性別名*/ // (a)
property alias chgRect1: changingRect1 //矩形changingRect1屬性別名
property alias chgRect2: changingRect2 //矩形changingRect2屬性別名
property alias rRect: redRect //紅矩形redRect屬性別名
width: 360
height: 360
anchors.fill: parent
/* 使用Anchor對(duì)三個(gè)矩形元素進(jìn)行橫向布局 */ //(b)
BlueRectangle { //藍(lán)矩形
id : blueRect
anchors.left: parent.left //與窗口左錨線錨定
anchors.top: parent.top //與窗口頂錨線錨定
anchors.leftMargin: 25 //左錨邊距(與窗口左邊距)
anchors.topMargin: 25 //頂錨邊距(與窗口頂邊距)
}
GreenRectangle { //綠矩形
id:greenRect
anchors.left: blueRect.right //綠矩形左錨線與藍(lán)矩形的右錨線錨定
anchors.top: blueRect.top //綠矩形頂錨線與藍(lán)矩形的頂錨線錨定
anchors.leftMargin: 40 //左錨邊距(與藍(lán)矩形的間距)
}
RedRectangle { //紅矩形
id:redRect
anchors.left: greenRect.right //紅矩形左錨線與綠矩形的右錨線錨定
anchors.top: greenRect.top //紅矩形頂錨線與綠矩形的頂錨線錨定
anchors.leftMargin: 40 //左錨邊距(與綠矩形的間距)
}
/*對(duì)比測試Anchor的性質(zhì)*/ //(c)
RedRectangle {
id:changingRect1
anchors.left: parent.left //矩形 changingRect1 初始與窗體左錨線錨定
anchors.top: blueRect.bottom
anchors.leftMargin: 25
anchors.topMargin: 25
}
RedRectangle {
id:changingRect2
anchors.left: parent.left //changingRect2與 changingRect1 左對(duì)齊
anchors.top: changingRect1.bottom
anchors.leftMargin: 25
anchors.topMargin: 20
}
/*復(fù)用按鈕*/
Button {
width:95
height:35 //(d)
anchors.right: redRect.right
anchors.top: changingRect2.bottom
anchors.topMargin: 10
}
}
}
其中,
import QtQuick 2.0
Rectangle { //將Rectangle自定義成按鈕
id:btn
width:100;height:62 //按鈕的尺寸
color: "teal" //按鈕顏色
border.color: "aqua" //按鈕邊界色
border.width: 3 //按鈕邊界寬度
Text { //Text元素作為按鈕文本
id: label
anchors.centerIn: parent
font.pointSize: 16
text: "開始"
}
MouseArea { //MouseArea對(duì)象作為按鈕單擊事件響應(yīng)區(qū)
anchors.fill: parent
onClicked: {//響應(yīng)單擊事件代碼
label.text="按鈕已按下!"
label.font.pointSize=11 //改變按鈕文本和字號(hào)
btn.color="aqua" //改變按鈕顏色
btn.border.color="teal" //改變按鈕邊界色
/* 改變changingRect1的右錨屬性 */ //(a)
windowRect.chgRect1.anchors.left=undefined;
windowRect.chgRect1.anchors.right=windowRect.rRect.right;
/* 改變 changingRect2 的右錨屬性 */ // (b)
windowRect.chgRect2.anchors.right=windowRect.rRect.right;
windowRect.chgRect2.anchors.left=undefined;
}
}
}
其中,
鼠標(biāo)點(diǎn)擊按鈕后如下圖所示。
————————————————
覺得有用的話請關(guān)注點(diǎn)贊,謝謝您的支持!
對(duì)于本系列文章相關(guān)示例完整代碼有需要的朋友,可關(guān)注并在評(píng)論區(qū)留言!
int indexIn(const QString &str, int offset=0, QRegExp::CaretMode caretMode=CaretAtZero) const
嘗試從位置偏移量(默認(rèn)為0)在str中查找匹配。如果offset為-1,則從最后一個(gè)字符開始搜索;如果-2,在最后一個(gè)字符的旁邊;等
返回第一個(gè)匹配的位置,如果沒有匹配則返回-1。
caretMode參數(shù)可以用來指示^應(yīng)該在索引0處匹配還是在偏移量處匹配。
QString regexStr="^[0-9a-zA-Z_]+@[0-9a-zA-Z]+(\\.[a-zA-Z]+)+$";
QRegExp regExp(regexStr);
QString str="123@qq.com";
if(regExp.indexIn(str)>=0)
{
qDebug()<<"This is email";
}
int QRegExp::captureCount() const
返回正則表達(dá)式中包含的捕獲數(shù)。
Qt開發(fā)必備技術(shù)棧學(xué)習(xí)路線和資料
介紹一下 ExclusiveGroup。
ExclusiveGroup (互斥分組)本身是不可見元素,用于將若干個(gè)可選擇元素組合在一起, 供用戶選擇其中的一個(gè)選項(xiàng)。你可以在 ExclusiveGroup 對(duì)象中定義 RadioButton、CheckBox、Action 等元素,此時(shí)不需要設(shè)置它們的 exclusiveGroup 屬性;也可以定義一個(gè)只設(shè)置了 id 屬性的 ExclusiveGroup 對(duì)象,在別處定義 RadioButton、CheckBox、Action 等元素時(shí)通過 id 初始化這些元素的 exclusiveGroup 屬性。current 屬性指向互斥分組中第一個(gè)選中的元素。
RadioButton用于多選一的場景,使用時(shí)需要通過 exclusiveGroup 屬性為其指定一個(gè)分組。 它也可以和GroupBox結(jié)合使用。要使用RadioButton,需要導(dǎo)入Controls模塊,這樣: import QtQuick.Controls 1.2。
RadioButtonStyle 用來定制一個(gè) RadioButton,要使用它需要引入 QtQuick.Controls.Styles 1.x 模塊。
Qt開發(fā)必備技術(shù)棧學(xué)習(xí)路線和資料
*請認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。