Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to properly expose data to View when the data is different than exposed model
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 262 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChrisTof
    wrote on last edited by
    #1

    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?

    sierdzioS 1 Reply Last reply
    0
    • C ChrisTof

      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?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @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.

      (Z(:^

      1 Reply Last reply
      1
      • C Offline
        C Offline
        ChrisTof
        wrote on last edited by ChrisTof
        #3

        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?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved