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.
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.
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 .
To just center the text inside an element, use text-align: center; This text is centered.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With