python - 无法在 Qt 设计器(macos)中显示自定义 PyQt5 小部件插件
问题描述
我想使用 python(3.7 和 pyqt5)为 Qt Designer 制作一个自定义小部件插件。一切都应该工作,但它没有出现在 Qt Designer 中。
这是我经过多次试验和错误以及其他有类似问题的其他人的一点帮助后所做的事情(Qt Designer: could not find custom PyQt widget plugins and Custom QWidgets. How do I build/get the pyqt5 plugin for Qt Designer on马克?)
我从 Qt 网站安装了 Qt Designer (Creator) 5.13。
我从源代码而不是 pip 安装了 SIP (4.9.18) 和 PyQt5 (5.13.0),因为有必要获取必要的 libpyqt5.dylib 文件(Mac 没有 pyqt5-tools)。我把这个文件放在 /Users/[user]/Qt/5.13.0/clang_64/plugins/designer 目录下
我只想在制作自己的插件之前正确设置。因此,我从https://github.com/baoboa/pyqt5/tree/master/examples/designer/plugins下载了analogclock.py 和analogclockplugin.py并修改了plugins.py 文件,如下所示:
from PyQt5.QtCore import QLibraryInfo, QProcess, QProcessEnvironment
# Tell Qt Designer where it can find the directory containing the plugins and
# Python where it can find the widgets.
env = QProcessEnvironment.systemEnvironment()
env.insert('PYQTDESIGNERPATH', '[path to the plugin.py files]/designer_plugins')
env.insert('PYTHONPATH', '[path to the widgets]/designer_widgets')
# Start Designer.
designer = QProcess()
designer.setProcessEnvironment(env)
designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath)
designer_bin = '/Users/[user]/Qt/5.13.0/clang_64/bin/Designer.app/Contents/MacOS/Designer'
designer.start(designer_bin)
designer.waitForFinished(-1)
我运行了 plugins.py。Qt Designer 正确打开,当我检查 Designer-->About Plugins 时,我在 Loaded Plugins 中看到 libpyqt5.dylib。但是,PyAnalogClock 小部件不在其中,插件小部件不在左侧小部件框中。
我尝试通过从终端设置环境变量来进行调试,如下所示:
[user]$ export QT_DEBUG_PLUGINS=1
[user]$ export PYQTDESIGNERPATH='[path to the widgets]/designer_widgets'
[user]$ export PYTHONPATH='[path to the widgets]/designer_widgets'
[user]$ /Users/[user]/Qt/5.13.0/clang_64/bin/Designer.app/Contents/MacOS/Designer
输出的相关部分是这样的:
Found metadata in lib /Users/[user]/Qt/5.13.0/clang_64/plugins/designer/libpyqt5.dylib, metadata=
{
"IID": "org.qt-project.Qt.QDesignerCustomWidgetCollectionInterface",
"archreq": 0,
"className": "PyCustomWidgets",
"debug": false,
"version": 331008
}
loaded library "/Users/[user]/Qt/5.13.0/clang_64/plugins/designer/libpyqt5.dylib"
并接近尾声
loaded library "Python.framework/Versions/3.7/Python"
ModuleNotFoundError: No module named 'PyQt5'
ModuleNotFoundError: No module named 'PyQt5'
- 当我没有从命令行导出 PYQTDESIGNERPATH 和 PYTHONPATH 时,这个错误就消失了。但是当然,Qt 不知道文件在哪里。
无论如何,这就是我目前的状态。我不明白为什么 Qt Designer 找不到 PyQt5 模块,或者我还能尝试什么让它工作。
解决方案
这是一个 ♂️ momemt 所以,我在 PyQt 参考指南上找到了这个,我不知何故忽略了它
- 有关定义新 Qt 信号、插槽和属性及其插件的自定义小部件的简单但完整且完整记录的示例,请查看 PyQt5 源包的示例/设计器/插件目录
好的。做过某事。运行演示。它工作正常。将整个插件目录复制到其他地方。还好。所以我一定做了一些时髦的命名文件夹之类的东西。无论如何,现在我将致力于制作我自己的插件。因为愚蠢而沮丧的日子。但很高兴它有效。
推荐阅读
- javascript - 为什么`querySelectorAll`不能按预期工作
- javascript - 我在函数调用中不断收到未定义的错误
- javascript - 类型“ITravel”上不存在属性“长度”。TS2339
- node.js - create-react-app 命令卡在使用 cra-template 安装 react react-dom react-scripts
- javascript - Javascript window.open 从 iFrame
- css - 调整元素大小时保持渐变大小
- android - 如何在更改 Activity 时保存 Intent 数据
- java - 通过参数将值传递给我的程序是什么意思?
- flutter - Flutter 导航栏不随主题变化
- pyspark - 如何结合条件 PySpark 的 2 个数据帧