Qt5 QML,何时使用ColumnLayout vs Column?

jkj*_*uio 11 qt qml qt5

例如,这有效:

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();  }
    }

}
Run Code Online (Sandbox Code Playgroud)

Column改为ColumnLayout,它没有(调整窗口大小会导致布局出错).

任何帮助,谢谢.

编辑1:

这也是Thing.qml按要求,

import QtQuick 2.0

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

    Rectangle
    {
        id: rectangle
        border.color: "white"
        anchors.fill: parent
    }
}
Run Code Online (Sandbox Code Playgroud)

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

sky*_*ack 9

文档Column:

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

而且,它简化了插入,删除等过程中的转换.它还将属性附加到项目上,以便为其提供有关其位置的概念.

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

我想无论如何,文档中最有趣的页面就是那个.
我只是引用它:

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

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

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

请注意,a Column是a Positioner,而a ColumnLayout是a Layout.何时使用它们主要取决于你的目标,像往常一样.

  • 我认为[定位器和布局](http://doc.qt.io/qt-5/qtquick-positioning-layouts.html)文档的链接在这里会很有用:) (3认同)
  • 感谢您的回答.据我了解,像"Column"这样的定位器可以被告知位置,但像"ColumnLayout"这样的布局必须自己计算位置.在`ColumnLayout`中设置`width`是不好的,但在`Column`中没关系. (2认同)
  • 在布局中,您有很多有趣的附加属性,如“ Layout.preferredwidth”。请参阅链接的文档。 (2认同)