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. setData function from qml to edit cells
Forum Updated to NodeBB v4.3 + New Features

setData function from qml to edit cells

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 410 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 arlyn123
    #1

    I've made a sudoku grid using a QAbstractTableModel c++ class. I want to edit the cells that are empty.

    Here if I enter something and then press enter nothing happend in the UI. The value is not displayed.
    image-17.png

    I think the problem can be the setData function but I don't know how to make it work because I don't know how to use it.
    How can I make the numbers to be displayed in the grid?

    #include "Grid.h"
    
    Grid::Grid(QObject *parent) : QAbstractTableModel(parent)
    {
        int initialValues[9][9] = {
            {7, 0, 2, 0, 5, 0, 6, 0, 0},
            {0, 0, 0, 0, 0, 3, 0, 0, 0},
            {1, 0, 0, 0, 0, 9, 5, 0, 0},
            {8, 0, 0, 0, 0, 0, 0, 9, 0},
            {0, 4, 3, 0, 0, 0, 7, 5, 0},
            {0, 9, 0, 0, 0, 0, 0, 0, 8},
            {0, 0, 9, 7, 0, 0, 0, 0, 5},
            {0, 0, 0, 2, 0, 0, 0, 0, 0},
            {0, 0, 7, 0, 4, 0, 2, 0, 3}
        };
    
        for (int row = 0; row < 9; ++row) {
            for (int col = 0; col < 9; ++col) {
                gridData[row][col] = initialValues[row][col];
            }
        }
    }
    
    int Grid::rowCount(const QModelIndex &) const
    {
        return 9;
    }
    
    int Grid::columnCount(const QModelIndex &) const
    {
        return 9;
    }
    
    QVariant Grid::data(const QModelIndex &index, int role) const
    {
        int row = index.row();
        int col = index.column();
    
        switch (role) {
        case Qt::DisplayRole:
            return gridData[row][col];
        }
    
        return QVariant();
    }
    
    bool Grid::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (role == Qt::EditRole) {
            if (!checkIndex(index))
                return false;
    
            gridData[index.row()][index.column()] = value.toInt();
            return true;
    
        }
        return false;
    }
    
    Qt::ItemFlags Grid::flags(const QModelIndex &index) const
    {
        return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
    }
    
    TableView {
                    id: tableView
                    anchors.fill: parent
                    clip: true
    
                    model: SudokuGrid {}
    
                    delegate: Rectangle {
                        implicitWidth: 50
                        implicitHeight: 50
                        border {
                             color: "white"
                             width: 1
                        }
    
                        Rectangle {
                         width: 1
                         height: parent.height
                         color: model.column % 3 == 0 ? "black" : "transparent"
                        }
    
                                       Rectangle {
                                           width: parent.width
                                           height: 1
                                           color: model.row % 3 == 0 ? "black" : "transparent"
                                       }
    
                        color: model.row % 2 ? "lightpink" : "lightblue"
    
    
                        Text {
                            anchors.centerIn: parent
                            text: display
                            font.pointSize: 12
                        }
    
                        TableView.editDelegate: TextField {
                                        anchors.fill: parent
                                        text: display
                                        horizontalAlignment: TextInput.AlignHCenter
                                        verticalAlignment: TextInput.AlignVCenter
                                        Component.onCompleted: selectAll()
    
                                        onAccepted: {
                                                tableView.commit()
                                            }
    
                                            TableView.onCommit: {
                                                display = text
                                            }
    
                        }
                    }
    
    JoeCFDJ JonBJ 2 Replies Last reply
    0
    • A arlyn123

      I've made a sudoku grid using a QAbstractTableModel c++ class. I want to edit the cells that are empty.

      Here if I enter something and then press enter nothing happend in the UI. The value is not displayed.
      image-17.png

      I think the problem can be the setData function but I don't know how to make it work because I don't know how to use it.
      How can I make the numbers to be displayed in the grid?

      #include "Grid.h"
      
      Grid::Grid(QObject *parent) : QAbstractTableModel(parent)
      {
          int initialValues[9][9] = {
              {7, 0, 2, 0, 5, 0, 6, 0, 0},
              {0, 0, 0, 0, 0, 3, 0, 0, 0},
              {1, 0, 0, 0, 0, 9, 5, 0, 0},
              {8, 0, 0, 0, 0, 0, 0, 9, 0},
              {0, 4, 3, 0, 0, 0, 7, 5, 0},
              {0, 9, 0, 0, 0, 0, 0, 0, 8},
              {0, 0, 9, 7, 0, 0, 0, 0, 5},
              {0, 0, 0, 2, 0, 0, 0, 0, 0},
              {0, 0, 7, 0, 4, 0, 2, 0, 3}
          };
      
          for (int row = 0; row < 9; ++row) {
              for (int col = 0; col < 9; ++col) {
                  gridData[row][col] = initialValues[row][col];
              }
          }
      }
      
      int Grid::rowCount(const QModelIndex &) const
      {
          return 9;
      }
      
      int Grid::columnCount(const QModelIndex &) const
      {
          return 9;
      }
      
      QVariant Grid::data(const QModelIndex &index, int role) const
      {
          int row = index.row();
          int col = index.column();
      
          switch (role) {
          case Qt::DisplayRole:
              return gridData[row][col];
          }
      
          return QVariant();
      }
      
      bool Grid::setData(const QModelIndex &index, const QVariant &value, int role)
      {
          if (role == Qt::EditRole) {
              if (!checkIndex(index))
                  return false;
      
              gridData[index.row()][index.column()] = value.toInt();
              return true;
      
          }
          return false;
      }
      
      Qt::ItemFlags Grid::flags(const QModelIndex &index) const
      {
          return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
      }
      
      TableView {
                      id: tableView
                      anchors.fill: parent
                      clip: true
      
                      model: SudokuGrid {}
      
                      delegate: Rectangle {
                          implicitWidth: 50
                          implicitHeight: 50
                          border {
                               color: "white"
                               width: 1
                          }
      
                          Rectangle {
                           width: 1
                           height: parent.height
                           color: model.column % 3 == 0 ? "black" : "transparent"
                          }
      
                                         Rectangle {
                                             width: parent.width
                                             height: 1
                                             color: model.row % 3 == 0 ? "black" : "transparent"
                                         }
      
                          color: model.row % 2 ? "lightpink" : "lightblue"
      
      
                          Text {
                              anchors.centerIn: parent
                              text: display
                              font.pointSize: 12
                          }
      
                          TableView.editDelegate: TextField {
                                          anchors.fill: parent
                                          text: display
                                          horizontalAlignment: TextInput.AlignHCenter
                                          verticalAlignment: TextInput.AlignVCenter
                                          Component.onCompleted: selectAll()
      
                                          onAccepted: {
                                                  tableView.commit()
                                              }
      
                                              TableView.onCommit: {
                                                  display = text
                                              }
      
                          }
                      }
      
      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      @arlyn123 said in setData function from qml to edit cells:

      bool Grid::setData(const QModelIndex &index, const QVariant &value, int role)
      {
      if (role == Qt::EditRole) {
      if (!checkIndex(index))
      return false;

          gridData[index.row()][index.column()] = value.toInt();
          return true;
      
      }
      return false;
      

      }

      Can you try? But you need to make sure setData func is called at first.
      emit dataChanged( index ); may not be needed.
      https://doc.qt.io/qt-6/qabstractitemmodel.html#dataChanged

      bool Grid::setData(const QModelIndex &index, const QVariant &value, int role)
      {
          if (role == Qt::EditRole) {
              if (!checkIndex(index))
                  return false;
      
              gridData[index.row()][index.column()] = value.toInt();
              emit dataChanged( index );
              return true;
      
          }
          return false;
      }
      
      1 Reply Last reply
      1
      • A arlyn123

        I've made a sudoku grid using a QAbstractTableModel c++ class. I want to edit the cells that are empty.

        Here if I enter something and then press enter nothing happend in the UI. The value is not displayed.
        image-17.png

        I think the problem can be the setData function but I don't know how to make it work because I don't know how to use it.
        How can I make the numbers to be displayed in the grid?

        #include "Grid.h"
        
        Grid::Grid(QObject *parent) : QAbstractTableModel(parent)
        {
            int initialValues[9][9] = {
                {7, 0, 2, 0, 5, 0, 6, 0, 0},
                {0, 0, 0, 0, 0, 3, 0, 0, 0},
                {1, 0, 0, 0, 0, 9, 5, 0, 0},
                {8, 0, 0, 0, 0, 0, 0, 9, 0},
                {0, 4, 3, 0, 0, 0, 7, 5, 0},
                {0, 9, 0, 0, 0, 0, 0, 0, 8},
                {0, 0, 9, 7, 0, 0, 0, 0, 5},
                {0, 0, 0, 2, 0, 0, 0, 0, 0},
                {0, 0, 7, 0, 4, 0, 2, 0, 3}
            };
        
            for (int row = 0; row < 9; ++row) {
                for (int col = 0; col < 9; ++col) {
                    gridData[row][col] = initialValues[row][col];
                }
            }
        }
        
        int Grid::rowCount(const QModelIndex &) const
        {
            return 9;
        }
        
        int Grid::columnCount(const QModelIndex &) const
        {
            return 9;
        }
        
        QVariant Grid::data(const QModelIndex &index, int role) const
        {
            int row = index.row();
            int col = index.column();
        
            switch (role) {
            case Qt::DisplayRole:
                return gridData[row][col];
            }
        
            return QVariant();
        }
        
        bool Grid::setData(const QModelIndex &index, const QVariant &value, int role)
        {
            if (role == Qt::EditRole) {
                if (!checkIndex(index))
                    return false;
        
                gridData[index.row()][index.column()] = value.toInt();
                return true;
        
            }
            return false;
        }
        
        Qt::ItemFlags Grid::flags(const QModelIndex &index) const
        {
            return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
        }
        
        TableView {
                        id: tableView
                        anchors.fill: parent
                        clip: true
        
                        model: SudokuGrid {}
        
                        delegate: Rectangle {
                            implicitWidth: 50
                            implicitHeight: 50
                            border {
                                 color: "white"
                                 width: 1
                            }
        
                            Rectangle {
                             width: 1
                             height: parent.height
                             color: model.column % 3 == 0 ? "black" : "transparent"
                            }
        
                                           Rectangle {
                                               width: parent.width
                                               height: 1
                                               color: model.row % 3 == 0 ? "black" : "transparent"
                                           }
        
                            color: model.row % 2 ? "lightpink" : "lightblue"
        
        
                            Text {
                                anchors.centerIn: parent
                                text: display
                                font.pointSize: 12
                            }
        
                            TableView.editDelegate: TextField {
                                            anchors.fill: parent
                                            text: display
                                            horizontalAlignment: TextInput.AlignHCenter
                                            verticalAlignment: TextInput.AlignVCenter
                                            Component.onCompleted: selectAll()
        
                                            onAccepted: {
                                                    tableView.commit()
                                                }
        
                                                TableView.onCommit: {
                                                    display = text
                                                }
        
                            }
                        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @arlyn123
        As @JoeCFD has said. If you change a value in a model you must emit dataChanged(...). That is what the table view --- widget or QML --- listens for to know it needs to re-read the value and update. If you do not, it does not refresh of its own accord.

        If you write your own setData() override then either it must do the emit or it must call an inherited setData() which will do that.

        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