繁体   English   中英

QML:如何用布局填充矩形

[英]QML: How to fill a Rectangle with a Layout

我的页面中间有一个矩形,我想用布局填充矩形。 这是我要实现的基本层次结构:

import QtQuick 2.0

Item {   
    Rectangle {        
        width: parent.width/3
        height: parent.height/3
        anchors.centerIn: parent

        ColumnLayout {                
            Rectangle {
                width: parent.width
                height: parent/4
                color: "red"
            }

            Rectangle {
                width: parent.width
                height: parent/4 
                color: "blue"
            }
        }        
    }
}

这样做的问题是,在Rectangle中定义ColumnLayout会导致错误(Qt Creator在单词下划线)。 这里有什么问题?

就你而言

height: parent/4

那应该是

height: parent.height/4

并尝试使用Item而不是Rectangle进行布局

Item {
    width: 900
    height: 900

    Item {
        width:  parent.width/3
        height: parent.height/3
        ColumnLayout {
            anchors.fill: parent
            Rectangle {
                width: parent.width
                height: parent.height/4
                color: "red"
            }

            Rectangle {
                width: parent.width
                height: parent.height/4
                color: "blue"
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM