簡體   English   中英

如何將TableViewColumn列表傳播到子TableView?

[英]How propagate TableViewColumn list to child TableView?

我正在嘗試為QML中的可編輯表創建自定義組件,如下所示:

// BaseTableView.qml
import QtQuick 2.3
import QtQuick.Controls 1.2

Item {
    signal addActionPerformed()
    signal editActionPerformed(int id)
    signal deleteActionPerformed(int id)

    property var model

    ToolBar {
        id: toolBar
        anchors.left: parent.left
        anchors.right: parent.right

        Row {
            ToolButton {
                id: addButton
                iconSource: "qrc:/icons/actions/add.png"
                onClicked: addActionPerformed()
            }

            ToolButton {
                id: editButton
                enabled: false
                iconSource: "qrc:/icons/actions/edit.png"
            }

            ToolButton {
                id: deleteButton
                enabled: false
                iconSource: "qrc:/icons/actions/delete.png"
            }
        }
    }

    TableView {
        id: tableView
        model: parent.model
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: toolBar.bottom
        anchors.bottom: parent.bottom

        onCurrentRowChanged: {
            editButton.enabled = currentRow !== null
            deleteButton.enabled = currentRow !== null
        }
    }
}

並在另一個文件中使用此組件,如下所示:

// Another.qml file
import QtQuick 2.3
import QtQuick.Controls 1.2

import "../common" // Here is BaseTableView.qml

BaseTableView {

    TableViewColumn {
        role: "id"
        title: qsTr("Id")
    }

    TableViewColumn {
        role: "object_expression"
        title: qsTr("Expression")
    }
}

因此,問題是我如何將表視圖列從使用情況傳遞給基礎TableView? 我試圖在BaseTableView中創建屬性列表,並在Aother.qml中將此屬性分配對象列表? 但沒有成功。

使用默認屬性

對象定義可以具有單個默認屬性。 默認屬性是如果在另一個對象的定義中聲明一個對象但未將其聲明為特定屬性的值,則為其分配值的屬性。

與您的情況更相關:

您會注意到,可以將子對象添加到任何基於Item的類型,而無需將其顯式添加到children屬性。 這是因為Item的默認屬性是其data屬性,並且添加到此列表的某個Item的所有項目都會自動添加到其子級列表中。 默認屬性對於重新分配項目的子項很有用。 請參見TabWidget示例,該示例使用默認屬性自動將TabWidget的子級重新分配為內部ListView的子級。

如果您看一下上一段所引用的TabWidget示例 ,那么您應該擁有所需的一切:

import QtQuick 2.0

Item {
    id: tabWidget

    // Setting the default property to stack.children means any child items
    // of the TabWidget are actually added to the 'stack' item's children.
    // See the "Property Binding"
    // documentation for details on default properties.
    default property alias content: stack.children

    property int current: 0

    onCurrentChanged: setOpacities()
    Component.onCompleted: setOpacities()

    function setOpacities() {
        for (var i = 0; i < stack.children.length; ++i) {
            stack.children[i].opacity = (i == current ? 1 : 0)
        }
    }

    Row {
        id: header

        Repeater {
            model: stack.children.length
            delegate: Rectangle {
                width: tabWidget.width / stack.children.length; height: 36

                Rectangle {
                    width: parent.width; height: 1
                    anchors { bottom: parent.bottom; bottomMargin: 1 }
                    color: "#acb2c2"
                }
                BorderImage {
                    anchors { fill: parent; leftMargin: 2; topMargin: 5; rightMargin: 1 }
                    border { left: 7; right: 7 }
                    source: "tab.png"
                    visible: tabWidget.current == index
                }
                Text {
                    horizontalAlignment: Qt.AlignHCenter; verticalAlignment: Qt.AlignVCenter
                    anchors.fill: parent
                    text: stack.children[index].title
                    elide: Text.ElideRight
                    font.bold: tabWidget.current == index
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: tabWidget.current = index
                }
            }
        }
    }

    Item {
        id: stack
        width: tabWidget.width
        anchors.top: header.bottom; anchors.bottom: tabWidget.bottom
    }
}

在這種情況下,如果您要復制由Qt提供的項目完成的操作,那么查看要復制源代碼也很有幫助。 但是,文檔更易於閱讀。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM