How can I align the currency value in a column? QAbstractTableModel.data() just has strings to return so I get an error trying to .setAlignment() there. The delegate has no ‘role’ parm to align for display. The createEditor works ok with the QLineEdit().setAlignment(Qt.AlignRight).
Do I need to implement paint() for display of text?
Maybe THIS will give you a hint.
I know that. If I use the QTableWidget (not my question here, I’m using QTableView/QAbstractTableModel with QStyledItemDelegate for editing) and set the QTableWidgetItem to the cell with setCellWidget, I can use the alignment of the QTableWidgetItem, along with a QValidator.
I’m trying to learn the Model/View concepts. I’ve got them working for what I need except for alignment.
Can you try using setTextAlignment()
instead of setAlignment()
?
I kept on searching, and of course it was right in front of me (in the Book!) - Learpyqt qtableview modelview. It is part of the model, data() function
…
elif role == Qt.TextAlignmentRole:
if index.column() in (amt_columns):
return Qt.AlignRight
For some reason it does not work correctly if I
return Qt.AlignRight | Qt.AlighVCenter
Edited:
read further on the page and saw Qt.AlignVCenter + Qt.AlignRight
But:
TypeError: unsupported operand type(s) for +: ‘PySide2.QtCore.Qt.AlignmentFlag’ and ‘PySide2.QtCore.Qt.AlignmentFlag’