首页 文章

Qt Quick:在GridLayout中放置Repeater和独立项会导致奇怪的行为

提问于
浏览
1

我的代码:

import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3

Window {
    visible: true
    width: 640
    height: 480

    GridLayout {
        id: grid
        anchors.fill: parent
        columns: 6
        Repeater {
            model: 7
            Rectangle {
                color: "blue"
                Layout.fillWidth: true
                Layout.fillHeight: true
            }
        }
        //Rectangle {color: "red"; width: 20; height: 20}
    }
}

它显示了这个:

screenshot

哪个好 .

但是当您取消注释包含 color: "red" 的行时,布局会中断:

screenshot

知道为什么会这样,以及如何解决它?

1 回答

  • 0

    我'm not sure if it'导致您的问题,因为我没有测试过,但您should avoid设置 widthheightLayout 管理的项目:

    注意:建议不要对布局中项目的x,y,width或height属性进行绑定,因为这会与Layout的目标冲突,并且还会导致绑定循环 .

    例如,您应该使用 Layout.preferredWidth 而不是 width .

    有关更多信息,请参阅Specifying Preferred Size .

相关问题