PDA

View Full Version : display of doubles in QTableView to match the DoubleSpinBox delegate



user
26th May 2008, 04:51
I have a QtableView that displays a row of comboBoxes as the first row and additional rows that have a spinBoxDelegate (the spinBoxDelegate is actually a doubleSpinBox).
When I click on any cell (that is not in the first row) I get the delegate editor which I setup to show with 2 digits after the decimal point.
My row of comboBoxes allows switching between units of display (different units for each column depending on the data), so if I entered 2345 for the distance column when the appropriate comboBox was set to "m", the value will change to 2.345 when I change the comBox to show "km".
It all works fine and I'm able to switch between units and converting the values for the column that was changed but I still have one problem:
When I convert the values between the units, I set the new value in the cell through the model

for (int row=0; row<myModel->rowCount(); row++)
{
double value = myModel->data(myModel->index(row, col1)).toDouble();
convertedValue = convertValue( value );
myModel->setData(myModel->index(row, col1), QVariant(convertedValue));
}

The new value that is displayed in the cell is not limited to 2 digits after the decimal point and when I click to edit it the additional digits disappear and I'm left with 2 digits after the decimal point.
How can I set the converted data to show properly in the first place (with 2 digits after the decimal point)?

jpn
30th May 2008, 15:33
Just make sure that your custom delegate's createEditor() or setEditorData() sets the appropriate amount of decimals with QDoubleSpinBox::setDecimals()?

user
2nd June 2008, 00:51
I already do setDecimals(2) in the delegate. The problem is that when I change the values through the code the editor (delegate) is not called and the value is not shown in the right format.
Is there a different way than what I've done, to set the table values in the source code? Maybe a way that will end up calling setEditorData()?
I've used QStandardItemModel::setData() but it doesn't seem to call SpinBoxDelegate::setEditorData().

jpn
2nd June 2008, 07:49
Ahh, sorry for overlooking the problem. In that case I think it would be sensible to simply close the editor...

user
3rd June 2008, 00:37
The SpinBoxDelegate for the table works perfectly. If the user double clicks on a table cell the editor comes up and the values are entered in the correct format. Then the user can press enter or shift focus to something else - the editor will close and the values are still displayed in the correct format. All of this functionality works well.

What I would like to do is: in my code to read the value the user entered in a specific cell, change it and display it back in the same cell.
e.g. the value from the user = 25.5, my calculated value 25.5/100=0.255. I want the value to show as 0.25 (2 places after the decimal point).

When I use QStandardItemModel functions to do that, I can't see a way to set the precision of the value I want to display? Will I have to calculate it manualy?

mstegehu
25th May 2011, 11:10
Is this already figured out? I have the same problem.

How can is show a double in a table view with the precision set?