Qml TableView - TableViewColumn - ListModel - 访问嵌套对象

Qml TableView - TableViewColumn - ListModel - access nested objects

目前我和一些学生正在用 QtQuick 编写一个小应用程序。
我们有以下 TableView:

TableView {
    model: ListModel {
        id: orderListModel
        Component.onCompleted: {
            var tOrderList = orderController.getOrderList();
            for(var i = 0; i < tTicketList.length; ++i){
                orderListModel.append(tOrderList[i]);
            }
        }
    }


    TableViewColumn {
        role: "orderId"
        title: "Auftragsnummer"
        width: 100
    }
    TableViewColumn {
        role: "customer.name"
        title: "Kunde"
        width: 100
    }
}

getOrderList returns 一个 QList<Object*> 所有订单。 Order class 有一个 属性 customer

Q_PROPERTY(Customer* customer READ getCustomer NOTIFY customerChanged)

依次有一个 属性 称为 name

我们想在 TableView 中显示后者 属性 但不幸的是只有 OrderorderId 属性 有效。

第二个角色应该有什么价值?我们如何访问嵌套对象的数据?

根据 documentation,您可以直接使用 QList<QObject*> 作为视图的模型,而无需像您那样手动复制数据。特别是:

The QObject* is available as the modelData property. As a convenience, the properties of the object are also made available directly in the delegate's context.

考虑到文档中链接的 the example,我们有 QObject 的 属性 可以通过以下方式用作角色:

ListView {
    width: 100; height: 100

    model: myModel                     // injected QList<QObject*> context property
    delegate: Rectangle {
        height: 25
        width: 100
        color: model.modelData.color   // without "modelData" it would not work
        Text { text: name }
    }
}

这同样适用于嵌套属性。 如果我们的 QObject 派生对象有一个 QObject 属性,就像你的情况一样,我们可以通过 modelData 检索它,然后访问它的属性。因此,例如使用 customer 属性,我们会在委托中有这样的内容:

Text { text: model.modelData.customer.name }

ListView 等类似情况确实如此,但我不确定是否可以直接使用 TableView 完成。一种可能的解决方法是将 QObject 派生角色的用法与 styleData.value 结合使用。您可以在role 属性 中定义角色的用法,并在styleData.value 中访问内部属性。针对您的案例的解决方案如下所示(假设 myModel 是上下文 属性,如上例所示):

TableView  {
    width: 100; height: 100

    model: myModel

    TableViewColumn {
        role: "customer"
        title: "Kunde"
        width: 100

        delegate: Text {
            text: styleData.value.name  // accessing the property
        }
    }

    itemDelegate: Item {   }            // should be set to empty to avoid warnings
}

这种方法的一个缺点是您应该为每一列实现一个委托。