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); 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); }
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.
-
QTreeWidgetItem *curItem=m_tree->currentItem();
QLineEdit edit=qobject_cast<QLineEdit>(m_tree->itemWidget(curItem,0));
if(!edit)
return;
You are trying to cast pointer to class object. -
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-implementcreateEditor()
to return a QLineEdit andsetModelData()
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 textvoid setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const { QLineEdit *le = qobject_cast<QLineEdit*>(editor); if (le && le->text().isEmpty()) { model->removeRow(index.row()); return; } 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
itemChanged?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); editor->setMinimum(0); editor->setMaximum(100); 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); spinBox->setValue(value); } void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QSpinBox *spinBox = static_cast<QSpinBox*>(editor); spinBox->interpretText(); int value = spinBox->value(); model->setData(index, value, Qt::EditRole); } void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const { editor->setGeometry(option.rect); }
QObject::connect(treeWidget,SIGNAL(itemDoubleClicked ( QTreeWidgetItem*, int)), this,SLOT(openPersistentEditor(QTreeWidgetItem*,int))); QObject::connect(treeWidget,SIGNAL(itemSelectionChanged ()), this,SLOT(slotSelectionChanged())); void CMainWindow::openPersistentEditor(QTreeWidgetItem* item,int column) { if( 1 == column ) { treeWidget->openPersistentEditor(item,column); lastOpen = item; } } void CMainWindow::slotSelectionChanged() { if( NULL!= lastOpen ) { treeWidget->closePersistentEditor(lastOpen,1); 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
model->removeRow(index.row());
i use this code....but remove failed...and it reture false.....
why? -
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............
-
Hi,
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? -
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