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

时间:2016-02-03 11:01:29

标签: 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_()

我正在使用PySide 1.2.1和Qt 4.8.6。

1 个答案:

答案 0 :(得分:10)

我发现它是一个旧bug。幸运的是有一个workaround。对其他人也许有用:

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