Qt module alternative for PyQt6
Asked Answered
C

2

9

I'm just migrating my application from PyQt5 to PyQt6. I understand that the Qt module has been removed in Qt6. I have stuff like 'Qt.AlignCenter', 'Qt.ToolButtonTextUnderIcon', 'Qt.LeftToolBarArea', which are no longer working. Is there any alternative for this functionality in Qt6?

Careworn answered 14/1, 2021 at 22:12 Comment(0)
B
9

The Qt module only exists in PyQt5 (not in Qt5) that allowed access to any class or element of any submodule, for example:

$ python
>>> from PyQt5 import Qt
>>> from PyQt5 import QtWidgets
>>> assert Qt.QWidget == QtWidgets.QWidget

That module is different from the Qt namespace that belongs to the QtCore module, so if you want to access Qt.AlignCenter then you must import Qt from QtCore:

import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QLabel


def main():
    app = QApplication(sys.argv)
    w = QLabel()
    w.resize(640, 498)

    w.setAlignment(Qt.Alignment.AlignCenter)
    w.setText("Qt is awesome!!!")
    w.show()

    app.exec()


if __name__ == "__main__":
    main()
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QApplication, QMainWindow, QStyle, QToolBar


def main():
    import sys

    app = QApplication(sys.argv)

    toolbar = QToolBar()
    toolbar.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextUnderIcon)

    icon = app.style().standardIcon(QStyle.StandardPixmap.SP_DesktopIcon)
    toolbar.addAction(icon, "desktop")

    w = QMainWindow()
    w.addToolBar(Qt.ToolBarAreas.LeftToolBarArea, toolbar)
    w.show()

    sys.exit(app.exec())


if __name__ == "__main__":
    main()
Blanco answered 14/1, 2021 at 23:48 Comment(0)
E
5

Currently, the AlignCenter and others can be found under AlignmentFlag enum:

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QPushButton, QVBoxLayout

def create_widget():
    layout = QVBoxLayout()
    button = QPushButton('Cancel')
    layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
    layout.addWidget(button)
Eulaheulalee answered 12/10, 2021 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.