前面介紹的Row、Column和Grid等外,QML還提供了一種使用Anchor(錨)來進行元素布局的方法。每個元素都可被認為有一組無形的“錨線”:left、horizontalCenter、right、top、verticalCenter和bottom,如下圖所示。Text元素還有一個baseline錨線(對于沒有文本的元素,它與top相同)。
這些錨線分別對應元素中的anchors.left、anchors.horizontalCenter等屬性,所有的可視元素都可以使用錨來布局。錨系統還允許為一個元素的錨指定邊距(margin)和偏移(offset)。邊距指定了元素錨到外邊界的空間量,而偏移允許使用中心錨線來定位。一個元素可以通過leftMargin、rightMargin、topMargin和bottomMargin來獨立地指定錯邊距,如下圖所示,也可以使用anchor.margins來為所有的4個鋪指定相同的邊距。
錨偏移使用horizontalCenterOffset、verticalCenterOffset和baselineOffset來指定。編程中還經常用anchors.fill將一個元素充滿另一個元素,這等價于使用了4個直接的錨。但要注意,只能在父子或兄弟元素之間使用錨,而且基于錨的布局不能與絕對的位置定義(如直接設置x和y屬性值)混合使用,否則會出現不確定的結果。
使用Anchor布局一組矩形元素,并測試錨的特性,布局運行效果如圖所示。
具體實現步驟如下。
將前面文章實例中的源文件“Button.qml”、“RedRectangle.qml”、“GreenRectangle.qml”及“BlueRectangle.qml”復制到本項目目錄下。右擊項目視圖“Resources” 一 “qml.qrc”下的“/’節點,選擇“Add Existing Files...”項,彈出“Add Existing Files”對話框,選中上述幾個.qml文件,單擊“打開”按鈕將它們添加到當前項目中。
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對三個矩形元素進行橫向布局 */ //(b)
BlueRectangle { //藍矩形
id : blueRect
anchors.left: parent.left //與窗口左錨線錨定
anchors.top: parent.top //與窗口頂錨線錨定
anchors.leftMargin: 25 //左錨邊距(與窗口左邊距)
anchors.topMargin: 25 //頂錨邊距(與窗口頂邊距)
}
GreenRectangle { //綠矩形
id:greenRect
anchors.left: blueRect.right //綠矩形左錨線與藍矩形的右錨線錨定
anchors.top: blueRect.top //綠矩形頂錨線與藍矩形的頂錨線錨定
anchors.leftMargin: 40 //左錨邊距(與藍矩形的間距)
}
RedRectangle { //紅矩形
id:redRect
anchors.left: greenRect.right //紅矩形左錨線與綠矩形的右錨線錨定
anchors.top: greenRect.top //紅矩形頂錨線與綠矩形的頂錨線錨定
anchors.leftMargin: 40 //左錨邊距(與綠矩形的間距)
}
/*對比測試Anchor的性質*/ //(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 左對齊
anchors.top: changingRect1.bottom
anchors.leftMargin: 25
anchors.topMargin: 20
}
/*復用按鈕*/
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對象作為按鈕單擊事件響應區
anchors.fill: parent
onClicked: {//響應單擊事件代碼
label.text="按鈕已按下!"
label.font.pointSize=11 //改變按鈕文本和字號
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;
}
}
}
其中,
鼠標點擊按鈕后如下圖所示。
————————————————
覺得有用的話請關注點贊,謝謝您的支持!
對于本系列文章相關示例完整代碼有需要的朋友,可關注并在評論區留言!
int indexIn(const QString &str, int offset=0, QRegExp::CaretMode caretMode=CaretAtZero) const
嘗試從位置偏移量(默認為0)在str中查找匹配。如果offset為-1,則從最后一個字符開始搜索;如果-2,在最后一個字符的旁邊;等
返回第一個匹配的位置,如果沒有匹配則返回-1。
caretMode參數可以用來指示^應該在索引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
返回正則表達式中包含的捕獲數。
Qt開發必備技術棧學習路線和資料
介紹一下 ExclusiveGroup。
ExclusiveGroup (互斥分組)本身是不可見元素,用于將若干個可選擇元素組合在一起, 供用戶選擇其中的一個選項。你可以在 ExclusiveGroup 對象中定義 RadioButton、CheckBox、Action 等元素,此時不需要設置它們的 exclusiveGroup 屬性;也可以定義一個只設置了 id 屬性的 ExclusiveGroup 對象,在別處定義 RadioButton、CheckBox、Action 等元素時通過 id 初始化這些元素的 exclusiveGroup 屬性。current 屬性指向互斥分組中第一個選中的元素。
RadioButton用于多選一的場景,使用時需要通過 exclusiveGroup 屬性為其指定一個分組。 它也可以和GroupBox結合使用。要使用RadioButton,需要導入Controls模塊,這樣: import QtQuick.Controls 1.2。
RadioButtonStyle 用來定制一個 RadioButton,要使用它需要引入 QtQuick.Controls.Styles 1.x 模塊。
Qt開發必備技術棧學習路線和資料
*請認真填寫需求信息,我們會在24小時內與您取得聯系。