Search code examples
pythonqtpyqtqlistwidget

Move items from one QListWidget to another


I have to QListWidgets, one source list, one destiny list and one button. Whenever the button is clicked, I want the selected item(s) from the source list to be removed and inserted into the destiny list. I tried to source_list.removeWidgetItem(aSelectedItem) but that does not even do a thing. :( What am I doing wrong? Do I need to update the list afterwards somehow?


Solution

  • takeItem will take the item from the source_list and give you a pointer to it you can use to append it to your destination list. Something like:

    source_list = new QListWidget();
    dest_list = new QListWidget();
    new QListWidgetItem(tr("Oak"), source_list);
    new QListWidgetItem(tr("Birch"), source_list);
    connect(source_list, SIGNAL(clicked(QModelIndex)), this, SLOT(swapEntry(QModelIndex)));
    
    
    void MyWidget::swapEntry(QModelIndex index)
    {
        dest_list->insertItem(dest_list->count(), source_list->takeItem(index.row()));
    }