Individual formatting of a QTableWidget's column
-
Hi all :-)
I have a
QTableWidgetI individualized using aQStyledItemDelegateso that the editor for one column becomes aQDoubleSpinBoxinstead of aQLineEdit(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
QTableViewusing a customQAbstractTableModel. Is this the case? I only need to do this one thing, everything else would be fine how theQTableWidgetdoes it ;-)Thanks for all clarification!
-
Hi all :-)
I have a
QTableWidgetI individualized using aQStyledItemDelegateso that the editor for one column becomes aQDoubleSpinBoxinstead of aQLineEdit(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
QTableViewusing a customQAbstractTableModel. Is this the case? I only need to do this one thing, everything else would be fine how theQTableWidgetdoes it ;-)Thanks for all clarification!
@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 == 5and5,50 == 5,5as 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
QTableWidgetis 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 theQStyledItemDelegateoutput it like that always (seedisplayText()). 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 trailing0s 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_ 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 == 5and5,50 == 5,5as 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
QTableWidgetis 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 theQStyledItemDelegateoutput it like that always (seedisplayText()). 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 trailing0s 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().
@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 nakeddoublein this case, no matter how I format the spin box.My question was if it's possble to tweak what's displayed by the
QTableWidgetfor one column (i.e. by changing theDisplayRole) without having to implement the whole thing.I can't use a
QSortFilterProxyModel, as setting a model is private for aQTableWidget. I can't useQStyledItemDelegate::displayTexteither, 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 … -
-
@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 nakeddoublein this case, no matter how I format the spin box.My question was if it's possble to tweak what's displayed by the
QTableWidgetfor one column (i.e. by changing theDisplayRole) without having to implement the whole thing.I can't use a
QSortFilterProxyModel, as setting a model is private for aQTableWidget. I can't useQStyledItemDelegate::displayTexteither, 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 …@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 QStyledItemDelegateand install on just that column via QAbstractItemView::setItemDelegateForColumn(). Or in a commonQStyledItemDelegateyou would overrideQStyledItemDelegate::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_
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 QStyledItemDelegateand install on just that column via QAbstractItemView::setItemDelegateForColumn(). Or in a commonQStyledItemDelegateyou would overrideQStyledItemDelegate::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.