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
{
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);
}
}
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);
}
}
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.
Bookmarks