Альтернатива модуля Qt для PyQt6

Я просто переношу свое приложение с PyQt5 на PyQt6. Я так понимаю, что модуль Qt был удален из Qt6. У меня есть такие вещи, как Qt.AlignCenter, Qt.ToolButtonTextUnderIcon, Qt.LeftToolBarArea, которые больше не работают. Есть ли альтернатива этой функциональности в Qt6?

2 ответа

Решение

Модуль Qt существует только в PyQt5 (не в Qt5), который разрешает доступ к любому классу или элементу любого подмодуля, например:

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

Этот модуль отличается от пространства имен Qt, которое принадлежит модулю QtCore, поэтому, если вы хотите получить доступ к Qt.AlignCenter, вы должны импортировать Qt из 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()

В настоящее время AlignCenterи другие можно найти в перечислении AlignmentFlag :

      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)
Другие вопросы по тегам