QItemViews: Add ability to show QJsonValue::Bool/Double

Q(Tree|Table|List)View was able to display a simple
QJsonValue::String, but not QJsonValue::Bool/Double. This
is an inconsistent behavior which is fixed with this patch.

Task-number: QTBUG-65082
Change-Id: I22c2fe2890f11e283cae4f7ea947aa67ff36f367
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
bb10
Christian Ehrlicher 2017-12-14 21:15:25 +01:00
parent 41b4e154d6
commit fb58845d8f
1 changed files with 14 additions and 2 deletions

View File

@ -58,6 +58,7 @@
#endif
#include <qapplication.h>
#include <qvalidator.h>
#include <qjsonvalue.h>
#include <private/qtextengine_p.h>
#include <private/qabstractitemdelegate_p.h>
@ -588,12 +589,23 @@ QString QAbstractItemDelegatePrivate::textForRole(Qt::ItemDataRole role, const Q
case QVariant::DateTime:
text = locale.toString(value.toDateTime(), formatType);
break;
default:
text = value.toString();
default: {
if (value.canConvert<QJsonValue>()) {
const QJsonValue val = value.toJsonValue();
if (val.isBool())
text = QVariant(val.toBool()).toString();
else if (val.isDouble())
text = locale.toString(val.toDouble(), 'g', precision);
else if (val.isString())
text = val.toString();
} else {
text = value.toString();
}
if (role == Qt::DisplayRole)
text.replace(QLatin1Char('\n'), QChar::LineSeparator);
break;
}
}
return text;
}