Thanks I got the basic key filtering working with the following code.
{
if(event
->type
() == QEvent::KeyPress) {
QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
return (keyEvent->key() < '0' || keyEvent->key() > '9');
}
// standard event processing
return QObject::eventFilter(obj, event
);
}
bool StringDelegate::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
return (keyEvent->key() < '0' || keyEvent->key() > '9');
}
// standard event processing
return QObject::eventFilter(obj, event);
}
To copy to clipboard, switch view to plain text mode
Now I am trying to enforce the range when the user exits editing: I tried to add the following to the eventFilter, it compiles but doesn't work.
} else if(event
->type
() == QEvent::Leave) {
QLineEdit *lineEdit
= static_cast<QLineEdit
*>
(obj
);
if(text.toInt() > 255)
text = "255";
else if(text.toInt() < 1)
text = "0";
lineEdit->setText(text);
}
} else if(event->type() == QEvent::Leave) {
QLineEdit *lineEdit = static_cast<QLineEdit*>(obj);
QString text = lineEdit->text();
if(text.toInt() > 255)
text = "255";
else if(text.toInt() < 1)
text = "0";
lineEdit->setText(text);
}
To copy to clipboard, switch view to plain text mode
The following code checks range when editing starts, but i'm just interested in when editing ends:
{
QString value
= index.
model()->data
(index, Qt
::EditRole).
toString();
QLineEdit *lineEdit
= static_cast<QLineEdit
*>
(editor
);
if(value.toInt() > 255)
value = "255";
else if(value.toInt() < 1)
value = "0";
lineEdit->setText(value);
}
void StringDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
if(value.toInt() > 255)
value = "255";
else if(value.toInt() < 1)
value = "0";
lineEdit->setText(value);
}
To copy to clipboard, switch view to plain text mode
Any ideas? Thanks in advance.
Bookmarks