summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Kelly <stephen.kelly@kdab.com>2012-10-02 13:01:30 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-10-19 00:44:54 +0200
commitd84f449bcde56fd2df12f82dd2ba2e1f0a4ae7c7 (patch)
tree0d982beb7be025b695e57b707b51eec1e048414c
parent8bed283f137c14a7be1fb9027eae5eae3ce796be (diff)
Make sure uints remain uints when editing in itemviews.
Task-number: QTBUG-22974 Change-Id: I07428862c4dffc629f868f3010f663eb655922d0 Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
-rw-r--r--src/widgets/itemviews/qitemeditorfactory.cpp33
-rw-r--r--tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp61
-rw-r--r--tests/auto/widgets/itemviews/qitemeditorfactory/tst_qitemeditorfactory.cpp19
3 files changed, 112 insertions, 1 deletions
diff --git a/src/widgets/itemviews/qitemeditorfactory.cpp b/src/widgets/itemviews/qitemeditorfactory.cpp
index 5162a5534c..5f24c3ea1b 100644
--- a/src/widgets/itemviews/qitemeditorfactory.cpp
+++ b/src/widgets/itemviews/qitemeditorfactory.cpp
@@ -73,6 +73,36 @@ public:
#endif // QT_NO_COMBOBOX
+
+#ifndef QT_NO_SPINBOX
+
+class QUIntSpinBox : public QSpinBox
+{
+ Q_OBJECT
+ Q_PROPERTY(uint value READ uintValue WRITE setUIntValue NOTIFY uintValueChanged USER true)
+public:
+ explicit QUIntSpinBox(QWidget *parent = 0)
+ : QSpinBox(parent)
+ {
+ connect(this, SIGNAL(valueChanged(int)), SIGNAL(uintValueChanged()));
+ }
+
+ uint uintValue()
+ {
+ return value();
+ }
+
+ void setUIntValue(uint value_)
+ {
+ return setValue(value_);
+ }
+
+Q_SIGNALS:
+ void uintValueChanged();
+};
+
+#endif // QT_NO_SPINBOX
+
/*!
\class QItemEditorFactory
\brief The QItemEditorFactory class provides widgets for editing item data
@@ -206,8 +236,9 @@ QWidget *QDefaultItemEditorFactory::createEditor(int userType, QWidget *parent)
#endif
#ifndef QT_NO_SPINBOX
case QVariant::UInt: {
- QSpinBox *sb = new QSpinBox(parent);
+ QSpinBox *sb = new QUIntSpinBox(parent);
sb->setFrame(false);
+ sb->setMinimum(0);
sb->setMaximum(INT_MAX);
return sb; }
case QVariant::Int: {
diff --git a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp
index bed1d52f94..06964bac0d 100644
--- a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp
+++ b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp
@@ -224,6 +224,7 @@ private slots:
void dateTimeEditor_data();
void dateTimeEditor();
void dateAndTimeEditorTest2();
+ void uintEdit();
void decoration_data();
void decoration();
void editorEvent_data();
@@ -939,6 +940,66 @@ void tst_QItemDelegate::dateAndTimeEditorTest2()
w.doCloseEditor(dateEdit);
}
+void tst_QItemDelegate::uintEdit()
+{
+ QListView view;
+ QStandardItemModel model;
+
+ {
+ QStandardItem *data=new QStandardItem;
+ data->setEditable(true);
+ data->setData(QVariant((uint)1), Qt::DisplayRole);
+ model.setItem(0, 0, data);
+ }
+ {
+ QStandardItem *data=new QStandardItem;
+ data->setEditable(true);
+ data->setData(QVariant((uint)1), Qt::DisplayRole);
+ model.setItem(1, 0, data);
+ }
+
+ view.setModel(&model);
+ view.setEditTriggers(QAbstractItemView::AllEditTriggers);
+
+ const QModelIndex firstCell = model.index(0, 0);
+
+ QCOMPARE(firstCell.data(Qt::DisplayRole).userType(), static_cast<int>(QMetaType::UInt));
+
+ view.selectionModel()->setCurrentIndex(model.index(0, 0), QItemSelectionModel::Select);
+ view.edit(firstCell);
+
+ QSpinBox *sb = view.findChild<QSpinBox*>();
+ QVERIFY(sb);
+
+ sb->stepUp();
+
+ // Select another index to trigger the end of editing.
+ const QModelIndex secondCell = model.index(1, 0);
+ view.selectionModel()->setCurrentIndex(secondCell, QItemSelectionModel::Select);
+
+ QCOMPARE(firstCell.data(Qt::DisplayRole).userType(), static_cast<int>(QMetaType::UInt));
+ QCOMPARE(firstCell.data(Qt::DisplayRole).toUInt(), static_cast<uint>(2));
+
+
+ view.edit(secondCell);
+
+ // The first spinbox is deleted with deleteLater, so it is still there.
+ QList<QSpinBox*> sbList = view.findChildren<QSpinBox*>();
+ QCOMPARE(sbList.size(), 2);
+
+ sb = sbList.at(1);
+
+ sb->stepDown(); // 1 -> 0
+ sb->stepDown(); // 0 (no effect)
+ sb->stepDown(); // 0 (no effect)
+
+ // Select another index to trigger the end of editing.
+ view.selectionModel()->setCurrentIndex(firstCell, QItemSelectionModel::Select);
+
+ QCOMPARE(secondCell.data(Qt::DisplayRole).userType(), static_cast<int>(QMetaType::UInt));
+ QCOMPARE(secondCell.data(Qt::DisplayRole).toUInt(), static_cast<uint>(0));
+}
+
void tst_QItemDelegate::decoration_data()
{
QTest::addColumn<int>("type");
diff --git a/tests/auto/widgets/itemviews/qitemeditorfactory/tst_qitemeditorfactory.cpp b/tests/auto/widgets/itemviews/qitemeditorfactory/tst_qitemeditorfactory.cpp
index e0a9db3d7d..44b1cebcfb 100644
--- a/tests/auto/widgets/itemviews/qitemeditorfactory/tst_qitemeditorfactory.cpp
+++ b/tests/auto/widgets/itemviews/qitemeditorfactory/tst_qitemeditorfactory.cpp
@@ -48,6 +48,7 @@ class tst_QItemEditorFactory: public QObject
private slots:
void createEditor();
void createCustomEditor();
+ void uintValues();
};
void tst_QItemEditorFactory::createEditor()
@@ -100,6 +101,24 @@ void tst_QItemEditorFactory::createCustomEditor()
delete creator;
}
+void tst_QItemEditorFactory::uintValues()
+{
+ QItemEditorFactory editorFactory;
+
+ QWidget parent;
+
+ {
+ QWidget *editor = editorFactory.createEditor(QMetaType::UInt, &parent);
+ QCOMPARE(editor->metaObject()->className(), "QUIntSpinBox");
+ QCOMPARE(editor->metaObject()->userProperty().type(), QVariant::UInt);
+ }
+ {
+ QWidget *editor = editorFactory.createEditor(QMetaType::Int, &parent);
+ QCOMPARE(editor->metaObject()->className(), "QSpinBox");
+ QCOMPARE(editor->metaObject()->userProperty().type(), QVariant::Int);
+ }
+}
+
QTEST_MAIN(tst_QItemEditorFactory)
#include "tst_qitemeditorfactory.moc"