lose precesion when qtreeview show double value
-
my json contains double value like 32.6743408794, but when i load and show it ,it only shows 32.67。When debug in QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* parent) fuction , i found the value in child is correct actually, so i think it's a treeview widget problem not jsonmodel. If someone could give me some advice, i'll appreciate. :)
-
@Christian-Ehrlicher Well yes, but also no :)
data()returns a QVariant, which does not need to be a string. In case of numbers it's often preferable to return the actual type, like double or int and let the view display it appropriately. This allows to have different views display the data from the same model differently e.g. have one view display a rounded number and another (e.g. a "detailed view") to show the full thing. You could do the conversion in the model, but it limits your further options and formatting shouldn't really be the model's job I think. The way I interpretDisplayRoleis "the data to be displayed", not "how to display the data".@Chris-Kawa thank for ur explaination. after custom my delegate , i solved this problem.

and when click double value item, should reimplement setEditorData function to specify the format,otherwise the double value will shrink too. below is my key code,hope it helps someone else.
QWidget* XtrTaskViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { return QStyledItemDelegate::createEditor(parent, option, index); } void XtrTaskViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { if (index.data().type() == QVariant::Double) { QDoubleSpinBox* doubleSpinBox = static_cast<QDoubleSpinBox*>(editor); doubleSpinBox->setDecimals(10); doubleSpinBox->setValue(index.data().toDouble()); } else { QStyledItemDelegate::setEditorData(editor, index); } } -
btw, i use https://github.com/dridk/QJsonModel for my json model ,it's easy to use, but this problem annoy me.
-
btw, i use https://github.com/dridk/QJsonModel for my json model ,it's easy to use, but this problem annoy me.
Fix your QJsonModel::data() function to return a proper string for Qt::DisplayRole.
-
Fix your QJsonModel::data() function to return a proper string for Qt::DisplayRole.
@Christian-Ehrlicher
thank for ur reply.
but i didn't want to change the type of such numberic value, cause i want to edit them through gui, if i return string , then when i save the json , a stringToDouble function will be added. I just wonder wheather there is a simpler way : -
@WuYaZi QTreeView by default uses QStyledItemDelegate to paint its items and that uses the locale object to get the display text from the double value returned from the model data. See source. By default the precision is set to 6 digits, so either your particular locale overrides that or you have a custom item delegate that does the double to string conversion differently.
In any case you can provide a custom delegate if you want to override the way it displays numbers.
-
@WuYaZi QTreeView by default uses QStyledItemDelegate to paint its items and that uses the locale object to get the display text from the double value returned from the model data. See source. By default the precision is set to 6 digits, so either your particular locale overrides that or you have a custom item delegate that does the double to string conversion differently.
In any case you can provide a custom delegate if you want to override the way it displays numbers.
@Chris-Kawa Or simply return the correct formated value in data() for Qt::DisplayRole (this is what this role is for).
-
@Chris-Kawa Or simply return the correct formated value in data() for Qt::DisplayRole (this is what this role is for).
@Christian-Ehrlicher Well yes, but also no :)
data()returns a QVariant, which does not need to be a string. In case of numbers it's often preferable to return the actual type, like double or int and let the view display it appropriately. This allows to have different views display the data from the same model differently e.g. have one view display a rounded number and another (e.g. a "detailed view") to show the full thing. You could do the conversion in the model, but it limits your further options and formatting shouldn't really be the model's job I think. The way I interpretDisplayRoleis "the data to be displayed", not "how to display the data". -
@Christian-Ehrlicher Well yes, but also no :)
data()returns a QVariant, which does not need to be a string. In case of numbers it's often preferable to return the actual type, like double or int and let the view display it appropriately. This allows to have different views display the data from the same model differently e.g. have one view display a rounded number and another (e.g. a "detailed view") to show the full thing. You could do the conversion in the model, but it limits your further options and formatting shouldn't really be the model's job I think. The way I interpretDisplayRoleis "the data to be displayed", not "how to display the data".@Chris-Kawa thank for ur explaination. after custom my delegate , i solved this problem.

and when click double value item, should reimplement setEditorData function to specify the format,otherwise the double value will shrink too. below is my key code,hope it helps someone else.
QWidget* XtrTaskViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { return QStyledItemDelegate::createEditor(parent, option, index); } void XtrTaskViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { if (index.data().type() == QVariant::Double) { QDoubleSpinBox* doubleSpinBox = static_cast<QDoubleSpinBox*>(editor); doubleSpinBox->setDecimals(10); doubleSpinBox->setValue(index.data().toDouble()); } else { QStyledItemDelegate::setEditorData(editor, index); } } -
W WuYaZi has marked this topic as solved on