Qt自己提供Qt Quick快速布局, 以可视化方式在布局中排列Qt Quick Item. 与anchors不一样,Qt Quick Layouts能够根据窗口大小调整其子项的大小以便布局. 需注意如下事项:html
RowLayout { id: layout anchors.fill: parent spacing: 6 Rectangle { color: 'azure' Layout.fillWidth: true Layout.minimumWidth: 50 Layout.preferredWidth: 100 Layout.maximumWidth: 300 Layout.minimumHeight: 150 Text { anchors.centerIn: parent text: parent.width + 'x' + parent.height } } Rectangle { color: 'plum' Layout.fillWidth: true Layout.minimumWidth: 100 Layout.preferredWidth: 200 Layout.preferredHeight: 100 Text { anchors.centerIn: parent text: parent.width + 'x' + parent.height } } }
注意:Layout和anchors都是占用更多内存和实例化时间的对象. 当简单地绑定到x、y、width和height属性就足够时, 避免使用它们(尤为是在列表和表格委托以及控件的样式中)安全
QML中使用var既简单又方便, 但也有几个缺点需注意:网络
property var name property var size property var optionsMenu
property string name property int size property MyMenu optionsMenu
在QML中, 能够使用命令式JavaScript代码执行诸如响应输入事件, 经过网络发送数据等任务. 命令式代码在QML中占有重要地位, 但重要的是要知道什么时候不使用它编辑器
例如, 如下命令赋值:布局
Rectangle { Component.onCompleted: color = "red" }
有如下缺点:字体
Rectangle { color: "red" }
随着显示分辨率的提升,可伸缩的应用程序UI变得愈来愈重要。实现这一点的方法之一是为不一样的屏幕分辨率维护UI的几个副本,并根据可用的分辨率加载适当的副本。尽管这工做得很好,但它增长了维护开销。ui
Qt为这个问题提供了更好的解决方案,建议采用:spa
有了这些,应用程序的UI就能够根据所提供的显示分辨率进行缩放。翻译
以上文章参考自Qt官方文档: https://doc.qt.io/qt-5/qtquick-bestpractices.html
依我的理解, 简要翻译, 若有错漏, 欢迎指正.code