PyQt6 的 Qt 模块替代品 [英] Qt module alternative for PyQt6

查看:100
本文介绍了PyQt6 的 Qt 模块替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是将我的应用程序从 PyQt5 迁移到 PyQt6.我知道 Qt 模块已在 Qt6 中删除.我有诸如Qt.AlignCenter"、Qt.ToolButtonTextUnderIcon"、Qt.LeftToolBarArea"之类的东西,它们不再起作用.Qt6 中是否有此功能的替代方案?

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?

推荐答案

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

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

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

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

这篇关于PyQt6 的 Qt 模块替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆