how can i create a new QTreeWidgetItem and then input its name in the created item?



  • how can i create a new QTreeWidgetItem and then input its name in the created item?



  • Hi,

    you have to make the Item Editable



  • void TreeEdit::addNode()
    {
    QTreeWidgetItem *curItem=m_tree->currentItem();
    QTreeWidgetItem *newItem=new QTreeWidgetItem(curItem);

    curItem->addChild(newItem);  
    
    QLineEdit *lineEdit=new QLineEdit(this);  
    connect(lineEdit,SIGNAL(editingFinished()),this,SLOT(finishEdit()));  
    
    m_tree->setItemWidget(newItem,0,lineEdit);  
    m_tree->expandItem(curItem);  
    
    m_tree->setCurrentItem(newItem);  
    
    lineEdit->setText("default");  
    lineEdit->setSelection(0,7);  
    

    }

    void TreeEdit::finishEdit()
    {
    QTreeWidgetItem *curItem=m_tree->currentItem();
    QLineEdit edit=qobject_cast<QLineEdit>(m_tree->itemWidget(curItem,0));
    if(!edit)
    return;

    QString text=edit->text();  
    
    m_tree->removeItemWidget(curItem,0);  
    
    curItem->setText(0,text);  
    

    }



  • is there any better way to do this?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.