问题
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
takeRow
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
takeRow
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