qt - QML 控件重叠

标签 qt qml

我在这里做错了什么。使用我的自定义委托(delegate),我的项目在我的 ListView 中相互重叠。这是我得到的...

qt,

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

enter image description here

QML

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
        }
    }
}

最佳答案

实际上,您的问题是您已将标签放在零大小的不可见矩形内(因为它有 height==0width==0 ),两者都在位置 (0, 0) .而不是把 Label转入 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 }
    }
}

你将拥有:

Layouts example

关于qt - QML 控件重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56137561/

相关文章:

qt - 为 QML 应用程序定义窗口图标

c++ - 如何使 Qt Signal 按值发出而不是引用而不会出现编译错误?

c++ - OpenGL "black screen"挫折

c++ - 为什么 QLineEdit with QCompleter for auto completion 没有出现?

c++ - 二进制字符串到 C++ 中的 byte[]?

javascript - 在 MouseClick 运行时动态更改 QML 主题

Qt/C - QWebView - 加载 Flash Lite 插件

c++ - 在 bb10 上实现后退按钮

qt - QML:来自动态鼠标区域的 'Steal' 事件

c++ - 无法动态更新 LineSeries QML 类型