Important: Please read the Qt Code of Conduct -

How to allow the insite renaming of a QTreeWidgetItem

  • Hello:
    I have filled a QTreeView by code, inserting a collection of QTreeWidgetItem. I am inserting by code new items in the TReeView, and I need to create them in 'edition mode' to allow the user the renaming of the new node,
    such as is made by the 'new folder' command in Windows.

    I don´t how to do it, is it possible ? nad how to do it ?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You can simply call the edit function from QTreeWidget

  • Moderators

    Don't mix QTreeView and QTreeWidgetItems. The latter should be used with QTreeWidget, not QTreeView.

    That being said, when you create a QTreeWidgetItem remember to "setFlags": to contain Qt::ItemIsEditable (apart from the default ones) and then call "editItem":

  • Thank you to Chris and Sgaist, but it doesn´t work. I show you my code below.
    After the editItem() call, the item's box doesn´t open.
    Moreover, whenever I make double-clicl on any item, the item doesn´t
    change to editing, such as any standard tree view in Windows.

     // We get the root node to insert a new palette
     usersPaletteRoot = ui.m_TreePalView->topLevelItem(1);
     // We create the novel palette entry
     newChild = new QTreeWidgetItem();
     newChild->setText(0, "New palette");
     newChild->setFlags(newChild->flags() | 
      Qt::ItemIsEditable | Qt::ItemIsEnabled
      | Qt::ItemIsSelectable);
     // We insert a new row in blank in the palette's list 
     disconnect(ui.m_TreePalView, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
      this, SLOT(OnPaletteSelected(QTreeWidgetItem*, int)));
     ui.m_TreePalView->editItem(newChild, 0);

  • Moderators

    Just checked this:

    auto item = new QTreeWidgetItem(ui->treeWidget, QStringList {"Foo"});
    item->setFlags(item->flags() | Qt::ItemIsEditable);

    Works as expected. Your code is similar so there's got to be some problem elsewhere.

    usersPaletteRoot = ui.m_TreePalView->topLevelItem(1);

    Is there always an element with index 1?

Log in to reply