前面介绍的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小时内与您取得联系。