Insert row into QSQLTableModel



  • Hi Folks,

    I would like to insert a new row into QSQLTableModel instance via a subclass of QStyledItemDelegate:

    void EditorDelegate::insertRow(QAbstractItemModel* model, const QModelIndex& index) const {
    model->insertRow(index.row() + 1);
    model->setData(model->index(index.row() + 1, 0), QUuid::createUuid());
    }
    

    In the second line would like to set the primary key.
    The problem is that after the insertion a new row will be displayed without the generated UUID and the row isn't clickable so this delegate isn't draw itself on the top of the new cell.

    Can anybody help me? Or please sign me if more information required.

    Regards,
    Norbert


  • Qt Champions 2016

    @moravas
    Hello,
    It this UUID supposed to be visible in the column? If so you might want to set its role. Something like this:

    QUuid id = QUuid::createUuid();
    model->setData(model->index(index.row() + 1, 0), id);
    model->setData(model->index(index.row() + 1, 0), id.toString(), Qt::DisplayRole);
    

    Kind regards.



  • Hi,

    thank you: I checked your solution without success :(


  • Lifetime Qt Champion

    Hi,

    Following Qt's documentation, insertRow insert a single row before the row given. So unless I'm mistaken, you are calling setData on the wrong index (off by one)



  • How do you call "EditorDelegate::insertRow" ? Do you pass the QModelIndex of last row in the model?

    Also you are setting the data in wrong index as mentioned my SGaist


Log in to reply
 

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