I wanted the same functionality for my app. So I searched through internet and didnt find anything. So I had to do it.
So, you can do it with QTableWidget by using the cellEntered(int,int); then takeItem(row, col) and then setItem()...
@connect(ui->tableWidget, SIGNAL(cellEntered(int,int)), this, SLOT(cellEnteredSlot(int,int)));@
@void MainWindow::cellEnteredSlot(int row, int column){
int colCount=ui->tableWidget->columnCount();
int rowsel;
if(ui->tableWidget->currentIndex().row()<row) rowsel=row-1; //down
else if(ui->tableWidget->currentIndex().row()>row) rowsel=row+1; //up
else return;
QList<QTableWidgetItem*> rowItems,rowItems1;
for (int col = 0; col < colCount; ++col)
{
rowItems << ui->tableWidget->takeItem(row, col);
rowItems1 << ui->tableWidget->takeItem(rowsel, col);
}
for (int cola = 0; cola < colCount; ++cola)
{
ui->tableWidget->setItem(rowsel, cola, rowItems.at(cola));
ui->tableWidget->setItem(row, cola, rowItems1.at(cola));
}
}@
Warning: I think theres a bug.
Although it works fine by clicking and holding the left mouse button and moving mouse up or down, it also works with Mouse WHEEL Scroll.
I have not figure it out yet on how to check if mouse wheel was triggered so not to move selected row.
The doc says:
bq. This signal is only emitted when mouseTracking is turned on, or when a mouse button is pressed while moving into an item.
BUT when you scroll the mouse wheel is not a mouse button press action.
So I think its a BUG. Should I submit it?