How to Move Up a Selected Row in Qt

╄→гoц情女王★ 提交于 2019-12-10 18:54:22

问题


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());

回答1:


Use Qt documentation for QStandartItemModel - QStandardItemModel Class

  1. takeRow
  2. insertRow



回答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."




回答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;
}



回答4:


I think this would help:

for(int colId=0;colId<ui->tableWidget->columnCount();++colId)
{
    QTableWidgetItem *item1=new QTableWidgetItem( *ui->tableWidget->item(id_row1,colId) );
    QTableWidgetItem *item2=new QTableWidgetItem( *ui->tableWidget->item(id_row2,colId) );

    ui->tableWidget->setItem(id_row1,colId, item2 );
    ui->tableWidget->setItem(id_row2,colId, item1 );
}



回答5:


Using the idea that SaZ said:

Use Qt documentation for QStandartItemModel - QStandardItemModel Class

  1. takeRow
  2. insertRow

This is the code I did for this purpose:

QModelIndexList selection = ui->tableView->selectionModel()->selectedRows();
int row = selection[0].row();
QList<QStandardItem*> itemList = ui->tableView->model()->takeRow(row);
ui->tableView->model()->insertRow(row-1,itemList);

I hope this help you.



来源:https://stackoverflow.com/questions/18694060/how-to-move-up-a-selected-row-in-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!