0

In the example below I use setStyleSheet to align text in the label. Unfortunately, it does not work and the text is always aligned to the left. Any help to solve it is appreciated. This example is a reduction of a much larger program and therefore the QVBoxLayout() is used.

import sys
from PyQt6.QtWidgets import (QApplication, QLabel, QMainWindow, QPushButton,
QVBoxLayout, QTextEdit, QWidget
)
   
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Example")
           
        layout = QVBoxLayout()
        layout.setContentsMargins(10, 10, 10, 10)
        layout.setSpacing(10)
         
        self.title = QLabel("Label Text", self)
        self.title.setStyleSheet ("text-align: center;")

        self.output_text = QTextEdit("Text Output")
        self.button1 = QPushButton("Button1")

        layout.addWidget(self.title)
        layout.addWidget(self.button1)

        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Menachem
  • 265
  • 1
  • 4
  • 17
  • I've not used PyQt6 yet, but does the behaviour of PyQt5 not still work? e.g. https://stackoverflow.com/questions/25638261/pyqt5-center-align-a-label – Greymanic Aug 09 '22 at 18:31
  • `self.title.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)`. – ekhumoro Aug 09 '22 at 18:46

0 Answers0