【问题标题】:Grandchild breaks repeater孙子打破中继器
【发布时间】:2020-07-12 06:10:55
【问题描述】:

为什么会这样:(有效 = 每个委托文本出现在前一个的下方)

    Column {
        Repeater {
            model: ['test1', 'test2', 'test3']
            delegate: Text {
                text: modelData
            }
        }
    }

但这会破坏布局,因为每个文本都出现在彼此之上:

    Column {
        Repeater {
            model: ['test1', 'test2', 'test3']
            delegate: Item {
                Text {
                    text: modelData
                }
            }
        }
    }

如果我创建一个单独的组件,也会发生同样的事情:

MyTextItem.qml

import QtQuick 2.5

Item {
    property string myText: ''
    Text {
       text: myText
    }
}

然后:

    Column {
        Repeater {
            model: ['test1', 'test2', 'test3']
            delegate: MyTextItem {
                myText: modelData
            }
        }
    }

【问题讨论】:

    标签: qt qml repeater qt5.5


    【解决方案1】:

    问题很简单:列基于委托的 topItem 的几何形状,在您的初始情况下,Text 具有基于内容的implicitWidth 和implicitHeight,但 Item 的几何形状为 0x0,导致它们重叠。解决方案是为 Item 建立适当的几何图形,例如它采用与 Text 相同的大小:

    Column {
        Repeater {
            model: ['test1', 'test2', 'test3']
            delegate: Item{
                implicitWidth: txt.implicitWidth
                implicitHeight: txt.implicitHeight
                Text {
                    id: txt
                    text: modelData
                }
            }
        }
    }

    【讨论】:

    • 是的,这行得通。我认为执行此操作时应该会生成一个警告,因为这很容易被忽略,而且我认为更常见的用例是让项目“排列”而不是堆叠。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2016-09-01
    • 2018-12-19
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多