PyQt6 的 Qt 模块替代品

问题描述 投票:0回答:2

我刚刚将我的应用程序从 PyQt5 迁移到 PyQt6。据我了解,Qt 模块已在 Qt6 中删除。我有“Qt.AlignCenter”、“Qt.ToolButtonTextUnderIcon”、“Qt.LeftToolBarArea”等不再工作的东西。 Qt6 中有此功能的替代方案吗?

python enums pyqt pyqt6
2个回答
7
投票

Qt 模块仅存在于 PyQt5 中(不在 Qt5 中),允许访问任何子模块的任何类或元素,例如:

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

该模块与属于 QtCore 模块的 Qt 命名空间不同,因此如果您想访问 Qt.AlignCenter 那么您必须从 QtCore 导入 Qt:

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()

4
投票

目前,

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)
© www.soinside.com 2019 - 2024. All rights reserved.