How to Move Up a Selected Row in Qt

10,477

Solution 1

Use Qt documentation for QStandartItemModel - QStandardItemModel Class

  1. takeRow
  2. insertRow

Solution 2

If you use Qt5 you can take a look on this function:

bool QAbstractItemModel::moveRow(const QModelIndex & sourceParent, int sourceColumn, const QModelIndex & destinationParent, int destinationChild)

"On models that support this, moves sourceColumn from sourceParent to destinationChild under destinationParent. Returns true if the columns were successfully moved; otherwise returns false."

Solution 3

Here is a short utility I use for moving the Qt::DisplayData of a row in QAbstractItemModel:

void CopyRowData( QAbstractItemModel * pModelDst, const QAbstractItemModel * pModelSrc, int nRowDst, int nRowSrc, const QModelIndex &parentDst /*= QModelIndex()*/, const QModelIndex &parentSrc /*= QModelIndex()*/, int nRole /*= Qt::DisplayRole*/ )
{
    if (parentSrc.isValid())
        assert(parentSrc.model() == pModelSrc);
    if (parentDst.isValid())
        assert(parentDst.model() == pModelDst);

    int nCols = pModelSrc->columnCount(parentSrc);

    for (int i = 0; i < nCols ; ++i)
        pModelDst->setData(pModelDst->index(nRowDst, i, parentDst), pModelSrc->index(nRowSrc, i, parentSrc).data(nRole), nRole);
}

bool MoveModelRows( QAbstractItemModel * pModel, int nSrcRow, int nDstRow, int nCount /*= 1*/, const QModelIndex &parent /*= QModelIndex()*/ )
{
    if (nSrcRow < 0 || nSrcRow >= pModel->rowCount(parent) ||
        nDstRow < 0 || nDstRow >= pModel->rowCount(parent))
        return false;

    if (nSrcRow == nDstRow)
        return true;

    int nDstRowNew = nSrcRow > nDstRow ? nDstRow : nDstRow + 1;

    if (!pModel->insertRows(nDstRowNew, nCount, parent))
        return false;

    int nSrcRowNew = nSrcRow > nDstRow ? nSrcRow + nCount : nSrcRow;

    for (int i = 0; i < nCount; ++i)
        CopyRowData(pModel, pModel, nDstRowNew + i, nSrcRowNew + i, parent, parent);

    pModel->removeRows(nSrcRowNew, nCount, parent);

    return true;
}
Share:
10,477
New Moon
Author by

New Moon

Updated on June 07, 2022

Comments

  • New Moon
    New Moon about 2 years

    I have a QTableView with 3 rows and 2 columns. (Here I am using a QStandardItemModel). I want to move up/move down a single row when a QPushButton is clicked. How can I move up/down a row in QTableView?

    Thanks for your reply vahancho. I have already tried using QAbstractItemModel::moveRow, but it doesn't work:

       int currentRow = ui->tableView->currentIndex().row();
       QModelIndex sourceParent = ui->tableView->model()->index(ui->tableView->selectionModel()->currentIndex().row(),0);
       QModelIndex destinationParent = ui->tableView->model()->index(ui->tableView->selectionModel()->currentIndex().row()+1,0);
       ui->tableView->model()->moveRow(sourceParent,currentRow, destinationParent,destinationParent.row());
    
  • Zaiborg
    Zaiborg almost 11 years
    i agree, QStandardItemModel is more convinient :)
  • Dmitry Sazonov
    Dmitry Sazonov almost 11 years
    I posted solution to author, but he deleted my post. Problem was that TS didn't know about type casting and he couldn't cast QModelIndex::model() to QStandartItemModel.
  • eric
    eric over 9 years
    abstractitemmodel, so it means you still need to implement it yourself, right?
  • vahancho
    vahancho over 9 years
    @neuronet, yes. Actually, to make moveRow() work, you need to implement QAbstractItemModel::moveRows() virtual function.
  • eric
    eric over 9 years
    @SaZ who is TS, what do you mean he deleted the post, he can't do that with such low rep can he?
  • Dmitry Sazonov
    Dmitry Sazonov over 9 years
    TS == topic starter. He posted an answer, and i commented it. So he deleted his answer and my comment became invisible.
  • Rotsiser Mho
    Rotsiser Mho over 4 years
    This creates unnecessary copies using somewhat expensive heap allocations.
  • Rotsiser Mho
    Rotsiser Mho over 4 years
    It is strange that this virtual function is available but no Qt classes implement it.