Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to properly expose data to View when the data is different than exposed model



  • I am implementing model-view using C++ data.
    I have a c++ class with data:

    class TransactionsList: public QObject
    {
        Q_OBJECT
    public:
        //....
    private:
        QVector<Transaction> transactions;
    };
    

    I have the model:

    class TransactionsModel : public QAbstractListModel
    {
        Q_OBJECT
        Q_PROPERTY(TransactionsList *modelTransactions READ getTransactions WRITE setTransactions)
    // ...
    private:
        TransactionsList *transactions;
    };
    

    And the view:

    TableView {
        id: transactionsTable
        anchors.fill: parent
        // ...
        model : TransactionsModel {
            id: transactionsModel;
            modelTransactions: transactions
        }
    

    And TransactionsList created and exposed to QML in main.cpp:

    TransactionsList transactions;
    engine.rootContext()->setContextProperty(QStringLiteral("transactions"), &transactions);
    

    I want to add ChartView that displays sums of transactions amounts for categories. I started like this:

    ChartView {
        //...
        PieSeries {
            id: pieSeries
        }
    
        function generateCategoriesAndAmounts() {
            var categories = getCategories(); // <<< returns set of unique categories.
            var amounts = getCategoriesAmounts(); // <<<
            // Append to pieSeries ...
       }
       Component.onCompleted: {
            pieSeries.generateCategoriesAndAmounts();
        }
    

    Where should getCategories() and getCategoriesAmounts() functions be implemented? In the TransactionsModel or in the TransactionsList class?


  • Moderators

    @ChrisTof said in How to properly expose data to View when the data is different than exposed model:

    Where should getCategories() and getCategoriesAmounts() functions be implemented? In the TransactionsModel or in the TransactionsList class?

    In model.

    The views should not know anything about the underlying data.



  • main.qml looks like this:

    Window {
        //...
        Item {
            id: listViewContainer
            //...
    
            ListView {
                id: listview
                //...
                model: ListModel {
                    ListElement {component: "Datasheet.qml"; } // TableView
                    ListElement {component: "PieChart.qml"}
                    ListElement {component: "BarSeries.qml"}
                }
                delegate: Loader { //... }
    

    I know how to make use of the TransactionsModel in Datasheet.qml

    model: TransactionsModel{
                id: transactionsModel;
                modelTransactions: transactions }
    

    How can I define this model once in the main.qml and distribute it to Datasheet.qml, PieChart.qml, BarSeries.qml?
    Or is it not how it should be done?
    Should I have three models, each for Datasheet.qml, PieChart.qml, BarSeries.qml?


Log in to reply