Search code examples
c++qtqlistwidgetqlistwidgetitem

How to edit a QListwidgetItem by using custom context menu?


I have a QListWidget named xml_scripts_textbox with some items in my UI, and when i right click on an item in my qlistwidget, a custom context menu appears, and one of the option of this context menu is "Edit the List item", so when this is clicked , i want that particular item in qlistwidget to be editable for once,

How can i do this ?

The code i have tried so far is

context menu code

void MainWindow::on_xml_scripts_textbox_customContextMenuRequested(const QPoint& pos)
{
    QMenu* rMenu = new QMenu(this);
    QAction* edit = new QAction(tr("Edit the List item"), this);

    rMenu->addAction(edit);
    connect(edit, SIGNAL(triggered()), this, SLOT(edithelp()));
    rMenu->exec(cursor().pos());

}

code for edithelp(), the slot function which will make the listitem editable

void MainWindow::edithelp()
{
    QListWidgetItem* item_1 = ui->xml_scripts_textbox->takeItem(ui->xml_scripts_textbox->currentRow());
    item_1->setFlags(Qt::ItemIsEditable);  // still not getting editable ?? why ??
}

Solution

  • Did you set item is editable?

    item->setFlags(item->flags() | Qt::ItemIsEditable)