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 pass 2d Array to qml

How to pass 2d Array to qml

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 2 Posters 1.0k 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 want to pass a c++ 2d sudoku empty grid (without numbers in it) to qml. I used a QAbstractTableModel. I do not have errors, only a warning of the unused parameter for the variable role and when I run the project the grid is not displayed in the qml file.

    //Grid.h
    #pragma once
    
    #include <QAbstractTableModel>
    
    class Grid : public QAbstractTableModel
    {
       Q_OBJECT
       
    public:
       explicit Grid(QObject *parent = nullptr);
       
       QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
       QModelIndex parent(const QModelIndex &index) const override;
       
       int rowCount(const QModelIndex &parent = QModelIndex()) const override;
       int columnCount(const QModelIndex &parent = QModelIndex()) const override;
       
       QVariant data(const QModelIndex &index, int role) const override;
    };
    
    #include "Grid.h"
    
    Grid::Grid(QObject *parent)
        : QAbstractTableModel(parent)
    {}
    
    QModelIndex Grid::index(int row, int column, const QModelIndex &parent) const
    {
        Q_UNUSED(parent);
        return createIndex(row, column);
    }
    
    QModelIndex Grid::parent(const QModelIndex &index) const
    {
        Q_UNUSED(index);
        return QModelIndex();
    }
    
    int Grid::rowCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent);
        return 9;
    }
    
    int Grid::columnCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent);
        return 9;
    }
    
    QVariant Grid::data(const QModelIndex &index, int role) const 
    {
        if (!index.isValid())
            return QVariant();
        
        return QVariant();
    }
    
    import QtQuick 2.15
    import QtQuick.Controls 2.15
    import SudokuGrid 1.0
    
    Window {
        visible: true
        width: 550
        height: 550
        title: "Game"
    
        GridView {
            id: grid
            anchors.fill: parent
            model: SudokuGrid
            cellWidth: 50
            cellHeight: 50
    
            delegate: Rectangle {
                width: 50
                height: 50
                color: model.row % 2 ? "lightgray" : "lightblue"
            }
        }
    }
    

    to pass the grid to qml I used this in main.cpp:
    qmlRegisterUncreatableType<Grid>("SudokuGrid", 1, 0, "SudokuGrid", "Cannot display the grid");

    JoeCFDJ 1 Reply Last reply
    0
    • A arlyn123

      I want to pass a c++ 2d sudoku empty grid (without numbers in it) to qml. I used a QAbstractTableModel. I do not have errors, only a warning of the unused parameter for the variable role and when I run the project the grid is not displayed in the qml file.

      //Grid.h
      #pragma once
      
      #include <QAbstractTableModel>
      
      class Grid : public QAbstractTableModel
      {
         Q_OBJECT
         
      public:
         explicit Grid(QObject *parent = nullptr);
         
         QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
         QModelIndex parent(const QModelIndex &index) const override;
         
         int rowCount(const QModelIndex &parent = QModelIndex()) const override;
         int columnCount(const QModelIndex &parent = QModelIndex()) const override;
         
         QVariant data(const QModelIndex &index, int role) const override;
      };
      
      #include "Grid.h"
      
      Grid::Grid(QObject *parent)
          : QAbstractTableModel(parent)
      {}
      
      QModelIndex Grid::index(int row, int column, const QModelIndex &parent) const
      {
          Q_UNUSED(parent);
          return createIndex(row, column);
      }
      
      QModelIndex Grid::parent(const QModelIndex &index) const
      {
          Q_UNUSED(index);
          return QModelIndex();
      }
      
      int Grid::rowCount(const QModelIndex &parent) const
      {
          Q_UNUSED(parent);
          return 9;
      }
      
      int Grid::columnCount(const QModelIndex &parent) const
      {
          Q_UNUSED(parent);
          return 9;
      }
      
      QVariant Grid::data(const QModelIndex &index, int role) const 
      {
          if (!index.isValid())
              return QVariant();
          
          return QVariant();
      }
      
      import QtQuick 2.15
      import QtQuick.Controls 2.15
      import SudokuGrid 1.0
      
      Window {
          visible: true
          width: 550
          height: 550
          title: "Game"
      
          GridView {
              id: grid
              anchors.fill: parent
              model: SudokuGrid
              cellWidth: 50
              cellHeight: 50
      
              delegate: Rectangle {
                  width: 50
                  height: 50
                  color: model.row % 2 ? "lightgray" : "lightblue"
              }
          }
      }
      

      to pass the grid to qml I used this in main.cpp:
      qmlRegisterUncreatableType<Grid>("SudokuGrid", 1, 0, "SudokuGrid", "Cannot display the grid");

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

      @arlyn123 said in How to pass 2d Array to qml:

      qmlRegisterUncreatableType

      Your class Grid is empty. What do you want to pass?

      A 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @arlyn123 said in How to pass 2d Array to qml:

        qmlRegisterUncreatableType

        Your class Grid is empty. What do you want to pass?

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

        @JoeCFD I am beginner to both qml and c++. How can I fill the c Grid class?

        JoeCFDJ 1 Reply Last reply
        0
        • A arlyn123

          @JoeCFD I am beginner to both qml and c++. How can I fill the c Grid class?

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

          @arlyn123 I guess you want to display digits as strings. Create a 2D array of QString inside class Grid, assign values to it and override func data of QAbstractTableModel in class Grid. Display text inside delegate with default display role

          A 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            @arlyn123 I guess you want to display digits as strings. Create a 2D array of QString inside class Grid, assign values to it and override func data of QAbstractTableModel in class Grid. Display text inside delegate with default display role

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

            @JoeCFD But I want to make a sudoku game with different game levels. Do I need to assign values for each grid?

            JoeCFDJ 1 Reply Last reply
            0
            • A arlyn123

              @JoeCFD But I want to make a sudoku game with different game levels. Do I need to assign values for each grid?

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

              @arlyn123 You have only one GridView in your layout. You can change the values of arrays to update the display on GridView.

              A 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @arlyn123 You have only one GridView in your layout. You can change the values of arrays to update the display on GridView.

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

                @JoeCFD But how do I do that? Do I need a separate method or a even a separate class?

                JoeCFDJ 1 Reply Last reply
                0
                • A arlyn123

                  @JoeCFD But how do I do that? Do I need a separate method or a even a separate class?

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

                  @arlyn123 add a func setData( int row, int column, const QString & value ) in class Grid()

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    arlyn123
                    wrote on last edited by arlyn123
                    #9

                    @JoeCFD also can you please give me an example of how can I work with the data function as you suggested în your first comment

                    JoeCFDJ 1 Reply Last reply
                    0
                    • A arlyn123

                      @JoeCFD also can you please give me an example of how can I work with the data function as you suggested în your first comment

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

                      @arlyn123 https://doc.qt.io/qt-6/qml-qtquick-tableview.html

                      A 1 Reply Last reply
                      1
                      • JoeCFDJ JoeCFD

                        @arlyn123 https://doc.qt.io/qt-6/qml-qtquick-tableview.html

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

                        @JoeCFD Thanks lot for giving me the table view documentation. I was able to use it instead of grid view

                        1 Reply Last reply
                        0
                        • A arlyn123 has marked this topic as solved on
                        • A arlyn123 has marked this topic as unsolved on
                        • A arlyn123 marked this topic as a regular topic on

                        • Login

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