创建按钮
创建按钮组件
- 选择File >New File > > Qt Quick Files> Qt Quick UI File>Choose 以创建名为
Button.ui.qml的用户界面文件(例如)。注意: 只有当文件名以大写字母开头时,才会在Components >My Components 中列出组件。
- 选择Design ,在2D 视图中编辑文件。
- 在Navigator 中,选择Item ,并在Properties 中设置按钮的宽度 (W) 和高度 (H) 。
- 从Components >Default Components >Basic 拖动一个Rectangle 到Navigator 中的组件。这样就创建了一个嵌套组件,其中项是矩形的父组件。组件的位置是相对于其父组件的。
- 在Properties 视图中,修改矩形的外观:
- 在Color 字段中,选择按钮颜色。
- 在Radius 字段中,设置矩形的半径,为按钮制作圆角。
- 选择Layout ,然后选择
(Fill to Parent) 按钮,将矩形锚定到项上。
- 在Navigator 中拖动Text 组件到项。
- 在Properties 视图中,编辑Text 组件的属性:
- 在Text 字段中,输入按钮。
您可以在Text color 字段中选择文本颜色,在Font 部分中选择字体、大小和样式。
- 在Alignment 字段中,选择居中按钮,将文本对齐到按钮中心。
- 选择Layout >Fill to Parent ,将文本锚定到整个按钮区域。
- 在Text 字段中,输入按钮。
- 选择Ctrl+S保存按钮。

按钮组件必须在项目中创建才有用。当您在项目中处理其他文件,为用户界面创建屏幕或其他组件时,按钮组件会出现在Components >My Components 中。您可以用它来创建按钮实例并修改其属性,例如为它们分配有用的 ID、更改它们的外观以及为每个按钮实例设置按钮文本。
要创建不使用矢量图形也能漂亮缩放的图形按钮,请使用BorderImage 组件。更多信息,请参阅创建可缩放按钮和边框。
另请参阅 如何使用用户界面组件,Qt Quick 用户界面设计,以及设计Qt Quick 用户界面。
Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.