Results 1 to 2 of 2

Thread: Moving selected items from QTableWidget1 to QTableWidget2

  1. #1
    Join Date
    Nov 2007
    Thanked 1 Time in 1 Post
    Qt products
    Unix/X11 Windows

    Default Moving selected items from QTableWidget1 to QTableWidget2

    Hi community,
    I need to move the selected items from 1 QTableWidget table to another QTableWidget table by pressing a button (both tables are in the same window).
    The moving part is not the problem, the problem is removing the original rows.

    Below my code:

    Qt Code:
    1. void MainWindow::on_addReportButton_clicked()
    2. {
    3. // availableItemsTable is the source table
    4. // selectedItemsTable is the destination table
    6. QList<QTableWidgetItem*> items = ui->availableItemsTable->selectedItems();
    8. if (items.isEmpty())
    9. {
    10. return;
    11. }
    13. for (auto &item : items)
    14. {
    15. auto selectedItem = ui->availableItemsTable->takeItem(item->row(), 0);
    17. ui->selectedItemsTable->insertRow(ui->selectedItemsTable->rowCount());
    18. ui->selectedItemsTable->setItem(ui->selectedItemsTable->rowCount() - 1, 0, selectedItem);
    19. }
    21. for (auto &item : items)
    22. {
    23. // Here I should remove the rows in the source table, but it removes wrong rows
    24. ui->availableItemsTable->removeRow(item->row());
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    In the loop where I try to remove the rows in the source table, it removes wrong rows.

    Any idea on what I am doing wrong?

    Thanks in advance,
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 844 Times in 831 Posts
    Qt products

    Default Re: Moving selected items from QTableWidget1 to QTableWidget2

    Any idea on what I am doing wrong?
    Two things, probably:

    1 - When you insert the item into table 2, it get a new row number that refers to its position in table 2, not the table it was removed from. The QTableWidgetItem instances are pointers, so their content can be changed by table 2 without the pointer itself changing.

    2 - When you remove a row from table 1, all of the rows that come after that row get renumbered, so if you are removing rows from low to high row number, every row after the first one you remove will be wrong.

    The trick to this is to make a list of the item row numbers -before- you do anything with the items. This list will refer to the row numbers in table 1 and won't be changed when you insert the items into table 2. Next step is to sort the list of row numbers in reverse (descending) order so when you remove rows, you start from the bottom of the table and work your way to the top. This way, none of the rows that come before the one you remove change row numbers.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    franco.amato (13th November 2021)

Similar Threads

  1. Moving up and down the selected rows in a QTableview
    By Raghaw in forum Qt Programming
    Replies: 0
    Last Post: 28th January 2013, 11:04
  2. Moving items in a QTreeWidget
    By sudheer168 in forum Qt Programming
    Replies: 0
    Last Post: 7th November 2011, 15:01
  3. Moving items between models
    By xtal256 in forum Qt Programming
    Replies: 2
    Last Post: 2nd July 2011, 13:39
  4. Moving items in QAbstractItemModel
    By arturo182 in forum Qt Programming
    Replies: 13
    Last Post: 1st June 2011, 00:53
  5. Moving Items with itemChange?
    By konvex in forum Qt Programming
    Replies: 5
    Last Post: 21st November 2008, 14:36


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.