8

The Qt.AlignRight right aligns the text but puts it into the right-top corner. The Qt.AlignRight | Qt.AlignVCenter doesn't work. Puts it into the left-top corner.

Is there a way to keep the text vertically centered and right aligned at the same time?

Code sample:

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_()

I'm using PySide 1.2.1 with Qt 4.8.6.

Norbert Sebők
  • 1,208
  • 8
  • 13

1 Answers1

23

I found that it's an old bug. Luckily there is a workaround. Maybe useful for others too:

Instead of Qt.AlignRight | Qt.AlignVCenter use int(Qt.AlignRight | Qt.AlignVCenter).

Norbert Sebők
  • 1,208
  • 8
  • 13