QDataWidgetMapper and numbers issue
-
Hello all,
I have QSqlTableModel, connected QTableView and form managed by QDataWidgetMapper.
SQL table has column with type INTEGER. Some table records have value in this column and some not.
I have mapped form fields (QLineEdits) for table columns. When I change selected record, all fields are populated well except the field for column with INTEGER type.
When there is selected record where this column contains data, the field correctly shows the value. But when I change the selection to the record where this column doesn't have data (NULL), the field keeps the value from previously selected record.I did some tests and this behavior is only for table column with INTEGER type. When I change the type to CHAR(n), it works well.
Any idea for this wrong behavior?
Thanks in advance,
Jaroslav -
It could be a bug in Qt -- can you check the data() returned for those positions where the value is missing (i.e. is NULL), namely what the returned variant contains and which type is it? If it's correct, then it could be a QDWM problem, and/or a QItemDelegate one.
A possible workaround could be installing a custom QItemDelegate subclass on the QDWM and forcibly clean the lineedit in case of a null value, but I suspect that the QDWM is never calling the QID setEditorData...
-
Thanks for quick response Peppe,
For empty cells, the model returns QVariant with type int, value 0, but isNull() returns true.
So QDataWidgetMapper should handle it as "empty value". I solved it in delegate setEditorData(). When variant is null I set empty string into edit line.
But it seems to me that is it a bug in default setEditorData function.
-
bq. but I suspect that the QDWM is never calling the QID setEditorData…
It calls it if the mapping does not have a custom property.
I tried to investigate, but I didn't find from where this bug could come from, without spending hours trying to understand the metaobject internal mechanism :D
However, the default delegate just set the corresponding widget's user property and QLineEdit has one.
This maybe comes from QMetaProperty::write ?
-
That's not really a bug:
- QVariant can't convert a null QVariant::Int to a string (checked in QMetaPropery::write through QVariant::convert),
- QObject::setProperty, called directly in QDWM or in setEditorData, won't assign a value from QVariant if it can't convert it from the original type to the real property type.
- QDWM can't possibly take care automatically of cases where conversions doesn't work.
So, if the "empty value" is a possibility, you should handle it yourself with a delegate as you did.
-
[quote author="alexisdm" date="1315313698"]That's not really a bug:
- QVariant can't convert a null QVariant::Int to a string (checked in QMetaPropery::write through QVariant::convert),
- QObject::setProperty, called directly in QDWM or in setEditorData, won't assign a value from QVariant if it can't convert it from the original type to the real property type.
- QDWM can't possibly take care automatically of cases where conversions doesn't work.
So, if the "empty value" is a possibility, you should handle it yourself with a delegate as you did.[/quote]
Makes perfectly sense, but why if a non-null QVariant::Int converts successfully into a QString, why doesn't a null QVariant::Int convert into a null QString? (Note: I don't know QVariant internals).
-
[quote author="peppe" date="1315319386"]but why if a non-null QVariant::Int converts successfully into a QString, why doesn't a null QVariant::Int convert into a null QString?[/quote]It is kind of converted: any null QVariant can be converted to a null QString, or to an invalid/null value of any type it could be converted to if it wasn't null, but QVariant::convert will return false, indicating an error.
QObject::setProperty just doesn't ignore that error.