Results 1 to 6 of 6

Thread: display of doubles in QTableView to match the DoubleSpinBox delegate

  1. #1
    Join Date
    May 2007
    Location
    Australia
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default display of doubles in QTableView to match the DoubleSpinBox delegate

    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
    Qt Code:
    1. for (int row=0; row<myModel->rowCount(); row++)
    2. {
    3. double value = myModel->data(myModel->index(row, col1)).toDouble();
    4. convertedValue = convertValue( value );
    5. myModel->setData(myModel->index(row, col1), QVariant(convertedValue));
    6. }
    To copy to clipboard, switch view to plain text mode 

    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)?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: display of doubles in QTableView to match the DoubleSpinBox delegate

    Just make sure that your custom delegate's createEditor() or setEditorData() sets the appropriate amount of decimals with QDoubleSpinBox::setDecimals()?
    J-P Nurmi

  3. #3
    Join Date
    May 2007
    Location
    Australia
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: display of doubles in QTableView to match the DoubleSpinBox delegate

    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().

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: display of doubles in QTableView to match the DoubleSpinBox delegate

    Ahh, sorry for overlooking the problem. In that case I think it would be sensible to simply close the editor...
    J-P Nurmi

  5. #5
    Join Date
    May 2007
    Location
    Australia
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: display of doubles in QTableView to match the DoubleSpinBox delegate

    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?

  6. #6
    Join Date
    Sep 2008
    Posts
    54
    Thanks
    3
    Thanked 10 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    2

    Default Re: display of doubles in QTableView to match the DoubleSpinBox delegate

    Is this already figured out? I have the same problem.

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.