如何在 QAbstractTableModel 中右对齐和垂直居中?

How to align right and vertically center in QAbstractTableModel?

Qt.AlignRight 右对齐文本,但将其放在右上角。 Qt.AlignRight | Qt.AlignVCenter 不起作用。放在左上角。

有没有办法让文本同时保持垂直居中和右对齐?

代码示例:

from PySide.QtCore import *
from PySide.QtGui import *


class TableView(QTableView):
    def __init__(self):
        QTableView.__init__(self)
        self.setModel(TableModel(self))


class TableModel(QAbstractTableModel):
    def rowCount(self, parent):
        return 1

    def columnCount(self, parent):
        return 2

    def data(self, index, role):
        if role == Qt.DisplayRole:
            return 'text'

        elif role == Qt.TextAlignmentRole:
            return Qt.AlignRight | Qt.AlignVCenter


app = QApplication([])
w = TableView()
w.show()
app.exec_()

我正在使用 PySide 1.2.1 和 Qt 4.8.6。

我发现它是旧的 bug. Luckily there is a workaround。也许对其他人也有用:

而不是 Qt.AlignRight | Qt.AlignVCenter 使用 int(Qt.AlignRight | Qt.AlignVCenter).