euraad
euraad

Reputation: 2836

Why can't I add or remove a QListWidgetItem?

I have two ListWidget at my ui and I want to move one QListWidgetItem from availableMeasurementsListWidget to selectedMeasurementListWidget

But this won't work for me. Nothing adds into selectedMeasurementListWidget and the item does not removes from availableMeasurementsListWidget. Why?

That only who works is displaying the text of the qDebug() << item->text();

void ChartSettingsWindow::on_availableMeasurementsListWidget_doubleClicked(const QModelIndex &index)
{
    Q_UNUSED(index);
    QListWidgetItem *item = ui->availableMeasurementsListWidget->currentItem();
    qDebug() << item->text();
   ui->selectedMeasurementListWidget->addItem(item);
    ui->availableMeasurementsListWidget->removeItemWidget(item);
}

Upvotes: 1

Views: 136

Answers (1)

G.M.
G.M.

Reputation: 12879

Note that QListWidget:: removeItemWidget doesn't remove the QListWidgetItem from the QListWidget: it only...

Removes the widget set on the given item.

To remove an item (row) from the list entirely, either delete the item or use takeItem().

So you probably want something like...

auto *available = ui->availableMeasurementsListWidget;
auto *selected  = ui->selectedMeasurementListWidget;
auto *item      = available->currentItem();
selected->addItem(available->takeItem(available->indexFromItem(item).row()));

Upvotes: 1

Related Questions