I'm developing a Qt application with QTreeWidget on Qt Designer form. User can press add-new-item button and new item will appear with default name, after that user must enter item's name.
So this is my code:
void MyFormClass::on_addNewItemButton_clicked()
{
auto newItem = new QTreeWidgetItem({ _defaultName });
newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
ui->tree->addTopLevelItem(newItem);
ui->tree->editItem(newItem);
}
In MyFormClass i also catch itemChanged signal to do some operations with newly created item and it's name:
void MyFormClass::on_tree_itemChanged(QTreeWidgetItem *item, int column)
{
if (item->text(0).isEmpty()) {
...
} else {
...
}
}
And everything works fine except case when user doesn't change anything and just press Enter/left-click somewhere. This case QTreeWidget [probably] check that item has not been actually changed and do not emits proper signal.
So any ideas how i can create new item, then allow user to edit it immediately and finally catch any edit result (i.e. same with default)? Maybe using QTreeView will help?
Qt 5.4.2, C++11, Linux/Windows platforms
Bonus questions: why Qt designed this way? I mean, isn't it my affair to check whether item has changed or not? OK, signal is called itemChanged, but why there is no editFinished signal or something?