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
Forum Updated to NodeBB v4.3 + New Features

How to pass 2d Array to qml

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 2 Posters 700 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 12 Jan 2024, 17:18 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");

    J 1 Reply Last reply 12 Jan 2024, 17:39
    0
    • A arlyn123
      12 Jan 2024, 17:18

      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");

      J Offline
      J Offline
      JoeCFD
      wrote on 12 Jan 2024, 17:39 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 12 Jan 2024, 17:47
      0
      • J JoeCFD
        12 Jan 2024, 17:39

        @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 12 Jan 2024, 17:47 last edited by
        #3

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

        J 1 Reply Last reply 12 Jan 2024, 18:11
        0
        • A arlyn123
          12 Jan 2024, 17:47

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

          J Offline
          J Offline
          JoeCFD
          wrote on 12 Jan 2024, 18:11 last edited by JoeCFD 1 Dec 2024, 18:13
          #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 12 Jan 2024, 18:29
          0
          • J JoeCFD
            12 Jan 2024, 18:11

            @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 12 Jan 2024, 18:29 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?

            J 1 Reply Last reply 12 Jan 2024, 18:35
            0
            • A arlyn123
              12 Jan 2024, 18:29

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

              J Offline
              J Offline
              JoeCFD
              wrote on 12 Jan 2024, 18:35 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 12 Jan 2024, 18:43
              0
              • J JoeCFD
                12 Jan 2024, 18:35

                @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 12 Jan 2024, 18:43 last edited by arlyn123 1 Dec 2024, 18:46
                #7

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

                J 1 Reply Last reply 12 Jan 2024, 18:55
                0
                • A arlyn123
                  12 Jan 2024, 18:43

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

                  J Offline
                  J Offline
                  JoeCFD
                  wrote on 12 Jan 2024, 18:55 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 12 Jan 2024, 18:56 last edited by arlyn123 1 Dec 2024, 18:57
                    #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

                    J 1 Reply Last reply 12 Jan 2024, 19:01
                    0
                    • A arlyn123
                      12 Jan 2024, 18:56

                      @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

                      J Offline
                      J Offline
                      JoeCFD
                      wrote on 12 Jan 2024, 19:01 last edited by
                      #10

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

                      A 1 Reply Last reply 13 Jan 2024, 10:18
                      1
                      • J JoeCFD
                        12 Jan 2024, 19:01

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

                        A Offline
                        A Offline
                        arlyn123
                        wrote on 13 Jan 2024, 10:18 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 13 Jan 2024, 11:04
                        • A arlyn123 has marked this topic as unsolved on 17 Jan 2024, 23:42
                        • A arlyn123 marked this topic as a regular topic on 17 Jan 2024, 23:42

                        1/11

                        12 Jan 2024, 17:18

                        • Login

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