Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set and use setdata() to add new data to custom QAbstractTableModel
Forum Updated to NodeBB v4.3 + New Features

How to set and use setdata() to add new data to custom QAbstractTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 6.6k Views
  • 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.
  • D Offline
    D Offline
    Dimis
    wrote on 17 Apr 2018, 11:00 last edited by VRonin
    #1

    Hi all,

    I've created my custom model subclassing the QAbstractTableModel which is populated in a QTableView. I've re-implemented the necessary functions but something I do wrong obviously and the setdata function doesn't work. Below is my code, please could you help me with this?

    header file for custom model:

    class tablemodel : public QAbstractTableModel
    {
        Q_OBJECT
    
    public:
        
            tablemodel(QObject *parent=0);
        
            int columnCount(const QModelIndex & parent = QModelIndex()) const override;
        
            int rowCount(const QModelIndex & parent = QModelIndex()) const override;
        
            QVariant data(const QModelIndex & index, int role=Qt::DisplayRole) const override;
       
        
            bool insertRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;
        
            bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;
        
            bool setData(const QModelIndex &index, const QVariant value, int role);
        
            Qt::ItemFlags flags(const QModelIndex &index) const;
    };
    

    source file for model and functions definitions:

    tablemodel::tablemodel(QObject * parent /* = 0*/)
        : QAbstractTableModel(parent)
    {
    }
    
    int tablemodel::columnCount(const QModelIndex &/*parent = QModelIndex()*/) const
    {
        return 3;
    }
    int tablemodel::rowCount(const QModelIndex & /* parent = QModelIndex()*/) const
    {
        return 1;
    }
    
    QVariant tablemodel::data(const QModelIndex & index, int role) const
    {
         int row=index.row();
         int col=index.column();
        if (role == Qt::DisplayRole)
           {
            return    QString("%1, %2")
                          .arg(row + 1)
                          .arg(col +1);
           }
        return QVariant();
    }
    
    
    bool tablemodel::insertRows(int position, int rows, const QModelIndex &parent)
    {
       rows=1
            beginInsertRows(parent, position, position + rows - 1);
            endInsertRows();
            return true;
    }
    
    bool tablemodel::removeRows(int position, int rows, const QModelIndex &parent)
    {
           beginRemoveRows(QModelIndex(), position, position + rows - 1);
           endRemoveRows();
           return true;
    }
    
    bool tablemodel::setData(const QModelIndex &index, const QVariant value, int role)
    { 
        if (index.isValid() && role == Qt::EditRole)
            {
                int row = index.row();
    
                value.toString()="enter test text";
    
                emit dataChanged(index, index);
                return true;
            }
            return false;
    }
    
    Qt::ItemFlags tablemodel::flags(const QModelIndex &index) const
    {
        return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    }
    

    and in the mainwindow source file, I have a button which inserts a new row in the tableview and add the new text but the setdata() for adding the text doesn't actually works:

    void MainWindow::insert_btn_released()
    {
      QModelIndex selectedRow = table->selectionModel()->currentIndex();
    
        mymodel->insertRows(selectedRow.row(),1);
        qDebug() << " SELECTED ROW " << selectedRow.row();
       QModelIndex newidx = mymodel->index(selectedRow.row()+1,0);
    
       mymodel->setData(newidx,"test text",Qt::DisplayRole);
    
    }
    

    Please any help would be really appreciated.

    Thank you in advance.

    J V 2 Replies Last reply 17 Apr 2018, 11:03
    0
    • D Dimis
      17 Apr 2018, 11:00

      Hi all,

      I've created my custom model subclassing the QAbstractTableModel which is populated in a QTableView. I've re-implemented the necessary functions but something I do wrong obviously and the setdata function doesn't work. Below is my code, please could you help me with this?

      header file for custom model:

      class tablemodel : public QAbstractTableModel
      {
          Q_OBJECT
      
      public:
          
              tablemodel(QObject *parent=0);
          
              int columnCount(const QModelIndex & parent = QModelIndex()) const override;
          
              int rowCount(const QModelIndex & parent = QModelIndex()) const override;
          
              QVariant data(const QModelIndex & index, int role=Qt::DisplayRole) const override;
         
          
              bool insertRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;
          
              bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;
          
              bool setData(const QModelIndex &index, const QVariant value, int role);
          
              Qt::ItemFlags flags(const QModelIndex &index) const;
      };
      

      source file for model and functions definitions:

      tablemodel::tablemodel(QObject * parent /* = 0*/)
          : QAbstractTableModel(parent)
      {
      }
      
      int tablemodel::columnCount(const QModelIndex &/*parent = QModelIndex()*/) const
      {
          return 3;
      }
      int tablemodel::rowCount(const QModelIndex & /* parent = QModelIndex()*/) const
      {
          return 1;
      }
      
      QVariant tablemodel::data(const QModelIndex & index, int role) const
      {
           int row=index.row();
           int col=index.column();
          if (role == Qt::DisplayRole)
             {
              return    QString("%1, %2")
                            .arg(row + 1)
                            .arg(col +1);
             }
          return QVariant();
      }
      
      
      bool tablemodel::insertRows(int position, int rows, const QModelIndex &parent)
      {
         rows=1
              beginInsertRows(parent, position, position + rows - 1);
              endInsertRows();
              return true;
      }
      
      bool tablemodel::removeRows(int position, int rows, const QModelIndex &parent)
      {
             beginRemoveRows(QModelIndex(), position, position + rows - 1);
             endRemoveRows();
             return true;
      }
      
      bool tablemodel::setData(const QModelIndex &index, const QVariant value, int role)
      { 
          if (index.isValid() && role == Qt::EditRole)
              {
                  int row = index.row();
      
                  value.toString()="enter test text";
      
                  emit dataChanged(index, index);
                  return true;
              }
              return false;
      }
      
      Qt::ItemFlags tablemodel::flags(const QModelIndex &index) const
      {
          return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
      }
      

      and in the mainwindow source file, I have a button which inserts a new row in the tableview and add the new text but the setdata() for adding the text doesn't actually works:

      void MainWindow::insert_btn_released()
      {
        QModelIndex selectedRow = table->selectionModel()->currentIndex();
      
          mymodel->insertRows(selectedRow.row(),1);
          qDebug() << " SELECTED ROW " << selectedRow.row();
         QModelIndex newidx = mymodel->index(selectedRow.row()+1,0);
      
         mymodel->setData(newidx,"test text",Qt::DisplayRole);
      
      }
      

      Please any help would be really appreciated.

      Thank you in advance.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Apr 2018, 11:03 last edited by
      #2

      @Dimis said in How to set and use setdata() to add new data to custom QAbstractTableModel:

      value.toString()="enter test text";

      What is this line supposed to do?
      You're assigning to a temporary value...

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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dimis
        wrote on 17 Apr 2018, 11:13 last edited by
        #3

        I was trying to assign some text in the value variable in order to be displayed.

        1 Reply Last reply
        0
        • D Dimis
          17 Apr 2018, 11:00

          Hi all,

          I've created my custom model subclassing the QAbstractTableModel which is populated in a QTableView. I've re-implemented the necessary functions but something I do wrong obviously and the setdata function doesn't work. Below is my code, please could you help me with this?

          header file for custom model:

          class tablemodel : public QAbstractTableModel
          {
              Q_OBJECT
          
          public:
              
                  tablemodel(QObject *parent=0);
              
                  int columnCount(const QModelIndex & parent = QModelIndex()) const override;
              
                  int rowCount(const QModelIndex & parent = QModelIndex()) const override;
              
                  QVariant data(const QModelIndex & index, int role=Qt::DisplayRole) const override;
             
              
                  bool insertRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;
              
                  bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;
              
                  bool setData(const QModelIndex &index, const QVariant value, int role);
              
                  Qt::ItemFlags flags(const QModelIndex &index) const;
          };
          

          source file for model and functions definitions:

          tablemodel::tablemodel(QObject * parent /* = 0*/)
              : QAbstractTableModel(parent)
          {
          }
          
          int tablemodel::columnCount(const QModelIndex &/*parent = QModelIndex()*/) const
          {
              return 3;
          }
          int tablemodel::rowCount(const QModelIndex & /* parent = QModelIndex()*/) const
          {
              return 1;
          }
          
          QVariant tablemodel::data(const QModelIndex & index, int role) const
          {
               int row=index.row();
               int col=index.column();
              if (role == Qt::DisplayRole)
                 {
                  return    QString("%1, %2")
                                .arg(row + 1)
                                .arg(col +1);
                 }
              return QVariant();
          }
          
          
          bool tablemodel::insertRows(int position, int rows, const QModelIndex &parent)
          {
             rows=1
                  beginInsertRows(parent, position, position + rows - 1);
                  endInsertRows();
                  return true;
          }
          
          bool tablemodel::removeRows(int position, int rows, const QModelIndex &parent)
          {
                 beginRemoveRows(QModelIndex(), position, position + rows - 1);
                 endRemoveRows();
                 return true;
          }
          
          bool tablemodel::setData(const QModelIndex &index, const QVariant value, int role)
          { 
              if (index.isValid() && role == Qt::EditRole)
                  {
                      int row = index.row();
          
                      value.toString()="enter test text";
          
                      emit dataChanged(index, index);
                      return true;
                  }
                  return false;
          }
          
          Qt::ItemFlags tablemodel::flags(const QModelIndex &index) const
          {
              return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
          }
          

          and in the mainwindow source file, I have a button which inserts a new row in the tableview and add the new text but the setdata() for adding the text doesn't actually works:

          void MainWindow::insert_btn_released()
          {
            QModelIndex selectedRow = table->selectionModel()->currentIndex();
          
              mymodel->insertRows(selectedRow.row(),1);
              qDebug() << " SELECTED ROW " << selectedRow.row();
             QModelIndex newidx = mymodel->index(selectedRow.row()+1,0);
          
             mymodel->setData(newidx,"test text",Qt::DisplayRole);
          
          }
          

          Please any help would be really appreciated.

          Thank you in advance.

          V Offline
          V Offline
          VRonin
          wrote on 17 Apr 2018, 11:20 last edited by
          #4

          I don't think you got the model design concept at all. Have a read of chapters 3 and 4 of Advanced Qt Programming or just use QStandardItemModel and end your pain

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3

          1/4

          17 Apr 2018, 11:00

          • Login

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