Home > OS >  Why can't I add or remove a QListWidgetItem?
Why can't I add or remove a QListWidgetItem?

Time:02-09

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);
}

CodePudding user response:

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();
to->addItem(from->takeItem(from->indexFromItem(item).row()));
  •  Tags:  
  • Related