I have a table in which I put numbers (as a string) in a column. For some reason, it appears that numbers with 2 or more periods (i.e. 5.5.5) will align on the left side of the cell, while numbers with fewer periods (i.e. 55.5) will align on the right side of the cell. Does anyone know how to change this?
PyQt 3.3.6: How to align text in a table?
4.9k views Asked by Jon At
2
There are 2 answers
0
On
I had a similar problem. My solution was slightly different. When filling each item into your table, check it matches your '5.5.5' format and set the item to be right aligned.
cell = QTableWidgetItem(value)
tableWidget.setItem(row, col, cell)
# check the value matches your requirement, via regex or as below
check = value.replace('.', '')
if check.isdigit():
tableWidget.item(row, col).setTextAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignVCenter)
I understand that the characters used for the thousands separator and the decimal mark may differ between locales, but surely no locale could sensibly interpret
5.5.5
as a number? Given that, it's hardly surprising that Qt wants to treat it as ordinary text.But anyway, the docs for QTableItem suggest you can work around this by reimplementing the alignment function:
The implementation of
is_pseudo_number()
is left as an exercise for the reader...(PS: since you are using PyQt3, the above code is completely untested)