QAbstractItemDelegate: Update spinbox editor value before committing
QAbstractItemView installs the delegate as an event filter on the editor, so the delegate will get the focusOut event (and other events) before the editor does. QAbstractItemDelegate will then emit commitData, signaling that the "updated" data should be written back to the model. In the case where the editor of a delegate (QAbstractItemDelegate) is a QSpinBox with keyboardTracking set to false, the value of the spinbox won't be updated while typing, but only when the spinbox's text edit focus is lost. In this case, the delegate's commitData will be emitted before the spinbox has had a chance to update the value in its handling of the focusOut event. To fix, make sure to update the value before the data is committed to the model in the delegate's tryFixup method. Fixes: QTBUG-116926 Pick-to: 6.5 6.6 Change-Id: I68540964342407d23387e4404a0fe3f00d80eb5f Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
parent
66be69139c
commit
faae3dc6b1
@ -514,6 +514,14 @@ bool QAbstractItemDelegatePrivate::tryFixup(QWidget *editor)
|
|||||||
return e->hasAcceptableInput();
|
return e->hasAcceptableInput();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
#if QT_CONFIG(spinbox)
|
||||||
|
// Give a chance to the spinbox to interpret the text and emit
|
||||||
|
// the appropriate signals before committing data.
|
||||||
|
if (QAbstractSpinBox *sb = qobject_cast<QAbstractSpinBox *>(editor)) {
|
||||||
|
if (!sb->keyboardTracking())
|
||||||
|
sb->interpretText();
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
Q_UNUSED(editor);
|
Q_UNUSED(editor);
|
||||||
#endif // QT_CONFIG(lineedit)
|
#endif // QT_CONFIG(lineedit)
|
||||||
|
@ -147,6 +147,8 @@ private slots:
|
|||||||
void inputMethodOpensEditor();
|
void inputMethodOpensEditor();
|
||||||
void selectionAutoScrolling_data();
|
void selectionAutoScrolling_data();
|
||||||
void selectionAutoScrolling();
|
void selectionAutoScrolling();
|
||||||
|
void testSpinBoxAsEditor_data();
|
||||||
|
void testSpinBoxAsEditor();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static QAbstractItemView *viewFromString(const QByteArray &viewType, QWidget *parent = nullptr)
|
static QAbstractItemView *viewFromString(const QByteArray &viewType, QWidget *parent = nullptr)
|
||||||
@ -3403,6 +3405,62 @@ void tst_QAbstractItemView::selectionAutoScrolling()
|
|||||||
|
|
||||||
QTest::mouseRelease(listview.viewport(), Qt::LeftButton, Qt::NoModifier, dragPoint);
|
QTest::mouseRelease(listview.viewport(), Qt::LeftButton, Qt::NoModifier, dragPoint);
|
||||||
}
|
}
|
||||||
|
class SpinBoxDelegate : public QStyledItemDelegate
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using QStyledItemDelegate::QStyledItemDelegate;
|
||||||
|
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const override
|
||||||
|
{
|
||||||
|
QSpinBox *spinboxEditor = new QSpinBox(parent);
|
||||||
|
return spinboxEditor;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
|
||||||
|
{
|
||||||
|
if (QSpinBox *spin = qobject_cast<QSpinBox *>(editor)) {
|
||||||
|
model->setData(index, spin->value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void tst_QAbstractItemView::testSpinBoxAsEditor_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<bool>("keyboardTracking");
|
||||||
|
QTest::newRow("true") << true;
|
||||||
|
QTest::newRow("false")<< false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_QAbstractItemView::testSpinBoxAsEditor()
|
||||||
|
{
|
||||||
|
QFETCH(bool, keyboardTracking);
|
||||||
|
|
||||||
|
QStandardItemModel model(2, 2);
|
||||||
|
SpinBoxDelegate delegate;
|
||||||
|
|
||||||
|
QTableView view;
|
||||||
|
view.setModel(&model);
|
||||||
|
view.setItemDelegate(&delegate);
|
||||||
|
|
||||||
|
view.setCurrentIndex(model.index(0, 1));
|
||||||
|
view.openPersistentEditor(model.index(0, 1));
|
||||||
|
const QList<QSpinBox *> list = view.viewport()->findChildren<QSpinBox *>();
|
||||||
|
QCOMPARE(list.size(), 1);
|
||||||
|
QSpinBox *sb = list.first();
|
||||||
|
QVERIFY(sb);
|
||||||
|
|
||||||
|
sb->setKeyboardTracking(keyboardTracking);
|
||||||
|
|
||||||
|
centerOnScreen(&view);
|
||||||
|
view.show();
|
||||||
|
QVERIFY(QTest::qWaitForWindowExposed(&view));
|
||||||
|
QTRY_COMPARE(QApplication::focusWidget(), sb);
|
||||||
|
|
||||||
|
QTest::keyClick(sb, Qt::Key_1, Qt::NoModifier);
|
||||||
|
QPoint clickpos = view.visualRect(model.index(0, 0)).center();
|
||||||
|
QTest::mouseDClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, clickpos);
|
||||||
|
|
||||||
|
QCOMPARE(model.data(model.index(0, 1)).toInt(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
QTEST_MAIN(tst_QAbstractItemView)
|
QTEST_MAIN(tst_QAbstractItemView)
|
||||||
#include "tst_qabstractitemview.moc"
|
#include "tst_qabstractitemview.moc"
|
||||||
|
Loading…
Reference in New Issue
Block a user