Use of QAbstractItemDelegate::closeEditor
I'm trying to user input perform validation in a QTableView/QAbstractTableModelstructure. I want to keep the cursor in a table cell if the entry is invalid. The only thing I've found that seems to work is
Code:
{
QString data
= static_cast<QLineEdit
*>
(editor
)->text
();
int pos;
QValidator::State state
= validator_
->validate
(data, pos
);
if (validator_
->validate
(data, pos
) == QValidator::Acceptable) {
model->setData(index, data);
emit const_cast<DoubleEditDelegate*>(this)->clearMessage();
}
else
{
emit const_cast<DoubleEditDelegate
*>
(this)->errorMessage
("Double required at line " + QString::number(index.
row()+1));
emit const_cast<DoubleEditDelegate
*>
(this)->closeEditor
(editor,
QAbstractItemDelegate::EditPreviousItem);
}
}
This doesn't seem right - having to const_cast the "this" pointer. Is this the correct way to use the closeEditor signal? If not, how do I do this?
Thanks for any help.
Re: Use of QAbstractItemDelegate::closeEditor
Maybe you should reimplement QItemDelegate::createEditor() instead and assign the validator to the editor there?
Re: Use of QAbstractItemDelegate::closeEditor
Quote:
Originally Posted by
jacek
The problem with that is that it still allows you to TAB to the next cell. It just doesn't store the data in the model. It doesn't provide any cursor control.
Re: Use of QAbstractItemDelegate::closeEditor
Quote:
Originally Posted by
jml
The problem with that is that it still allows you to TAB to the next cell. It just doesn't store the data in the model. It doesn't provide any cursor control.
I finally solved the problem. QItemDelegate re-implements the virtual function eventFilter. So I just created a new class derived from QItemDelegate.
In eventFilter, if it's not a TAB key:
If it is a TAB and validates OK:
Code:
emit commitData(lineEdit);
return true;
If not: