create a new QTreeWidgetItem in QTreeWidget, edit its name in QTreeWidgetItem, if the name is legal, create the new QTreeWidgetItem, if not, remove it.
-
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