I have solved this one
model->setData(index(row,column),QString::number(yourNum ber, 'f', 3));
No, actually that puts strings (wrapped in QVariant) into the model. For a lot of purposes this may be adequate. If you want an actual QVariant<double> to preserve precision then you need to use the setData() function with a double. You can use a delegate to drive the display form of the double. Compare these:
double yourNumber = 10.7002;
item.
setData(QString::number(yourNumber,
'f',
3), Qt
::DisplayRole);
qDebug() << item.data(Qt::DisplayRole) << item.data(Qt::DisplayRole).toDouble();
qDebug() << item.data(Qt::DisplayRole);
// QVariant(QString, "10.700") 10.7
item.setData(yourNumber, Qt::DisplayRole);
qDebug() << item.data(Qt::DisplayRole) << item.data(Qt::DisplayRole).toDouble();
// QVariant(double, 10.7002) 10.7002
double yourNumber = 10.7002;
QStandardItem item;
item.setData(QString::number(yourNumber, 'f', 3), Qt::DisplayRole);
qDebug() << item.data(Qt::DisplayRole) << item.data(Qt::DisplayRole).toDouble();
qDebug() << item.data(Qt::DisplayRole);
// QVariant(QString, "10.700") 10.7
item.setData(yourNumber, Qt::DisplayRole);
qDebug() << item.data(Qt::DisplayRole) << item.data(Qt::DisplayRole).toDouble();
// QVariant(double, 10.7002) 10.7002
To copy to clipboard, switch view to plain text mode
Bookmarks