QML 中 的 Row 和 RowLayout
目录
📊 核心区别概览
🎯 Row 的用法
基本语法
常用属性
实际示例
对齐控制
🎯 RowLayout 的用法
基本语法
Layout 附加属性(关键!)
实际示例
示例 1:弹性布局
示例 2:复杂表单布局
示例 3:混合尺寸约束
⚠️ 重要注意事项
1. 导入模块
2. 性能考虑
3. 常见错误
🏆 选择指南:什么时候用什么?
使用 Row 当:
使用 RowLayout 当:
💡 高级技巧
1. 嵌套使用
2. 与锚点结合
3. 动态调整
总结
📊 核心区别概览
特性 | Row | RowLayout |
---|---|---|
所属模块 | QtQuick | QtQuick.Layouts |
布局能力 | 简单排列 | 智能布局 |
尺寸管理 | 固定或隐式尺寸 | 弹性尺寸,可拉伸填充 |
对齐控制 | 基本对齐 | 精细对齐控制 |
间距 | 固定间距 | 智能间距管理 |
使用复杂度 | 简单 | 相对复杂 |
🎯 Row 的用法
Row
是一个简单的水平排列容器,按照子项的隐式尺寸或显式尺寸进行排列。
基本语法
import QtQuick 2.15Row {spacing: 5 // 子项之间的间距layoutDirection: Qt.LeftToRight // 排列方向:LeftToRight 或 RightToLeftpadding: 10 // 内边距// 子项会从左到右水平排列Rectangle { color: "red"; width: 50; height: 50 }Rectangle { color: "green"; width: 70; height: 50 }Rectangle { color: "blue"; width: 30; height: 50 }
}
常用属性
-
spacing
: 子项之间的间距 -
padding
: 容器内边距 -
layoutDirection
: 排列方向 -
topPadding
,bottomPadding
,leftPadding
,rightPadding
: 分别设置各边内边距
实际示例
Row {spacing: 10padding: 15// 固定尺寸的子项Button { text: "确定"; width: 80 }Button { text: "取消"; width: 80 }Button { text: "帮助"; width: 80 }// 使用 Item 作为间隔符Item { width: 20; height: 1 } // 固定间隔Button { text: "高级选项"; width: 120 }
}
对齐控制
Row {width: 300height: 100spacing: 5// 垂直对齐方式// Top(默认), Center, Bottom, BaselineverticalAlignment: Row.AlignVCenterRectangle { color: "red"; width: 50; height: 30 }Rectangle { color: "green"; width: 70; height: 50 }Rectangle { color: "blue"; width: 30; height: 70 }
}
🎯 RowLayout 的用法
RowLayout
是一个强大的布局管理器,提供智能的尺寸分配和对齐控制。
基本语法
import QtQuick.Layouts 1.15RowLayout {spacing: 5// 子项需要使用 Layout 附加属性Rectangle { color: "red"Layout.preferredWidth: 100Layout.fillHeight: true}Rectangle { color: "green" Layout.preferredWidth: 200Layout.preferredHeight: 50}
}
Layout 附加属性(关键!)
这些是 RowLayout
的核心,用于控制每个子项的布局行为:
属性 | 描述 |
---|---|
Layout.preferredWidth | 首选宽度 |
Layout.preferredHeight | 首选高度 |
Layout.minimumWidth | 最小宽度 |
Layout.maximumWidth | 最大宽度 |
Layout.fillWidth | 是否填充可用宽度 |
Layout.fillHeight | 是否填充可用高度 |
Layout.alignment | 子项在布局单元内的对齐方式 |
实际示例
示例 1:弹性布局
RowLayout {width: 400height: 50spacing: 5Button {text: "固定按钮"Layout.preferredWidth: 100 // 固定宽度}Button {text: "弹性按钮"Layout.fillWidth: true // 填充剩余空间}Button {text: "另一个固定"Layout.preferredWidth: 120}
}
示例 2:复杂表单布局
RowLayout {width: 300spacing: 10// 标签 - 固定宽度Text {text: "用户名:"Layout.preferredWidth: 80 // 固定标签宽度Layout.alignment: Qt.AlignVCenter // 垂直居中}// 输入框 - 弹性宽度TextField {placeholderText: "请输入用户名"Layout.fillWidth: true // 填充剩余空间}// 按钮 - 固定宽度Button {text: "检查"Layout.preferredWidth: 60}
}
示例 3:混合尺寸约束
RowLayout {width: 500height: 40Rectangle {color: "lightblue"Layout.preferredWidth: 50Layout.minimumWidth: 30 // 最小宽度Layout.maximumWidth: 70 // 最大宽度Layout.fillHeight: true}Rectangle {color: "lightgreen"Layout.fillWidth: true // 填充剩余空间Layout.minimumWidth: 100 // 但至少100像素Layout.fillHeight: true}Rectangle {color: "lightcoral"Layout.preferredWidth: 80Layout.fillHeight: true}
}
⚠️ 重要注意事项
1. 导入模块
// Row 在 QtQuick 中
import QtQuick 2.15
Row { /* ... */ }// RowLayout 需要单独导入 Layouts
import QtQuick.Layouts 1.15
RowLayout { /* ... */ }
2. 性能考虑
-
Row
:更轻量,性能更好,适合简单排列 -
RowLayout
:功能更强但稍重,适合复杂布局需求
3. 常见错误
// 错误:RowLayout 的子项没有使用 Layout 属性
RowLayout {Button { width: 100 } // ❌ 这样写不会按预期工作Button { width: 100 }
}// 正确:应该使用 Layout 附加属性
RowLayout {Button { Layout.preferredWidth: 100 // ✅ 正确方式}
}
🏆 选择指南:什么时候用什么?
使用 Row
当:
-
只需要简单的水平排列
-
子项有固定尺寸或隐式尺寸
-
不需要复杂的弹性布局
-
追求最佳性能
// 简单的按钮栏
Row {spacing: 10Button { text: "新建"; width: 60 }Button { text: "打开"; width: 60 }Button { text: "保存"; width: 60 }
}
使用 RowLayout
当:
-
需要子项弹性填充可用空间
-
需要精细的尺寸约束(最小/最大尺寸)
-
需要复杂的对齐需求
-
构建表单或需要响应式布局
// 复杂的表单布局
RowLayout {Text { text: "邮箱:"; Layout.preferredWidth: 60 }TextField { Layout.fillWidth: true }Button { text: "验证" Layout.preferredWidth: 70 Layout.alignment: Qt.AlignVCenter}
}
💡 高级技巧
1. 嵌套使用
RowLayout {// 外层水平布局ColumnLayout {// 内层垂直布局Text { text: "标签" }TextField { }}Button {text: "按钮"Layout.alignment: Qt.AlignBottom}
}
2. 与锚点结合
Item {RowLayout {id: layoutanchors {top: parent.topleft: parent.leftright: parent.rightmargins: 10}// 布局内容...}
}
3. 动态调整
RowLayout {property bool expanded: falseButton {text: "详情"Layout.preferredWidth: 80}Rectangle {color: "lightgray"Layout.fillWidth: trueLayout.preferredHeight: expanded ? 100 : 0 // 动态高度visible: expandedBehavior on Layout.preferredHeight {NumberAnimation { duration: 200 }}}
}
总结
-
Row
:简单、快速、轻量的水平排列工具 -
RowLayout
:强大、灵活、智能的布局管理器
简单规则:如果只是简单排列用 Row
,如果需要智能布局用 RowLayout
。掌握两者的区别和适用场景,能够让你更高效地构建 QML 界面。