// 添加子节点
void TreeEdit::addNode()
{
QTreeWidgetItem *curItem=m_tree->currentItem();
QTreeWidgetItem *newItem=new QTreeWidgetItem(curItem);
// 添加子节点
curItem->addChild(newItem);
// 使用QTextEdit的信号
QLineEdit *lineEdit=new QLineEdit(this);
connect(lineEdit,SIGNAL(editingFinished()),this,SLOT(finishEdit()));
// 设置widget
m_tree->setItemWidget(newItem,0,lineEdit);
m_tree->expandItem(curItem);
// 选中项
m_tree->setCurrentItem(newItem);
// 设置默认值
lineEdit->setText("default");
lineEdit->setSelection(0,7);
}
// 编辑完成后对应的槽函数
void TreeEdit::finishEdit()
{
// 编辑完成后,移除widget,还原树
QTreeWidgetItem *curItem=m_tree->currentItem();
QLineEdit *edit=qobject_cast<QLineEdit*>(m_tree->itemWidget(curItem,0));
if(!edit)
return;
QString text=edit->text();
// 移除widget
m_tree->removeItemWidget(curItem,0);
// 还原值
curItem->setText(0,text);
}