PDA

View Full Version : Selected Rows moveUp/moveDown in QTableWidget



imagineryhead
13th September 2010, 17:57
Hello folks,
I've a quick question. I need to moveUp selected rows in tablewidget.
I guess miss something, not working!



void MainWindow::moveUp()
{
QItemSelectionModel *smodel= ui.tableView->selectionModel();
if (smodel==0) {
return;
}
QModelIndexList selectionList =smodel->selectedRows();
QModelIndex idx = selectionList.first();
int row=idx.row()-1;
if (row<0) {
row=0;
}
QModelIndex previous=model->index(row,idx.column());
ui.tableView->selectionModel()->select(previous, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
ui.tableView->setCurrentIndex(model->index(row,idx.column()));
ui.tableView->scrollTo(ui.tableView->currentIndex());

}

wysota
13th September 2010, 19:46
What is wrong exactly? Code doesn't compile, app crashes or the code does nothing? And are you using QTableWidget or QTableView?

imagineryhead
13th September 2010, 21:37
try to convert QTable codes to QTableWidget,
same crash problem here,

eventually, need simple codes which may moveUp and moveDown selected rows in QTableWidget

wysota
13th September 2010, 22:55
I don't understand you. Does the code crash or does it simply not do what you expect of it?

imagineryhead
14th September 2010, 09:34
selectionList doesnt get any values,

QModelIndexList selectionList =smodel->selectedRows();

Lykurg
14th September 2010, 10:00
QModelIndexList selectionList =smodel->selectedRows();From the docs
Returns the indexes in the given column for the rows where all columns are selected. Are you sure you set the corresponding selection behavior on the table? Or better use selectdIndexes().

imagineryhead
14th September 2010, 11:32
yes, selectedIndexes() works,
but still stuck,

try to move up (swap) selected rows, takeItem() and setItem() works for single selection, how they can be used for multi selection in loop ?

Thanks,

wysota
14th September 2010, 11:45
Show us your current code for moving the item, please.

imagineryhead
14th September 2010, 12:20
QItemSelectionModel *smodel= ui.tableWidget->selectionModel();
if (smodel==0) {
return;
}

QModelIndexList selectionList =smodel->selectedIndexes();
int row=selectionList.last().row();

QTableWidgetItem *item = ui.tableWidget->takeItem(row,0);
QTableWidgetItem *itemBelow = ui.tableWidget->takeItem(row+1,0);
ui.tableWidget->setItem(row,0,itemBelow);
ui.tableWidget->setItem(row+1,0,item);
ui.tableWidget->setCurrentItem(item);

wysota
14th September 2010, 13:01
Look at QTableWidget::selectedItems(). You can use it to iterate over items and move them one by one just like you did with the single item.

imagineryhead
14th September 2010, 14:13
QItemSelectionModel *smodel= ui.tableWidget->selectionModel();
if (smodel==0) {
return;
}

QModelIndexList selectionList =smodel->selectedIndexes();

QItemSelection selection( ui.tableWidget->selectionModel()->selection() );
QList<int> rows;
foreach( const QModelIndex & index, selection.indexes() ) {
rows.append( index.row() );
}

qSort( rows );

int prev = -1;
for( int i = selectionList.length() - 1; i >= 0; i -= 1 ) {
int current = rows[i];
if( current != prev ) {
QTableWidgetItem *item = ui.tableWidget->takeItem(current,0);
QTableWidgetItem *itemBelow = ui.tableWidget->takeItem(current+1,0);
ui.tableWidget->setItem(current,0,itemBelow);
ui.tableWidget->setItem(current+1,0,item);

prev = current;
}
}


Now, it works. moves selected!

seems still have little problem; my selected blues not move wth current moving.

wysota
14th September 2010, 14:16
Why are you using QItemSelectionModel here? You can substitute first 15 lines of your code with one line that uses the method I gave you in my previous post.

imagineryhead
14th September 2010, 16:01
QList<QTableWidgetItem *> selection = ui.tableWidget->selectedItems();

int prev = -1;
for( int i = selection.length() - 1; i >= 0; i -= 1 ) {
int current = selection[i]->row();
if( current != prev ) {
QTableWidgetItem *item = ui.tableWidget->takeItem(current,0);
QTableWidgetItem *itemBelow = ui.tableWidget->takeItem(current+1,0);
ui.tableWidget->setItem(current,0,itemBelow);
ui.tableWidget->setItem(current+1,0,item);

prev = current;
}
}

yes ,you are right, and now its more simple!

but my selected blues not move wth current moving. setSelected() but how?