如何更改列或行中项目之间的间距

时间:2015-10-20 04:28:54

标签: qml qt5 qtquick2

我在QML中使用ItemColumn类型对齐Row。有没有办法给每个Item提供不同的间距。以下内容:

ITEM1

间距:10

ITEM2

间距:20

项目3

间距:40

ITEM4

这是我的代码:

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

    Rectangle{
        id: rect
        anchors.fill: parent

        Column{
            id: column
            anchors.centerIn: parent
            spacing: 10

            Row{
                id: row1
                anchors.horizontalCenter:  parent.horizontalCenter
                Rectangle{
                    width: 300
                    height: 100
                    color: "lightgreen"
                }
            }
            Row{
                id: row2
                anchors.horizontalCenter:  parent.horizontalCenter
                Rectangle{
                    width: 100
                    height: 50
                    color: "lightblue"
                }
            }
            Row{
                id: row3
                anchors.horizontalCenter:  parent.horizontalCenter
                Rectangle{
                    width: 50
                    height: 50
                    color: "green"
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

带有spacer项目的hacky版本

有时我比ColumnLayout更喜欢这个,因为在某些情况下我只是不能使用ColumnLayout(虽然目前无法解释原因)。 我按照以下方式开展工作

Column {
    Rectangle {
        // ...
    }

    Item {
        width: 1 // dummy value != 0
        height: 10
    }

    Rectangle {
        // ...
    }

    Item {
        width: 1 // dummy value != 0
        height: 20
    }

    Rectangle {
        // ...
    }
}

宽度为0的项目不起作用。我建议将Spacer_Col.qml(和Spacer_Row模拟)放入Toolbox,看起来像

import QtQuick 2.8
Item {
    id: root
    property alias spacing: root.height
    width: 1 // dummy value different from 0
}

使用ColumnLayout

ColumnLayout {
    Rectangle{
        // ...
    }

    Rectangle{
        Layout.topMargin: 10
        // ...
    }

    Rectangle{
        Layout.topMargin: 20
        // ...
    }
}

答案 1 :(得分:0)

通过嵌套要间隔的每个矩形,如下所示;

void Application_EndRequest(Object source, EventArgs e)
{
    foreach (var item in HttpContext.Current.Items.Values)
   {
       var disposableItem = item as IDisposable;

       if (disposableItem != null)
       {
           disposableItem.Dispose();
       }
   }

Different spacings in a row