Originally Posted by
Billy Lee Black
What I need to do, is check every time the user edits the text of an item if it isn´t equal to the text of the other items. In order to do that, I have to catch when the editor gets closed (know when the user has finished editing the item).
QLineEdit is used as editor provided that it's textual data. Perhaps you could make use of QLineEdit::editingFinished()?
{
Q_OBJECT
public:
QWidget* createEditor
(QWidget* parent,
const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const {
// following cast & check aren't actually needed for the connect
// you'll get a warning if it's not a QLineEdit (in case of other than textual data)
QLineEdit* lineEdit
= dynamic_cast<QLineEdit
*>
(editor
);
if (lineEdit)
connect(lineEdit, SIGNAL(editingFinished()), this, SIGNAL(editingFinished()));
return editor;
}
signals:
void editingFinished();
};
// usage:
treeWidget->setItemDelegate(new ItemDelegate(treeWidget));
connect(treeWidget->itemDelegate(), SIGNAL(editingFinished()), ...);
class ItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
ItemDelegate(QObject* parent = 0)
: QItemDelegate(parent) { }
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QWidget* editor = QItemDelegate::createEditor(parent, option, index);
// following cast & check aren't actually needed for the connect
// you'll get a warning if it's not a QLineEdit (in case of other than textual data)
QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(editor);
if (lineEdit)
connect(lineEdit, SIGNAL(editingFinished()), this, SIGNAL(editingFinished()));
return editor;
}
signals:
void editingFinished();
};
// usage:
treeWidget->setItemDelegate(new ItemDelegate(treeWidget));
connect(treeWidget->itemDelegate(), SIGNAL(editingFinished()), ...);
To copy to clipboard, switch view to plain text mode
You could also use QSignalMapper if you want to pass the editor widget as signal parameter.
So, if there is another item with the same text, than I show a QMessageBox warning the user the item must have a different text, so I open the persistent editor again for the user to change the text.
Idea: make corresponding editor backgrounds red or something instead of popping up annoying message boxes
Bookmarks