QML 控件重叠

Jok*_*ini 4 qt qml

我在这里做错了什么。我的项目使用我的自定义委托在我的列表视图中相互重叠。这是我得到的...

qt,

这就是我想要做的......

在此处输入图片说明

质量管理语言

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

Frame {
    ListView {
        implicitWidth: 250
        implicitHeight: 250
        clip: true

        model: ListModel {
            ListElement {
                done: true
                description: "Wash the car this could be a really long message with some multiline support\n we will see how it works."
            }
            ListElement {
                done: false
                description: "Fix the car"
            }
            ListElement {
                done: false
                description: "Sell the car"
            }
            ListElement {
                done: true
                description: "Wash the car"
            }
            ListElement {
                done: false
                description: "Fix the car"
            }
            ListElement {
                done: false
                description: "Sell the car"
            }
            ListElement {
                done: true
                description: "Wash the car"
            }
            ListElement {
                done: false
                description: "Fix the car"
            }
            ListElement {
                done: false
                description: "Sell the car"
            }
        }

        delegate: Row {
            spacing: 6
            width: parent.width

            Rectangle {
                id: newsicon
                width: 16
                height: 16
                color: "steelblue"
            }

            Column {
                Rectangle {
                    color: "lightgrey"

                    Label {
                        id: messageText
                        text: model.description
                        width: parent.width
                        wrapMode: Label.Wrap
                    }
                    Label {
                        id: dateText
                        text: "Dec 20, 2019"
                        wrapMode: Label.Wrap
                    }
                }
            }
        }

        ScrollBar.vertical: ScrollBar {
            active: true
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

tro*_*ane 5

实际上,您的问题是您已将标签放置在零大小的不可见矩形内(因为它具有height==0width==0),都在位置 (0, 0)。而不是把Labels 放进Column你把Rectangle它放进去。这就是为什么你有重叠。


我个人建议您使用Layouts,例如:

Frame {
    anchors.centerIn: parent
    ListView {
        implicitWidth: 250
        implicitHeight: 250
        clip: true

        model: listModel
        delegate: RowLayout {

            Rectangle {
                id: newsicon
                width: 16
                height: 16
                color: "steelblue"
            }

            ColumnLayout {
                Layout.fillWidth: true
                spacing: 0
                Label {
                    id: messageText
                    text: model.description
                    width: parent.width
                    wrapMode: Label.WrapAtWordBoundaryOrAnywhere
                }
                Label {
                    id: dateText
                    text: "Dec 20, 2019"
                    font.italic: true
                    color: "grey"
                    wrapMode: Label.WrapAtWordBoundaryOrAnywhere
                }
            }

        }
        ScrollBar.vertical: ScrollBar { active: true }
    }
}
Run Code Online (Sandbox Code Playgroud)

你将拥有:

布局示例

  • 噢,我明白了。感谢您解释这一点以及答案。非常感谢。 (2认同)