QML Listview主播

时间:2017-07-13 11:20:02

标签: c++ qml qlistview

我有一个信使,我希望我的信息在右边,客户信息在左边。消息将添加到列表视图中。

ListView {
            id: listview
            Layout.fillWidth: true
            Layout.fillHeight: true
            model: messageModel
            delegate: ItemDelegate
            {
                text: model.message
                checked: true
                font.family: "Comic Sans MS"
                font.pixelSize: 15
                anchors.right: parent.right
            }
        }

所以我的信息是正确的。

           TextField {
                id: textField
                Material.accent: Material.Purple
                font.family: "Comic Sans MS"
                Layout.fillWidth: true; Layout.fillHeight: true
                placeholderText: "Write a message..."
                background: null
                focus: true
                Keys.onPressed: {
                    if (event.key == Qt.Key_Enter && textField.text != "") {
                        listview.anchors.right = parent.right
                        messageModel.append({message: textField.text});
                        textField.text = ""
                        listview.positionViewAtEnd()
                    }
                }
            }

listview.anchors为什么不做任何事情?

1 个答案:

答案 0 :(得分:0)

将委托锚定到其父级。例如:

ListView {
    anchors.fill: parent
    anchors.margins: 10
    spacing: 2
    model: ListModel {
        ListElement { target: 1; message: "message1" }
        ListElement { target: 1; message: "messageeeee2" }
        ListElement { target: 2; message: "messageee3" }
        ListElement { target: 2; message: "messageeeeeeeeeeee4" }
        ListElement { target: 1; message: "messageeeeee5" }
    }
    delegate: Rectangle {
        height: 30
        width: txt.width + 10
        anchors.right: target == 1 ? undefined : parent.right
        anchors.left: target == 2 ? undefined : parent.left
        radius: 5
        border {
            width: 2
            color: "orange"
        }
        Text {
            id: txt
            text: message
            anchors.centerIn: parent
        }
    }
}