PyQt 3.3.6:如何对齐 table 中的文本?
PyQt 3.3.6: How to align text in a table?
我有一个 table,我在其中将数字(作为字符串)放在一列中。出于某种原因,似乎具有 2 个或更多句点的数字(即 5.5.5)将在单元格的左侧对齐,而具有较少句点的数字(即 55.5)将在单元格的右侧对齐。有谁知道如何改变这个?
我知道用于千位分隔符和小数点的字符可能因语言环境而异,但肯定没有语言环境可以明智地将 5.5.5
解释为数字?鉴于此,Qt 将其视为普通文本也就不足为奇了。
但是无论如何,QTableItem suggest you can work around this by reimplementing the alignment 函数的文档:
class TableItem(QTableItem):
def alignment(self):
if is_pseudo_number(self.text()):
return Qt.AlignRight
return QTableItem.alignment(self)
...
table.setItem(row, column, TableItem('5.5.5'))
is_pseudo_number()
的实施留作 reader...
的练习
(PS: 由于您使用的是 PyQt3,以上代码完全未经测试)
我遇到了类似的问题。我的解决方案略有不同。
将每个项目填充到您的 table 时,检查它是否符合您的“5.5.5”格式并将项目设置为右对齐。
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)
我有一个 table,我在其中将数字(作为字符串)放在一列中。出于某种原因,似乎具有 2 个或更多句点的数字(即 5.5.5)将在单元格的左侧对齐,而具有较少句点的数字(即 55.5)将在单元格的右侧对齐。有谁知道如何改变这个?
我知道用于千位分隔符和小数点的字符可能因语言环境而异,但肯定没有语言环境可以明智地将 5.5.5
解释为数字?鉴于此,Qt 将其视为普通文本也就不足为奇了。
但是无论如何,QTableItem suggest you can work around this by reimplementing the alignment 函数的文档:
class TableItem(QTableItem):
def alignment(self):
if is_pseudo_number(self.text()):
return Qt.AlignRight
return QTableItem.alignment(self)
...
table.setItem(row, column, TableItem('5.5.5'))
is_pseudo_number()
的实施留作 reader...
(PS: 由于您使用的是 PyQt3,以上代码完全未经测试)
我遇到了类似的问题。我的解决方案略有不同。 将每个项目填充到您的 table 时,检查它是否符合您的“5.5.5”格式并将项目设置为右对齐。
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)