Make sure uints remain uints when editing in itemviews.

Task-number: QTBUG-22974
Change-Id: I07428862c4dffc629f868f3010f663eb655922d0
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Stephen Kelly 2012-10-02 13:01:30 +02:00 committed by The Qt Project
parent 8bed283f13
commit d84f449bcd
3 changed files with 112 additions and 1 deletions

View File

@ -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: {

View File

@ -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");

View File

@ -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"