Qt5 QML,何时使用 ColumnLayout 与 Column? [英] Qt5 QML, when to use a ColumnLayout vs Column?

查看:363
本文介绍了Qt5 QML,何时使用 ColumnLayout 与 Column?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,这有效:

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.2

ApplicationWindow
{
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    function thingWidth()
    {
        return width*80/100
    }

    Column
    {
        spacing: 10;
        anchors.horizontalCenter: parent.horizontalCenter

        Thing { color: "red"; width: thingWidth(); }
        Thing { color: "yellow"; width: thingWidth();  }
        Thing { color: "green"; width: thingWidth();  }
    }

}

但是将 Column 更改为 ColumnLayout 并且它不会(调整窗口大小会导致布局出错).

But change Column to ColumnLayout and it doesn't (resizing window causes layout to go wrong).

任何帮助,谢谢.

编辑 1:

这里也是 Thing.qml 根据要求,

Here's also Thing.qml as requested,

import QtQuick 2.0

Item {
    property alias color: rectangle.color
    width: 50; height: 50

    Rectangle
    {
        id: rectangle
        border.color: "white"
        anchors.fill: parent
    }
}

看起来我的帖子主要是代码.是的,保姆做到了!那是因为人们在这里发布代码.

It looks like my post is mostly code. Yes, nanny it does! that's because people post code on here.

推荐答案

来自 文档:

As from the documentation of Column:

Column 是一种将其子项沿单个列定位的类型.它可以用作在不使用锚点的情况下垂直定位一系列项目的便捷方式.

Column is a type that positions its child items along a single column. It can be used as a convenient way to vertically position a series of items without using anchors.

此外,它还简化了插入、删除等过程中的转换.它还将 properties 附加到项目上,让他们了解自己位置.

Moreover, it eases transitions during insertion, deletion and so on. It also attaches properties to the items to give them notions about their positions.

另一方面,这个GridLayout 的文档(请注意,ColumnLayout 是一个方便的实用程序,但它只不过是一个只有一列的网格,从它的 文档).
它有一组完全不同的属性,以及附加的属性,完全面向项目的排列.

On the other side, this is the documentation of GridLayout (please, note that ColumnLayout is a convenience utility, but it is nothing more than a grid with one column, as from its documentation).
It has a completely different set of properties, as well as attached properties, completely oriented to the arrangement of the items.

无论如何我想文档中最有趣的页面是那个 一个.
我简单地引用一下:

I guess anyway that the most interesting page from the documentation is that one.
I simply cite it:

定位项是在声明性用户界面中管理项位置的容器项.定位器的行为方式与用于标准 Qt 小部件的布局管理器类似,不同之处在于它们本身也是容器.

Positioner items are container items that manage the positions of items in a declarative user interface. Positioners behave in a similar way to the layout managers used with standard Qt widgets, except that they are also containers in their own right.

当需要以常规布局排列时,定位器可以更轻松地处理许多项目.

Positioners make it easier to work with many items when they need to be arranged in a regular layout.

Qt Quick Layouts 还可用于在用户界面中排列 Qt Quick 项目.它们在声明式用户界面上管理项目的位置和大小,非常适合可调整大小的用户界面.

Qt Quick Layouts can also be used to arrange Qt Quick items in a user interface. They manage both the positions and the sizes of items on a declarative user interface, and are well suited for resizable user interfaces.

请注意,ColumnPositioner,而 ColumnLayoutLayout.与往常一样,何时使用它们主要取决于您的目标.

Please, note that a Column is a Positioner, while a ColumnLayout is a Layout. When to use them depends mainly on your goal, as usual.

这篇关于Qt5 QML,何时使用 ColumnLayout 与 Column?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆