setAlignment(QtCore.Qt.AlignCenter) method is not working as expected

Why calling setAlignment on a QStackedLayout doesn't center your widgets, and what to do instead
Heads up! You've already completed this tutorial.

I'm trying to center a login form relative to the window space, but calling setAlignment(Qt.AlignCenter) on my QStackedLayout isn't working as expected. The form stays stuck in the top-left corner. How do I properly center widgets inside a stacked layout?

This is a common source of confusion when working with layouts in Qt. The setAlignment method on a QStackedLayout doesn't do what you might expect, and QFormLayout has its own way of handling alignment. Let's walk through why this happens and how to fix it.

Why setAlignment() on QStackedLayout doesn't center widgets

A QStackedLayout displays only one widget at a time — like a deck of cards where you can only see the top card. Each widget in the stack fills the entire layout area by default. Because of this, calling setAlignment on the stacked layout itself has no visible effect. The alignment you set applies to how the stacked layout positions its child widgets, but since each child widget already expands to fill the available space, there's nothing to "center."

The alignment you actually want needs to be set on the inner layouts — the ones that arrange your form fields and buttons within each page of the stack.

The addStretch() problem

Another common issue is using addStretch() in your vertical layout. A stretch is a flexible spacer that expands to consume available space. If you add a stretch below your form layout, it pushes the form up to the top of the window, which is the opposite of centering.

If you want your content vertically centered, you'd need stretches on both sides (above and below). But for horizontal centering, which is usually what you want with a login form, the solution is different — you set alignment on the layout itself.

How to center a QFormLayout with setFormAlignment

QFormLayout is a specialized layout that arranges widgets in a two-column label–field format. It has its own alignment property called setFormAlignment(), which controls where the form as a whole is positioned within its available space.

The regular setAlignment() method on other layouts (like QVBoxLayout) works as you'd expect — it controls where the child widgets sit within the layout's area. But for QFormLayout, you need setFormAlignment() to move the entire form block.

Here's the difference in practice:

Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PyQt6 Edition) The hands-on guide to making apps with Python — Save time and build better with this book. Over 15K copies sold.

Get the book

python
# This centers a QVBoxLayout's contents:
my_vbox_layout.setAlignment(Qt.AlignmentFlag.AlignCenter)

# This centers a QFormLayout's contents:
my_form_layout.setFormAlignment(Qt.AlignmentFlag.AlignCenter)

Complete example: Centered login form with QStackedLayout

Let's look at a complete working example. This creates a simple two-page app with a login form on the first page and a logout button on the second page, both properly centered:

python
import sys

from PySide6.QtCore import Qt
from PySide6.QtWidgets import (
    QApplication,
    QFormLayout,
    QLabel,
    QLineEdit,
    QMainWindow,
    QPushButton,
    QStackedLayout,
    QVBoxLayout,
    QWidget,
)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Quiz Application")
        self.setFixedWidth(500)
        self.setFixedHeight(500)

        # Login page
        self.login_layout = QVBoxLayout()

        login_form_layout = QFormLayout()

        self.username_edit = QLineEdit()
        self.password_edit = QLineEdit()
        self.username_edit.setFixedWidth(120)
        self.password_edit.setFixedWidth(120)
        login_form_layout.addRow(QLabel("Username"), self.username_edit)
        login_form_layout.addRow(QLabel("Password"), self.password_edit)

        self.login_btn = QPushButton("Log In")
        self.login_btn.setFixedWidth(120)
        self.login_btn.pressed.connect(self.login_cmd)
        login_form_layout.addWidget(self.login_btn)

        self.login_layout.addLayout(login_form_layout)

        # Center the form within its available space
        login_form_layout.setFormAlignment(Qt.AlignmentFlag.AlignCenter)

        self.login_widget = QWidget()
        self.login_widget.setLayout(self.login_layout)

        # Main menu page
        self.main_menu_layout = QVBoxLayout()
        self.logout_btn = QPushButton("Log Out")
        self.logout_btn.setFixedWidth(120)
        self.logout_btn.pressed.connect(self.logout_cmd)
        self.main_menu_layout.addWidget(self.logout_btn)

        # Center the button within its available space
        self.main_menu_layout.setAlignment(Qt.AlignmentFlag.AlignCenter)

        self.main_menu_widget = QWidget()
        self.main_menu_widget.setLayout(self.main_menu_layout)

        # Stack layout to switch between pages
        self.stack_layout = QStackedLayout()
        self.stack_layout.addWidget(self.login_widget)
        self.stack_layout.addWidget(self.main_menu_widget)
        self.stack_layout.setCurrentIndex(0)

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

    def login_cmd(self):
        self.stack_layout.setCurrentIndex(1)

    def logout_cmd(self):
        self.stack_layout.setCurrentIndex(0)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

There are four changes from the original code that make this work:

  1. Removed addStretch() — The stretch was pushing the form to the top of the window. Without it, the layout has room to center the content.
  2. Used setFormAlignment(Qt.AlignmentFlag.AlignCenter) on the QFormLayout — This tells the form layout to center itself within its parent layout's space, rather than sitting in the default top-left position.
  3. Used setAlignment(Qt.AlignmentFlag.AlignCenter) on the QVBoxLayout for the main menu page — For standard layouts like QVBoxLayout, the regular setAlignment() method works fine.
  4. Removed setAlignment() from the QStackedLayout — Since it has no visible effect there, removing it keeps the code clear about where alignment is actually being controlled.

PySide6 and PyQt6 enum syntax for alignment flags

If you're migrating from PySide2 or PyQt5, you'll notice the alignment flag syntax has changed. In PySide6 and PyQt6, you use the fully-qualified enum form:

python
# PySide2 / PyQt5
Qt.AlignCenter

# PySide6 / PyQt6
Qt.AlignmentFlag.AlignCenter

This applies to all enum values across the Qt framework, not just alignment flags. PySide6 also supports the older short form for backwards compatibility, but PyQt6 requires the fully-qualified form.

Summary

When you need to center widgets inside a QStackedLayout, remember that the alignment belongs on the inner layouts of each page, not on the stacked layout itself. For QFormLayout, use setFormAlignment(). For other layouts like QVBoxLayout and QHBoxLayout, use setAlignment(). And watch out for stray addStretch() calls that might be fighting against your alignment settings.

Over 15,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt6
Get the book

Downloadable ebook (PDF, ePub) & Complete Source code

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

setAlignment(QtCore.Qt.AlignCenter) method is not working as expected was written by Martin Fitzpatrick with contributions from Leo Well.

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.