Qt World Summit: Register Today!

create a new QTreeWidgetItem in QTreeWidget, edit its name in QTreeWidgetItem, if the name is legal, create the new QTreeWidgetItem, if not, remove it.

  • i want to create a new QTreeWidgetItem in QTreeWidget, edit its name in QTreeWidgetItem, if the name is legal, create the new QTreeWidgetItem, if not, remove it.
    and i found code like this...

    void TreeEdit::addNode()  
        QTreeWidgetItem *curItem=m_tree->currentItem();  
        QTreeWidgetItem *newItem=new QTreeWidgetItem(curItem);  
        QLineEdit *lineEdit=new QLineEdit(this);  
    void TreeEdit::finishEdit()  
        QTreeWidgetItem *curItem=m_tree->currentItem();  
        QLineEdit * edit = qobject_cast< QLineEdit * >(m_tree->itemWidget(curItem,0));  
        QString text=edit->text();  

    however, when i do some custom things in finishEdit(), which i do emit some signals, which make the app crash in app.exec(); which is really wired......

    so i want to ask is there any other way to do this ?
    and i would like to have a try.

  • This post is deleted!

  • Your code should not even compile.

    1. QTreeWidgetItem *curItem=m_tree->currentItem();
      QLineEdit edit=qobject_cast<QLineEdit>(m_tree->itemWidget(curItem,0));
      You are trying to cast pointer to class object.

    2. There is no any need to create lineEdit and set it with setItemWidget
      newItem->setText(0, tr("Cities"));

    Read documentation on QTreeWidgetItem, also it might be helpful to create and populate QTreeWidget in the designer and then check code it generates.

  • u don't get what i want to do ......

  • Hi,

    I didn't tried your code but you can get what you want creating a custom delegate for your Tree.
    In that case you can re-implement createEditor() to return a QLineEdit and setModelData() to handle the text inserted

  • what should i do when i want to do many things include emit signals when editFinishing?

  • Hi,

    the setModelData is called when the editing is finished; so you can check the inserted text in that method.
    In example in our program we delete automatically the Item if the user insert/change an empty text

    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
        QLineEdit *le = qobject_cast<QLineEdit*>(editor);
        if (le && le->text().isEmpty()) {
        QStyledItemDelegate::setModelData(editor, model, index);

    you could do something similar

  • If you need control over editor widget you may use custom model/view delegate.
    That allows you to intercept editor widget creation.
    That what people above tried to tell you.
    You can search for using delegates on your own,
    But mostly likely you do not need it.
    QTreeWidget provides sufficient notifications..
    In you case connecting to QTreeWidget::itemChanged signal may be sufficient.
    Bu who knows if you do not tell us .

  • thank you for all !

  • @alex_malyu

    i want to create a new QTreeWidgetItem in QTreeWidget, edit its name in QTreeWidgetItem, if the name is legal, create the new QTreeWidgetItem, if not, remove it.
    so that's why i tried sth like finishEdit() as said above...

  • i create a delegate and set it to QTreeWidget, but how to edit the item?
    i tried paint(), it go into it,
    but for createEditor and setModelData...it does not go into them...
    i tried setEditTriggers()....and setFlags(Qt::ItemIsEditable)....both not work...

    but openPersistentEditor does work! but when should i call closePersistentEditor?

    which one should i use?

  • is this a good method?

        QWidget *SpinBoxDelegate::createEditor(QWidget *parent,  
            const QStyleOptionViewItem &/* option */,  
            const QModelIndex &/* index */) const  
            QSpinBox *editor = new QSpinBox(parent);  
            return editor;  
        void SpinBoxDelegate::setEditorData(QWidget *editor,  
                                            const QModelIndex &index) const  
            int value = index.model()->data(index, Qt::EditRole).toInt();  
            QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
        void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,  
                                           const QModelIndex &index) const  
            QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
            int value = spinBox->value();  
            model->setData(index, value, Qt::EditRole);  
        void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,  
            const QStyleOptionViewItem &option, const QModelIndex &/* index */) const  
        QObject::connect(treeWidget,SIGNAL(itemDoubleClicked ( QTreeWidgetItem*, int)),  
        QObject::connect(treeWidget,SIGNAL(itemSelectionChanged ()),  
        void CMainWindow::openPersistentEditor(QTreeWidgetItem* item,int column)  
            if( 1 == column  )  
                lastOpen = item;  
        void CMainWindow::slotSelectionChanged()  
            if( NULL!= lastOpen )  
                lastOpen = NULL;  

  • another wired problem....

    connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
    void Class::commitAndCloseEditor()
    QLineEdit* p = qobject_cast<QLineEdit*>(sender());
    //emit commitData(p);  //is this duplicated? i found setModelData is called without this emit
    emit closeEditor(p);  //not work......
    (CMainWindow*)parent()->slotSelectionChanged();  //succeed to close the editor...

  • @mcosta
    i use this code....but remove failed...and it reture false.....

  • and how can i delete a QTreeWidgetItem ? just call delete pQTreeWidgetItem ?

  • @mcosta
    hi do u know how to keep the focus and editing state on the editor(that is QLineEidt here), until the text user input is allowed?
    i want to pop out meesagebox, also when user click the other item...after that i want the focus and editing state is still on the QLineEdit...

    i tried to set the QLineEdit::setFocus....but after i do this, it always call setModelData again............

  • Lifetime Qt Champion


    You would probably need to have a look at the focusOut event for that matter

  • now i use a bool to avoid setModelData implemeted multi-times.
    and is there any example that show what i should do in this situation?

  • Lifetime Qt Champion

    There's no example because when a user input is mandatory, you generally use something like a QDialog than lock the rest of the application

Log in to reply