赞
踩
baseline的作用和top一致
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") Rectangle { anchors.fill: parent //填充到整个窗口 anchors.margins: 20 //边距,与界面边界到间隙 color: "blue" radius: 20 Text { id: text anchors.centerIn: parent //锚定方式:界面到中心 text: qsTr("Welcome To My World!") font.pointSize: 30 color: "orange" anchors.verticalCenterOffset: -50 //垂直中心的偏移负数表示往上偏移 } Button{ id:button1 anchors.top: text.bottom //button到上边界就是text到底部 anchors.topMargin: 70 //与text到间距 text: "进入" font.pixelSize: 20 anchors.horizontalCenter: text.horizontalCenter //以text到垂直中心为中心 anchors.horizontalCenterOffset: -150 //向左偏移150 background: Rectangle { color: "orange" radius: 15 border.color: "black" } } Button{ id:button2 anchors.top: text.bottom anchors.topMargin: 70 text: "退出" font.pixelSize: 20 anchors.left: button1.right anchors.leftMargin: 250 background: Rectangle { color: "orange" radius: 15 border.color: "black" } } } }
一种容器,很方便把众多控件组合在一起
无法根据界面尺寸自动改变定位器管理的控件大小
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 Window { visible: true width: 640 height: 480 title: qsTr("layout") color: "gray" Row{ anchors.centerIn: parent spacing: 10 Rectangle{ width: 100 height: 100 color: "blue" } Rectangle{ width: 100 height: 100 color: "orange" } Rectangle{ width: 100 height: 100 color: "green" } } }
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 Window { visible: true width: 640 height: 480 title: qsTr("layout") color: "gray" Column{ anchors.centerIn: parent spacing: 10 Rectangle{ width: 100 height: 100 color: "blue" } Rectangle{ width: 100 height: 100 color: "orange" } Rectangle{ width: 100 height: 100 color: "green" } } }
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 Window { visible: true width: 640 height: 480 title: qsTr("layout") color: "gray" Grid{ anchors.centerIn: parent rows: 2 rowSpacing: 10 columns: 2 columnSpacing: 10 Rectangle{ width: 100 height: 100 color: "blue" } Rectangle{ width: 100 height: 100 color: "orange" } Rectangle{ width: 100 height: 100 color: "green" } Rectangle{ width: 100 height: 100 color: "purple" } } }
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 Window { visible: true width: 640 height: 480 title: qsTr("layout") color: "gray" Flow{ anchors.centerIn: parent spacing: 10 width: 400 Rectangle{ width: 100 height: 100 color: "blue" } Rectangle{ width: 100 height: 100 color: "orange" } Rectangle{ width: 100 height: 100 color: "green" } Rectangle{ width: 100 height: 100 color: "purple" } } }
Qt5.1开始
需要导入模块
import QtQuick.Layouts 1.3
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 import QtQuick.Layouts 1.3 Window { visible: true width: 640 height: 480 title: qsTr("layout") color: "gray" RowLayout{ anchors.fill: parent spacing: 20 Rectangle{ Layout.alignment: Qt.AlignTop //对齐方式 默认居中对齐 color: "orange" Layout.preferredWidth: 100 Layout.preferredHeight: 100 } Rectangle{ Layout.alignment: Qt.AlignTop color: "blue" Layout.preferredWidth: 100 Layout.preferredHeight: 100 } Rectangle{ Layout.alignment: Qt.AlignBottom color: "green" Layout.preferredWidth: 100 Layout.preferredHeight: 100 } Rectangle{ Layout.alignment: Qt.AlignBottom color: "purple" Layout.preferredWidth: 100 Layout.preferredHeight: 100 } } }
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 import QtQuick.Layouts 1.3 Window { visible: true width: 640 height: 480 title: qsTr("layout") color: "gray" ColumnLayout{ anchors.centerIn: parent spacing: 20 Text{ text: "请选择喜欢的科目" font.pointSize: 20 color: "purple" font.bold: true Layout.leftMargin: 30 } CheckBox{ text: "语文" Layout.leftMargin: 30 font.pixelSize: 15 } CheckBox{ text: "数学" Layout.leftMargin: 30 font.pixelSize: 15 } CheckBox{ text: "英语" Layout.leftMargin: 30 font.pixelSize: 15 } CheckBox{ text: "物理" Layout.leftMargin: 30 font.pixelSize: 15 } CheckBox{ text: "计算机" Layout.leftMargin: 30 font.pixelSize: 15 } } }
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 import QtQuick.Layouts 1.3 Window { visible: true width: 640 height: 480 title: qsTr("layout") color: "gray" GridLayout{ anchors.centerIn: parent columns: 2 rows: 3 columnSpacing: 20 rowSpacing: 20 Layout.rowSpan: 20 //第一行 Label{ text:"用户名" font.pixelSize: 20 } Rectangle{ id:rect1 border.color: "black" width: 200 height: 30 TextEdit{ anchors.fill: rect1 font.pixelSize: 20 } } //第二行 Label{ text:"密码" font.pixelSize: 20 } Rectangle{ id:rect2 border.color: "black" width: 200 height: 30 TextEdit{ anchors.fill: rect2 font.pixelSize: 20 } } //第三行 Button{ text: "登录" font.pixelSize: 20 Layout.leftMargin: -30 Layout.topMargin: 50 } Button{ text: "退出" font.pixelSize: 20 Layout.leftMargin: 140 Layout.topMargin: 50 } } }
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 import QtQuick.Layouts 1.3 Window { visible: true width: 640 height: 480 title: qsTr("layout") color: "gray" StackLayout { id: layout anchors.fill: parent currentIndex: 1 Rectangle { color: 'teal' implicitWidth: 200 implicitHeight: 200 } Rectangle { color: 'plum' implicitWidth: 300 implicitHeight: 200 } } MouseArea { anchors.fill: parent onClicked: { layout.currentIndex === 1 ? layout.currentIndex = 0 : layout.currentIndex = 1 } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。