summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/itemviews
diff options
context:
space:
mode:
authorDoris Verria <doris.verria@qt.io>2023-09-18 10:41:34 +0200
committerDoris Verria <doris.verria@qt.io>2023-09-25 15:12:05 +0200
commitfaae3dc6b15e3d8754b15a7cfb22b6a797fd2e36 (patch)
tree63d08e06b9e058c9e6b1afb6ff599425cdd652e4 /tests/auto/widgets/itemviews
parent66be69139ce15be5df800e93a4702891fa8d6fb3 (diff)
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>
Diffstat (limited to 'tests/auto/widgets/itemviews')
-rw-r--r--tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
index 9b8948001d..172fe99e2f 100644
--- a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
+++ b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
@@ -147,6 +147,8 @@ private slots:
void inputMethodOpensEditor();
void selectionAutoScrolling_data();
void selectionAutoScrolling();
+ void testSpinBoxAsEditor_data();
+ void testSpinBoxAsEditor();
private:
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);
}
+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)
#include "tst_qabstractitemview.moc"