(solved) QAbstractItemModel::endInsertRows: Invalid index run time error in c++



  • Hi, greeting everyone.
    I'm new in Qt, and currently I'm deleveloping a GUI desktop application and I use the QTableView
    However, when I'm doing adding row into the table, the run time error which is "QAbstractItemModel::endInsertRows: Invalid index ( 2 , 2 ) in model TableModel(0x15579700) " occurs
    This problem only occurs when my selection of row to the table is the last row in my QTableView, although it doesn't casue a program crash, but it will show the run time error in Qt creator, and I just wanna figure out why is this happen and is there any solution for this problem? Thanks~

    what I think it's occurs because after each insert of row, it will automatically go to next row, because the go to next row happen before insertion, the index of last row cant be found and the problem happens.
    If my assumption is true, is there anyway to disable the selection of next row after new insertion of row, thanks

    Here is my relevant code
    I create own table model and it's function about insert row into table
    @bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
    {
    Q_UNUSED(index);
    beginInsertRows(QModelIndex(), position, position+rows-1);
    endInsertRows();
    return true;
    }@

    setDate function
    @ bool TableModel::setData(const QModelIndex &index, const reminder &value, int role)
    {

       if (role == Qt::EditRole) {
                aList.insert(aList.getLength()+1, value); /* aList is a linked List to store reminder */
                emit(dataChanged(index, index));
                aList.saveList(listFile);
                return true;
        }
    
    return false;
    

    }@

    it's in my mainwindow, which is the button click slot
    @void MainWindow::on_submitButton_clicked()
    {
    reminder node;
    node.setNote(ui->noteInput->toPlainText().toStdString());
    node.setDate(ui->dateInput->text().toStdString());
    QModelIndex index = mymodel->index(0, 0, QModelIndex());
    mymodel->insertRows(0, 1, index);
    mymodel->setData(index, node, Qt::EditRole);
    ui->tableView->scrollToBottom();
    }@



  • insertRows is the function where you insert actual rows (with default contents) into your list.
    setData is the function where to change the contents of the row (with index "index") to "value" if the role is EditRole.

    EDIT: And maybe you have not overwritten rowCount or other necessary virtual functions (correctly).



  • [quote author="msue" date="1407939332"]insertRows is the function where you insert actual rows (with default contents) into your list.
    setData is the function where to change the contents of the row (with index "index") to "value" if the role is EditRole.

    EDIT: And maybe you have not overwritten rowCount or other necessary virtual functions (correctly).[/quote]

    Thanks, msue, you get the point
    I solve this problem after i move my insert into list function to insertRows there, because if i insert inside setData, the rowCount() return the size of list before insertion, that's the problem
    TQVM :)


Log in to reply
 

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