Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align right and vertically center in QAbstractTableModel?

Tags:

python

pyside

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.

like image 525
Norbert Sebők Avatar asked Feb 03 '16 11:02

Norbert Sebők


People also ask

How do I center text vertically in a div?

In most cases, you can also center text vertically in a div by setting the line-height property with a value that is equal to the container element's height.

How do you align text boxes vertically in HTML?

Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .

How do I align text to the top center in HTML?

To just center the text inside an element, use text-align: center; This text is centered.


Video Answer


1 Answers

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).

like image 122
Norbert Sebők Avatar answered Sep 19 '22 14:09

Norbert Sebők