PDA

View Full Version : tableview with doubles



Arend
25th November 2012, 22:56
Hello,

I want a tableView with doubles, for instance:

QVector<double> data(10);
for (int i=0;i<10;++i)
data[i]=1+i/10;
QStandardItemModel *model = new QStandardItemModel(10,2,this);
model->setHorizontalHeaderItem(0, new QStandardItem(QString("Column 1")));
...
for(int row=0;row<10;++row)
for(int col=0;col<2;++col)
{
QModelIndex index = model -> index(row,col,QModelIndex());
model->setData(index,data[row]);
}
But I get integers in the table, how can I get doubles: 1.1

Regards,
Arend

Added after 12 minutes:

I have solved this one
model->setData(index(row,column),QString::number(yourNumb er, 'f', 3));

ChrisW67
26th November 2012, 05:12
I have solved this one
model->setData(index(row,column),QString::number(yourNumb er, '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;

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

wysota
26th November 2012, 07:43
QVector<double> data(10);
for (int i=0;i<10;++i)
data[i]=1+i/10;
Hmm... let me see.... data[0] = 1, data[1] = 1, data[2] = 1, data[3] = 1, data[4] = 1 ... data[9] = 1

They are all integers.