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



  • 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.


  • Moderators

    @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...



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



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.