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

Nor*_*bők 4 python pyside

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_()
Run Code Online (Sandbox Code Playgroud)

我正在使用PySide 1.2.1和Qt 4.8.6.

Nor*_*bők 10

我发现这是一个老bug.幸运的是有一个解决方法.对其他人也许有用:

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