Details
-
Bug
-
Resolution: Done
-
P3: Somewhat important
-
4.4.0
-
None
-
0866e48ae25f53eb9b27b0b86d333700f912459f
Description
When using a QValidator on an itemview, same data can be set as Invalid after a Return press and Intermediate after clicking on other cell of the table or by pressing Tab or an arrow key. The difference happens in Qt 4.4.0 only, although the behavior after Enter/Return is the correct one. Also, in 4.3.x the result is not correct.
In the 4.4.0 source code "src/gui/itemviews/qitemdelegate.cpp", in QItemDelegate::eventFilter() the correct behavior is executed only for Enter and Return:
case Qt::Key_Enter:
case Qt::Key_Return:
....
if (QLineEdit e = qobject_cast<QLineEdit>(editor))
if (!e->hasAcceptableInput())
return false;
Code to reproduce (string without a comma followed by another character should be invalid, so "Hello" should be invalid):
#include <QtGui> class MyDelegate : public QItemDelegate { Q_OBJECT public: MyDelegate(QObject *parent = 0); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; }; MyDelegate::MyDelegate(QObject *parent) : QItemDelegate(parent) { } QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const { QLineEdit *editor = new QLineEdit(parent); QValidator *validator = new QRegExpValidator( QRegExp("^[a-z0-9]+,[a-z0-9]+$"), parent); editor->setValidator(validator); return editor; } void MyDelegate::setEditorData(QWidget *weditor, const QModelIndex &index) const { QLineEdit *editor = static_cast<QLineEdit*>(weditor); QString value = index.model()->data(index, Qt::EditRole).toString(); editor->setText(value); } void MyDelegate::setModelData(QWidget *weditor, QAbstractItemModel *model, const QModelIndex &index) const { QLineEdit *editor = static_cast<QLineEdit*>(weditor); int pos = 0; const QValidator * validator = editor->validator(); QString text = editor->text(); QValidator::State state = validator->validate(text, pos); if (state == QValidator::Invalid) { QMessageBox::warning(0,"","Validator has invalid state"); } if (state == QValidator::Intermediate) { QMessageBox::warning(0,"","Validator has intermediate state"); } model->setData(index, editor->text(), Qt::EditRole); } void MyDelegate::updateEditorGeometry(QWidget *weditor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const { weditor->setGeometry(option.rect); } int main(int argc, char *argv[]) { QApplication app(argc, argv); QStandardItemModel model(4, 2); QTableView tableView; tableView.setModel(&model); MyDelegate delegate; tableView.setItemDelegate(&delegate); for (int row = 0; row < 4; ++row) { for (int column = 0; column < 2; ++column) { QModelIndex index = model.index(row, column, QModelIndex()); model.setData(index, QVariant((row+1) * (column+1))); } } tableView.show(); return app.exec(); } #include "main.moc"