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. Pass 2d array c++ to qml using GridView
Forum Updated to NodeBB v4.3 + New Features

Pass 2d array c++ to qml using GridView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 3 Posters 406 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.
  • A Offline
    A Offline
    arlyn123
    wrote on last edited by
    #1

    I have a sudoku matrix in c++ code:

    #include "SudokuModel.h"
    
    SudokuModel::SudokuModel(QObject *parent) : QObject(parent)
    {
        sudokuMatrix = {
            {5, 3, 0, 0, 7, 0, 0, 0, 0},
            {6, 0, 0, 1, 9, 5, 0, 0, 0},
            {0, 9, 8, 0, 0, 0, 0, 6, 0},
            {8, 0, 0, 0, 6, 0, 0, 0, 3},
            {4, 0, 0, 8, 0, 3, 0, 0, 1},
            {7, 0, 0, 0, 2, 0, 0, 0, 6},
            {0, 6, 0, 0, 0, 0, 2, 8, 0},
            {0, 0, 0, 4, 1, 9, 0, 0, 5},
            {0, 0, 0, 0, 8, 0, 0, 7, 9}
        };
    }
    
    int SudokuModel::getValue(int row, int col) const
    {
        return sudokuMatrix[row][col];
    }
    

    And its header:

    #ifndef SUDOKUMODEL_H
    #define SUDOKUMODEL_H
    
    #include <QObject>
    #include <QList>
    
    class SudokuModel : public QObject
    {
        Q_OBJECT
    public:
        explicit SudokuModel(QObject *parent = nullptr);
    
        Q_INVOKABLE int getValue(int row, int col) const;
    
    private:
        QList<QList<int>> sudokuMatrix;
    };
    
    #endif 
    

    And I want to pass its values using setContextProperty in a qml GridView. How can I do that?
    I also read that you can also pass a c++ 2d array to qml using QAbstractTableModel but how can I work with it? Does anyone has an example with a QAbstractTableModel?

    JoeCFDJ 1 Reply Last reply
    0
    • A arlyn123

      I have a sudoku matrix in c++ code:

      #include "SudokuModel.h"
      
      SudokuModel::SudokuModel(QObject *parent) : QObject(parent)
      {
          sudokuMatrix = {
              {5, 3, 0, 0, 7, 0, 0, 0, 0},
              {6, 0, 0, 1, 9, 5, 0, 0, 0},
              {0, 9, 8, 0, 0, 0, 0, 6, 0},
              {8, 0, 0, 0, 6, 0, 0, 0, 3},
              {4, 0, 0, 8, 0, 3, 0, 0, 1},
              {7, 0, 0, 0, 2, 0, 0, 0, 6},
              {0, 6, 0, 0, 0, 0, 2, 8, 0},
              {0, 0, 0, 4, 1, 9, 0, 0, 5},
              {0, 0, 0, 0, 8, 0, 0, 7, 9}
          };
      }
      
      int SudokuModel::getValue(int row, int col) const
      {
          return sudokuMatrix[row][col];
      }
      

      And its header:

      #ifndef SUDOKUMODEL_H
      #define SUDOKUMODEL_H
      
      #include <QObject>
      #include <QList>
      
      class SudokuModel : public QObject
      {
          Q_OBJECT
      public:
          explicit SudokuModel(QObject *parent = nullptr);
      
          Q_INVOKABLE int getValue(int row, int col) const;
      
      private:
          QList<QList<int>> sudokuMatrix;
      };
      
      #endif 
      

      And I want to pass its values using setContextProperty in a qml GridView. How can I do that?
      I also read that you can also pass a c++ 2d array to qml using QAbstractTableModel but how can I work with it? Does anyone has an example with a QAbstractTableModel?

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      @arlyn123 you need a C++ model for gridview. for example: QAbstractTableModel
      check here out:
      https://forum.qt.io/topic/71629/2d-game-grid-model-from-c-to-qml

      A 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @arlyn123 you need a C++ model for gridview. for example: QAbstractTableModel
        check here out:
        https://forum.qt.io/topic/71629/2d-game-grid-model-from-c-to-qml

        A Offline
        A Offline
        arlyn123
        wrote on last edited by
        #3

        @JoeCFD Yes if you can, but firstly I have an error int the board.cpp that says board.cpp:36:12: Use of undeclared identifier 'qVariantFromValue'

        jsulmJ 1 Reply Last reply
        0
        • A arlyn123

          @JoeCFD Yes if you can, but firstly I have an error int the board.cpp that says board.cpp:36:12: Use of undeclared identifier 'qVariantFromValue'

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @arlyn123 said in Pass 2d array c++ to qml using GridView:

          qVariantFromValue

          It is obsolete, see https://doc.qt.io/qt-5/qvariant-obsolete.html#qVariantFromValue

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          0
          • jsulmJ jsulm

            @arlyn123 said in Pass 2d array c++ to qml using GridView:

            qVariantFromValue

            It is obsolete, see https://doc.qt.io/qt-5/qvariant-obsolete.html#qVariantFromValue

            A Offline
            A Offline
            arlyn123
            wrote on last edited by
            #5

            @jsulm should I use QVariant::fromValue, right? I am a newbie to qml

            jsulmJ 1 Reply Last reply
            0
            • A arlyn123

              @jsulm should I use QVariant::fromValue, right? I am a newbie to qml

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @arlyn123 Yes

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              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