Results 1 to 13 of 13

Thread: Selected Rows moveUp/moveDown in QTableWidget

  1. #1
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Selected Rows moveUp/moveDown in QTableWidget

    Hello folks,
    I've a quick question. I need to moveUp selected rows in tablewidget.
    I guess miss something, not working!


    Qt Code:
    1. void MainWindow::moveUp()
    2. {
    3. QItemSelectionModel *smodel= ui.tableView->selectionModel();
    4. if (smodel==0) {
    5. return;
    6. }
    7. QModelIndexList selectionList =smodel->selectedRows();
    8. QModelIndex idx = selectionList.first();
    9. int row=idx.row()-1;
    10. if (row<0) {
    11. row=0;
    12. }
    13. QModelIndex previous=model->index(row,idx.column());
    14. ui.tableView->selectionModel()->select(previous, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    15. ui.tableView->setCurrentIndex(model->index(row,idx.column()));
    16. ui.tableView->scrollTo(ui.tableView->currentIndex());
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 13th September 2010 at 19:42.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    What is wrong exactly? Code doesn't compile, app crashes or the code does nothing? And are you using QTableWidget or QTableView?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    try to convert QTable codes to QTableWidget,
    same crash problem here,

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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    I don't understand you. Does the code crash or does it simply not do what you expect of it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    selectionList doesnt get any values,

    QModelIndexList selectionList =smodel->selectedRows();

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    Quote Originally Posted by imagineryhead View Post
    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().

  7. #7
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    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,

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    Show us your current code for moving the item, please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    Qt Code:
    1. QItemSelectionModel *smodel= ui.tableWidget->selectionModel();
    2. if (smodel==0) {
    3. return;
    4. }
    5.  
    6. QModelIndexList selectionList =smodel->selectedIndexes();
    7. int row=selectionList.last().row();
    8.  
    9. QTableWidgetItem *item = ui.tableWidget->takeItem(row,0);
    10. QTableWidgetItem *itemBelow = ui.tableWidget->takeItem(row+1,0);
    11. ui.tableWidget->setItem(row,0,itemBelow);
    12. ui.tableWidget->setItem(row+1,0,item);
    13. ui.tableWidget->setCurrentItem(item);
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    Qt Code:
    1. QItemSelectionModel *smodel= ui.tableWidget->selectionModel();
    2. if (smodel==0) {
    3. return;
    4. }
    5.  
    6. QModelIndexList selectionList =smodel->selectedIndexes();
    7.  
    8. QItemSelection selection( ui.tableWidget->selectionModel()->selection() );
    9. QList<int> rows;
    10. foreach( const QModelIndex & index, selection.indexes() ) {
    11. rows.append( index.row() );
    12. }
    13.  
    14. qSort( rows );
    15.  
    16. int prev = -1;
    17. for( int i = selectionList.length() - 1; i >= 0; i -= 1 ) {
    18. int current = rows[i];
    19. if( current != prev ) {
    20. QTableWidgetItem *item = ui.tableWidget->takeItem(current,0);
    21. QTableWidgetItem *itemBelow = ui.tableWidget->takeItem(current+1,0);
    22. ui.tableWidget->setItem(current,0,itemBelow);
    23. ui.tableWidget->setItem(current+1,0,item);
    24.  
    25. prev = current;
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 


    Now, it works. moves selected!

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

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Selected Rows moveUp/moveDown in QTableWidget

    Qt Code:
    1. QList<QTableWidgetItem *> selection = ui.tableWidget->selectedItems();
    2.  
    3. int prev = -1;
    4. for( int i = selection.length() - 1; i >= 0; i -= 1 ) {
    5. int current = selection[i]->row();
    6. if( current != prev ) {
    7. QTableWidgetItem *item = ui.tableWidget->takeItem(current,0);
    8. QTableWidgetItem *itemBelow = ui.tableWidget->takeItem(current+1,0);
    9. ui.tableWidget->setItem(current,0,itemBelow);
    10. ui.tableWidget->setItem(current+1,0,item);
    11.  
    12. prev = current;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

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

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

Similar Threads

  1. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 13:49
  2. Show selected rows
    By mpi in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2010, 02:59
  3. removing of selected rows
    By MrShahi in forum Qt Programming
    Replies: 4
    Last Post: 10th June 2008, 16:05
  4. Replies: 5
    Last Post: 2nd April 2007, 09:57
  5. Get list of selected rows from QTableView
    By jnk5y in forum Qt Programming
    Replies: 8
    Last Post: 17th February 2006, 17:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.