Home > Enterprise >  How to define alignment in stylesheets in PySide6?
How to define alignment in stylesheets in PySide6?

Time:10-02

In PySide2 we could use "qproperty-alignment: AlignRight;", but this no longer works in PySide6. Since there are changes in PySide6 and shortcuts are no longer supported I've tried:

  • Alignment.AlignRight
  • Qt.Alignment.AlignRight
  • AlignmentFlag.AlignRight
  • Qt.AlignmentFlag.AlignRight

but nothing seems to work.

Here is the minimal reproducible example:

from PySide6 import QtWidgets

app = QtWidgets.QApplication([])
window = QtWidgets.QLabel('Window from label')
window.setStyleSheet('qproperty-alignment: AlignRight;')
window.setFixedWidth(1000)
window.show()
app.exec()

CodePudding user response:

It seems that Qt6 no longer interprets the text "AlignRight" but requires the integer values, for example Qt::AlignRight is 0x0002 so a possible solution is to convert the flags to integers:

from PySide6 import QtCore, QtWidgets


app = QtWidgets.QApplication([])
window = QtWidgets.QLabel("Window from label")

window.setStyleSheet(f"qproperty-alignment: {int(QtCore.Qt.AlignRight)};")
window.setFixedWidth(1000)

window.ensurePolished()

assert window.alignment() & QtCore.Qt.AlignRight

window.show()

app.exec()
  • Related