PDA

View Full Version : Use of QAbstractItemDelegate::closeEditor



jml
30th July 2007, 22:32
I'm trying to user input perform validation in a QTableView/QAbstractTableModel structure. 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


void DoubleEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
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.

jacek
30th July 2007, 23:45
Maybe you should reimplement QItemDelegate::createEditor() instead and assign the validator to the editor there?

jml
1st August 2007, 00:17
Maybe you should reimplement QItemDelegate::createEditor() instead and assign the validator to the editor there?

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.

jml
3rd August 2007, 22:55
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:

return QItemDelegate::eventFilter(editor, event);
If it is a TAB and validates OK:


emit commitData(lineEdit);
emit closeEditor(lineEdit, QAbstractItemDelegate::EditNextItem);
return true;

If not:


emit closeEditor(lineEdit, QAbstractItemDelegate::NoHint);
return true