AttributeError type object 'Qt' has no attribute 'Alignment'

Understanding enum naming changes in PyQt6.1 and how to fix alignment flags
Heads up! You've already completed this tutorial.

If you're working with PyQt6 and run into this error:

python
AttributeError: type object 'Qt' has no attribute 'Alignment'

you're dealing with an enum naming change that was introduced in PyQt6.1. The fix is straightforward once you understand what changed and why.

The Error

Here's a typical traceback you might see:

python
Traceback (most recent call last):
  File "main.py", line 24, in <module>
    window = MainWindow()
  File "main.py", line 17, in __init__
    widget.setAlignment(Qt.Alignment.AlignHCenter | Qt.Alignment.AlignVCenter)
AttributeError: type object 'Qt' has no attribute 'Alignment'

This happens when your code uses Qt.Alignment to access alignment constants, but your installed version of PyQt6 expects a different name for that enum.

What Changed

Starting with PyQt6 6.1 (released May 11, 2021), the enum names were updated to bring PyQt6 in line with PySide6. This is a good change overall — it means code is more portable between the two frameworks — but it does mean that any code written for PyQt6 6.0 needs updating.

The specific change here is that Qt.Alignment was renamed to Qt.AlignmentFlag.

The Fix

Replace Qt.Alignment with Qt.AlignmentFlag in your code.

Before (PyQt6 6.0):

python
widget.setAlignment(Qt.Alignment.AlignHCenter | Qt.Alignment.AlignVCenter)

After (PyQt6 6.1+):

python
widget.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)

That's it! The individual flag values like AlignHCenter and AlignVCenter stay the same — only the parent enum name changed from Alignment to AlignmentFlag.

A Broader Pattern

This renaming wasn't limited to just Qt.Alignment. PyQt6.1 updated several enum names across the library to match PySide6's conventions. The general pattern is that many enum types gained a Flag suffix or had their names adjusted slightly. Here are a few common examples:

PyQt6 6.0 (old) PyQt6 6.1+ (new)
Qt.Alignment Qt.AlignmentFlag
Qt.Orientation Qt.Orientation (unchanged)
Qt.WindowType Qt.WindowType (unchanged)
Qt.Key Qt.Key (unchanged)
QFrame.Shadow QFrame.Shadow (unchanged)

Not every enum changed, which can make it a bit confusing. If you hit a similar AttributeError on a different enum, check the PyQt6 documentation or the Qt documentation for the correct enum name. The error message itself is usually a good clue — if Python says the attribute doesn't exist, try looking up the current name for that enum in the docs.

Checking Your PyQt6 Version

If you're not sure which version of PyQt6 you have installed, you can check with pip:

bash
pip show PyQt6

Or from within Python:

python
from PyQt6.QtCore import PYQT_VERSION_STR
print(PYQT_VERSION_STR)

If your version is 6.1 or later, use Qt.AlignmentFlag. If you're on exactly 6.0, the old Qt.Alignment name would still work — but upgrading and using the new names is recommended, since 6.0 is outdated at this point.

Summary

This error comes down to a simple rename: Qt.Alignment became Qt.AlignmentFlag in PyQt6.1. Update your code to use the new name and you'll be back on track. If you're following along with a book or tutorial written for PyQt6 6.0, keep an eye out for similar enum name changes — they follow the same pattern and are just as easy to fix.

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

Packaging Python Applications with PyInstaller by Martin Fitzpatrick

This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book

Martin Fitzpatrick

AttributeError type object 'Qt' has no attribute 'Alignment' was written by Martin Fitzpatrick.

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.