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:
void MainWindow::on_addReportButton_clicked()
{
// availableItemsTable is the source table
// selectedItemsTable is the destination table
QList<QTableWidgetItem*> items = ui->availableItemsTable->selectedItems();
if (items.isEmpty())
{
return;
}
for (auto &item : items)
{
auto selectedItem = ui->availableItemsTable->takeItem(item->row(), 0);
ui->selectedItemsTable->insertRow(ui->selectedItemsTable->rowCount());
ui->selectedItemsTable->setItem(ui->selectedItemsTable->rowCount() - 1, 0, selectedItem);
}
for (auto &item : items)
{
// Here I should remove the rows in the source table, but it removes wrong rows
ui->availableItemsTable->removeRow(item->row());
}
}
void MainWindow::on_addReportButton_clicked()
{
// availableItemsTable is the source table
// selectedItemsTable is the destination table
QList<QTableWidgetItem*> items = ui->availableItemsTable->selectedItems();
if (items.isEmpty())
{
return;
}
for (auto &item : items)
{
auto selectedItem = ui->availableItemsTable->takeItem(item->row(), 0);
ui->selectedItemsTable->insertRow(ui->selectedItemsTable->rowCount());
ui->selectedItemsTable->setItem(ui->selectedItemsTable->rowCount() - 1, 0, selectedItem);
}
for (auto &item : items)
{
// Here I should remove the rows in the source table, but it removes wrong rows
ui->availableItemsTable->removeRow(item->row());
}
}
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
Bookmarks