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
Qt Code:
  1. void DoubleEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  2. {
  3. QString data = static_cast<QLineEdit*>(editor)->text();
  4. int pos;
  5. QValidator::State state = validator_->validate(data, pos);
  6. if (validator_->validate(data, pos) == QValidator::Acceptable)
  7. {
  8. model->setData(index, data);
  9. emit const_cast<DoubleEditDelegate*>(this)->clearMessage();
  10. }
  11. else
  12. {
  13. emit const_cast<DoubleEditDelegate*>(this)->errorMessage("Double required at line " + QString::number(index.row()+1));
  14. emit const_cast<DoubleEditDelegate*>(this)->closeEditor(editor, QAbstractItemDelegate::EditPreviousItem);
  15. }
  16. }
To copy to clipboard, switch view to plain text mode 

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.