Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Individual formatting of a QTableWidget's column
Qt 6.11 is out! See what's new in the release blog

Individual formatting of a QTableWidget's column

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • l3u_L Offline
    l3u_L Offline
    l3u_
    wrote on last edited by l3u_
    #1

    Hi all :-)

    I have a QTableWidget I individualized using a QStyledItemDelegate so that the editor for one column becomes a QDoubleSpinBox instead of a QLineEdit (as in this column, a price should be entered and not text).

    The problem is that if one e.g. enters "5,00 €" in the spin box, I still get "5" after pressing enter, or "5,5" for "5,50 €".

    I searched how to change this, but it seems that changing the DisplayRole of some data requires to implement an own QTableView using a custom QAbstractTableModel. Is this the case? I only need to do this one thing, everything else would be fine how the QTableWidget does it ;-)

    Thanks for all clarification!

    JonBJ 1 Reply Last reply
    0
    • l3u_L l3u_

      Hi all :-)

      I have a QTableWidget I individualized using a QStyledItemDelegate so that the editor for one column becomes a QDoubleSpinBox instead of a QLineEdit (as in this column, a price should be entered and not text).

      The problem is that if one e.g. enters "5,00 €" in the spin box, I still get "5" after pressing enter, or "5,5" for "5,50 €".

      I searched how to change this, but it seems that changing the DisplayRole of some data requires to implement an own QTableView using a custom QAbstractTableModel. Is this the case? I only need to do this one thing, everything else would be fine how the QTableWidget does it ;-)

      Thanks for all clarification!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @l3u_ said in Individual formatting of a QTableWidget's column:

      The problem is that if one e.g. enters "5,00 €" in the spin box, I still get "5" after pressing enter, or "5,5" for "5,50 €".

      It's a double spin box for entering a floating point number. 5,00 == 5 and 5,50 == 5,5 as numbers. Where exactly are you seeing the text you are quoting?

      Do you mean when the editing spin box has gone, the value the user entered has been stored as data and the QTableWidget is displaying that as uneditable text?

      If this the case then you have two choices:

      • If you want that column to always be shown as, say, 2 decimal places with a euro symbol you can store it as a number like QDoubleSpiinBox::value() gives you, and have the QStyledItemDelegate output it like that always (see displayText()). What the user actually typed is ignored.

      • If you want that column to show what the user actually typed into the spin box, not just its value converted to a double, so you keep e.g. how many trailing 0s were typed or whether a currency symbol was entered, then you would have to store the result not as a number but as text. For that the base class has QAbstractSpinBox::text().

      l3u_L 1 Reply Last reply
      0
      • JonBJ JonB

        @l3u_ said in Individual formatting of a QTableWidget's column:

        The problem is that if one e.g. enters "5,00 €" in the spin box, I still get "5" after pressing enter, or "5,5" for "5,50 €".

        It's a double spin box for entering a floating point number. 5,00 == 5 and 5,50 == 5,5 as numbers. Where exactly are you seeing the text you are quoting?

        Do you mean when the editing spin box has gone, the value the user entered has been stored as data and the QTableWidget is displaying that as uneditable text?

        If this the case then you have two choices:

        • If you want that column to always be shown as, say, 2 decimal places with a euro symbol you can store it as a number like QDoubleSpiinBox::value() gives you, and have the QStyledItemDelegate output it like that always (see displayText()). What the user actually typed is ignored.

        • If you want that column to show what the user actually typed into the spin box, not just its value converted to a double, so you keep e.g. how many trailing 0s were typed or whether a currency symbol was entered, then you would have to store the result not as a number but as text. For that the base class has QAbstractSpinBox::text().

        l3u_L Offline
        l3u_L Offline
        l3u_
        wrote on last edited by l3u_
        #3

        @JonB Yes, exactly. The entered text is displayed formatted by the QDoubleSpinBox. I'm totally aware of the fact the the value is of course a naked double in this case, no matter how I format the spin box.

        My question was if it's possble to tweak what's displayed by the QTableWidget for one column (i.e. by changing the DisplayRole) without having to implement the whole thing.

        I can't use a QSortFilterProxyModel, as setting a model is private for a QTableWidget. I can't use QStyledItemDelegate::displayText either, because there's no way to see from which column the data comes …

        Edit:

        Oh, I cross-posted or you cross-edited. Well, as said, I can't use QStyledItemDelegate::displayText. Well, storing the actual text would be a possibility, but I would have to process the text later on, to get the double value back, because I use the data entered later …

        JonBJ 1 Reply Last reply
        0
        • l3u_L l3u_

          @JonB Yes, exactly. The entered text is displayed formatted by the QDoubleSpinBox. I'm totally aware of the fact the the value is of course a naked double in this case, no matter how I format the spin box.

          My question was if it's possble to tweak what's displayed by the QTableWidget for one column (i.e. by changing the DisplayRole) without having to implement the whole thing.

          I can't use a QSortFilterProxyModel, as setting a model is private for a QTableWidget. I can't use QStyledItemDelegate::displayText either, because there's no way to see from which column the data comes …

          Edit:

          Oh, I cross-posted or you cross-edited. Well, as said, I can't use QStyledItemDelegate::displayText. Well, storing the actual text would be a possibility, but I would have to process the text later on, to get the double value back, because I use the data entered later …

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @l3u_
          I had already appended to my previous answer for this.

          First confirm which of the two you want. Let's guess it's the first, you want it stored as a number and just want to control the output format. Then the simplest is to use QStyledItemDelegate::displayText().

          To make this operate only on a given column. Either create your own class DoubleStyledItemDelegate: public QStyledItemDelegate and install on just that column via QAbstractItemView::setItemDelegateForColumn(). Or in a common QStyledItemDelegate you would override QStyledItemDelegate::paint(), which is passed the model index to identify the column, and produce the desired text to print there (example at, say, https://stackoverflow.com/questions/34729858/override-text-in-qstyleditemdelegate-for-qtreeview). If you can, the first alternative seems preferable and re-usable.

          l3u_L 1 Reply Last reply
          0
          • JonBJ JonB

            @l3u_
            I had already appended to my previous answer for this.

            First confirm which of the two you want. Let's guess it's the first, you want it stored as a number and just want to control the output format. Then the simplest is to use QStyledItemDelegate::displayText().

            To make this operate only on a given column. Either create your own class DoubleStyledItemDelegate: public QStyledItemDelegate and install on just that column via QAbstractItemView::setItemDelegateForColumn(). Or in a common QStyledItemDelegate you would override QStyledItemDelegate::paint(), which is passed the model index to identify the column, and produce the desired text to print there (example at, say, https://stackoverflow.com/questions/34729858/override-text-in-qstyleditemdelegate-for-qtreeview). If you can, the first alternative seems preferable and re-usable.

            l3u_L Offline
            l3u_L Offline
            l3u_
            wrote on last edited by
            #5

            @JonB Oh, I didn't know you could install a delegate for just one column. I think that will work then, I'll try this. Thanks for the hint :-)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved