How to prevent changing selection in QtableView, if the data is invalid?
-
Hi,
When adding new data to QtableView/QRelationalTableModel, I need to verify the data entered by the user. The verification is done by using QValidator, but how can I prevent changing from that cell if the data is invalid, and to instruct user to correct the data?
Thanks in advance,
Ras -
Reimplement the focusOutEvent for the input widget and setFocus() if there is no acceptable input. You can then use a custom delegate to show your input widget.
@
class LineEdit : public QLineEdit
{
Q_OBJECTpublic:
LineEdit(QWidget *parent = 0) : QLineEdit(parent) {}protected:
void focusOutEvent(QFocusEvent *event)
{
if (((currentFocus_ == this) || (currentFocus_ == 0)) &&
(hasAcceptableInput() == false))
{
currentFocus_ = this;
setFocus();
}
else
{
currentFocus_ = 0;
QLineEdit::focusOutEvent(event);
}
}private:
static LineEdit *currentFocus_;
};LineEdit *LineEdit::currentFocus_ = 0;
@
Brain to terminal. Not tested.