Here is the answer for move up...
void MainWindow::on_moveupButton_clicked()
{
if( ui->listWidget->selectionModel()->hasSelection() ){
if( ui->listWidget->currentIndex().row() != 0 ){
int row = ui->listWidget->currentIndex().row();
QModelIndex currentIndex
= ui
->listWidget
->currentIndex
();
QString selectedString
= ui
->listWidget
->currentIndex
().
data().
toString();
ui->listWidget->model()->setData( currentIndex, indexAbove.data().toString() );
ui->listWidget->model()->setData( indexAbove, selectedString );
ui->listWidget->setCurrentIndex( indexAbove );
}
}
}
void MainWindow::on_moveupButton_clicked()
{
if( ui->listWidget->selectionModel()->hasSelection() ){
if( ui->listWidget->currentIndex().row() != 0 ){
int row = ui->listWidget->currentIndex().row();
QModelIndex currentIndex = ui->listWidget->currentIndex();
QModelIndex indexAbove = ui->listWidget->model()->index( row-1, 0, QModelIndex() );
QString selectedString = ui->listWidget->currentIndex().data().toString();
ui->listWidget->model()->setData( currentIndex, indexAbove.data().toString() );
ui->listWidget->model()->setData( indexAbove, selectedString );
ui->listWidget->setCurrentIndex( indexAbove );
}
}
}
To copy to clipboard, switch view to plain text mode
Here is the answer for move down...
void MainWindow::on_movedownButton_clicked()
{
if( ui->listWidget->selectionModel()->hasSelection() ){
if( ui->listWidget->currentIndex().row() != ui->listWidget->model()->rowCount()-1 ){
int row = ui->listWidget->currentIndex().row();
QModelIndex currentIndex
= ui
->listWidget
->currentIndex
();
QString selectedString
= ui
->listWidget
->currentIndex
().
data().
toString();
ui->listWidget->model()->setData( currentIndex, indexBelow.data().toString() );
ui->listWidget->model()->setData( indexBelow, selectedString );
ui->listWidget->setCurrentIndex( indexBelow );
}
}
}
void MainWindow::on_movedownButton_clicked()
{
if( ui->listWidget->selectionModel()->hasSelection() ){
if( ui->listWidget->currentIndex().row() != ui->listWidget->model()->rowCount()-1 ){
int row = ui->listWidget->currentIndex().row();
QModelIndex currentIndex = ui->listWidget->currentIndex();
QModelIndex indexBelow = ui->listWidget->model()->index( row+1, 0, QModelIndex() );
QString selectedString = ui->listWidget->currentIndex().data().toString();
ui->listWidget->model()->setData( currentIndex, indexBelow.data().toString() );
ui->listWidget->model()->setData( indexBelow, selectedString );
ui->listWidget->setCurrentIndex( indexBelow );
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks