如何在rowdelegate中创建一个矩形边框(TableVIew)

时间:2016-01-23 08:21:21

标签: qt qml tableview

所以这个主题基本上就是我所要求的。

TableViewStyle 
{

    Component {

        id: header
        Rectangle {
            anchors.topMargin: 5
            height: 22
            anchors.verticalCenter: parent.verticalCenter
            color: "#6d6e70"
            Text {
                anchors.fill: parent
                anchors.verticalCenter: parent.verticalCenter
                horizontalAlignment: Text.Center
                verticalAlignment: Text.AlignVCenter
                text: styleData.value
                color: "white"
                renderType: Text.NativeRendering
            }
            Rectangle {
                anchors.right: parent.right
                anchors.top: parent.top
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 1
                anchors.topMargin: 1
                width: 1
                color: "black"
            }
        }
    }
    Component {
        id: defaultRow
        Rectangle {
            height: 30
            property color selectedColor: styleData.row % 2 ? "#fbfbfc" : "white"
            color: styleData.selected ? "#999" : selectedColor
            Rectangle {
                width: 1
                color: "black"
            }
        }

    }
    Component {
        id: largeRow
        Rectangle {
            height: 120
            property color selectedColor: styleData.row % 2 ? "#fbfbfc" : "white"
            color: styleData.selected ? "#999" : selectedColor
            Rectangle {
                width: 1
                color: "black"
            }
        }
    }

    Component {
        id: imageDelegate
        Image {
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            fillMode: Image.PreserveAspectFit
            cache: true;
            asynchronous: true;
            source: styleData.value
        }
    }
}

对于标题,有一个宽度为1和黑色的边框,但对于行,即使我使用了saming参数,它也无法正常工作。如何为rowdelegate生成边框?

1 个答案:

答案 0 :(得分:1)

你可以破解这样的东西:

Rectangle {
    color: "black"

    Rectangle {
        anchors.fill: parent
        anchors.rightMargin: 1

        color: "white"
    }
}

(不要以为我已经理解了这个问题)

<强>更新

(感谢Mitch数据示例)

您想要列边框的类似内容吗?

enter image description here

import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2

Window {
    id: win
    width: 360
    height: 360
    visible: true

    ListModel {
        id: libraryModel
        ListElement {
            title: "A Masterpiece"
            author: "Gabriel"
        }
        ListElement {
            title: "Brilliance"
            author: "Jens"
        }
        ListElement {
            title: "Outstanding"
            author: "Frederik"
        }
    }

    TableView {
        anchors.fill: parent

        TableViewColumn {
            role: "title"
            title: "Title"
            width: 100
        }
        TableViewColumn {
            role: "author"
            title: "Author"
            width: 200
        }
        model: libraryModel

        style: TableViewStyle {
            itemDelegate: Rectangle {
                width: 200
                height: 200

                color: "black"

                Rectangle {
                    anchors.fill: parent
                    anchors.rightMargin: 1

                    color: "white"

                    Text {
                        id: textItem
                        anchors.fill: parent
                        verticalAlignment: Text.AlignVCenter
                        horizontalAlignment: styleData.textAlignment
                        anchors.leftMargin: 12
                        text: styleData.value
                        elide: Text.ElideRight
                        color: textColor
                        renderType: Text.NativeRendering
                    }
                }
            }
        }
    }
}