可以在Virtualenv(Ubuntu)中播放QT设计器中获取自定义Pyqt5小部件插件

发布于 2025-01-30 01:12:47 字数 5294 浏览 1 评论 0原文

我正在尝试使用PYQT设计器上的自定义小部件,使用: https://github.com/baoboa/pyqt5/blob/master/examples/designer/designer/plugins/plugins/plugins.py 以及插件和窗口 https://github.com/tranter/blogs/blogs/blogs/tree/master/master/pythonplugin/pythonplugin

我的设计师通过apt-get Designer和按照

QlibraryInfo.location(QlibraryInfo.BinariesPath)/usr/usr/usr/lib/qt5/bin

LED小部件和插件在运行脚本的同一级别中位于两个目录(请参阅代码):

#!/usr/bin/env python3


#############################################################################
##
## Copyright (C) 2013 Riverbank Computing Limited.
## All rights reserved.
##
## This file is part of the examples of PyQt.
##
## You may use this file under the terms of the BSD license as follows:
##
## "Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are
## met:
##   * Redistributions of source code must retain the above copyright
##     notice, this list of conditions and the following disclaimer.
##   * Redistributions in binary form must reproduce the above copyright
##     notice, this list of conditions and the following disclaimer in
##     the documentation and/or other materials provided with the
##     distribution.
##   * Neither the name of Riverbank Computing Limited nor the names of
##     its contributors may be used to endorse or promote products
##     derived from this software without specific prior written
##     permission.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
##
#############################################################################


import sys
import os

from PyQt5.QtCore import QLibraryInfo, QProcess, QProcessEnvironment
from PyQt5.QtWidgets import QApplication, QMessageBox

designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath)

print('designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath) :', designer_bin)

app = QApplication(sys.argv)

QMessageBox.information(None, "PyQt Designer Plugins",
        "<p>This example will start Qt Designer when you click the <b>OK</b> "
        "button.</p>"
        "<p>Before doing so it sets the <tt>PYQTDESIGNERPATH</tt> environment "
        "variable to the <tt>python</tt> directory that is part of this "
        "example.  This directory contains all the example Python plugin "
        "modules.</p>"
        "<p>It also sets the <tt>PYTHONPATH</tt> environment variable to the "
        "<tt>widgets</tt> directory that is also part of this example.  This "
        "directory contains the Python modules that implement the example "
        "custom widgets.</p>"
        "<p>All of the example custom widgets should then appear in "
        "Designer's widget box in the <b>PyQt Examples</b> group.</p>")

# Tell Qt Designer where it can find the directory containing the plugins and
# Python where it can find the widgets.
base = os.path.dirname(__file__)

print('base :',base)

env = QProcessEnvironment.systemEnvironment()





env.insert('PYQTDESIGNERPATH', os.path.join(base, 'python3'))
env.insert('PYTHONPATH', os.path.join(base, 'widgets3'))

# Start Designer.
designer = QProcess()
designer.setProcessEnvironment(env)




designer_bin = '/usr/lib/qt5/bin/designer'

print('\ndesigner_bin :', designer_bin)


 


def process_finished():

    #designer.waitForFinished(-1)
    print('finished')
    
    sys.exit()




designer.start(designer_bin)        


#designer.setProgram(designer_bin)  #anche con questo non cambia da virtualenv non funza
#designer.startDetached()           # da usare insieme a sopra

designer.finished.connect(process_finished)



print('designer.pid() :', designer.pid())

print(env.keys())

print('\nenv PYTHONPATH :', env.value('PYTHONPATH'))

print('\nenv PYQTDESIGNERPATH :', env.value('PYQTDESIGNERPATH'))

sys.exit(app.exec_())

当我在Main Enviroment中启动脚本时,我可以看到我的自定义窗口:

​从Virtualenv内部运行脚本,LED自定义窗口小部件不会显示。

我可以通过pip安装pyqt5-tools和运行

pyqt5-tools设计器-p ** path_to_to_both_widget.py_widgetplugin.py **让我的自定义

窗口>可用,但我不知道为什么第一种方法不起作用,也让我发疯,对此有任何想法

I am trying to use custom widget on PyQt Designer, using code from: https://github.com/baoboa/pyqt5/blob/master/examples/designer/plugins/plugins.py and both plugin and widget from
https://github.com/tranter/blogs/tree/master/PythonPlugin.

My Designer is installed win ubuntu via apt-get designer and as per

QLibraryInfo.location(QLibraryInfo.BinariesPath) is in /usr/lib/qt5/bin

the led widget and plugin are in two directories at same level of the run script (see the code):

#!/usr/bin/env python3


#############################################################################
##
## Copyright (C) 2013 Riverbank Computing Limited.
## All rights reserved.
##
## This file is part of the examples of PyQt.
##
## You may use this file under the terms of the BSD license as follows:
##
## "Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are
## met:
##   * Redistributions of source code must retain the above copyright
##     notice, this list of conditions and the following disclaimer.
##   * Redistributions in binary form must reproduce the above copyright
##     notice, this list of conditions and the following disclaimer in
##     the documentation and/or other materials provided with the
##     distribution.
##   * Neither the name of Riverbank Computing Limited nor the names of
##     its contributors may be used to endorse or promote products
##     derived from this software without specific prior written
##     permission.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
##
#############################################################################


import sys
import os

from PyQt5.QtCore import QLibraryInfo, QProcess, QProcessEnvironment
from PyQt5.QtWidgets import QApplication, QMessageBox

designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath)

print('designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath) :', designer_bin)

app = QApplication(sys.argv)

QMessageBox.information(None, "PyQt Designer Plugins",
        "<p>This example will start Qt Designer when you click the <b>OK</b> "
        "button.</p>"
        "<p>Before doing so it sets the <tt>PYQTDESIGNERPATH</tt> environment "
        "variable to the <tt>python</tt> directory that is part of this "
        "example.  This directory contains all the example Python plugin "
        "modules.</p>"
        "<p>It also sets the <tt>PYTHONPATH</tt> environment variable to the "
        "<tt>widgets</tt> directory that is also part of this example.  This "
        "directory contains the Python modules that implement the example "
        "custom widgets.</p>"
        "<p>All of the example custom widgets should then appear in "
        "Designer's widget box in the <b>PyQt Examples</b> group.</p>")

# Tell Qt Designer where it can find the directory containing the plugins and
# Python where it can find the widgets.
base = os.path.dirname(__file__)

print('base :',base)

env = QProcessEnvironment.systemEnvironment()





env.insert('PYQTDESIGNERPATH', os.path.join(base, 'python3'))
env.insert('PYTHONPATH', os.path.join(base, 'widgets3'))

# Start Designer.
designer = QProcess()
designer.setProcessEnvironment(env)




designer_bin = '/usr/lib/qt5/bin/designer'

print('\ndesigner_bin :', designer_bin)


 


def process_finished():

    #designer.waitForFinished(-1)
    print('finished')
    
    sys.exit()




designer.start(designer_bin)        


#designer.setProgram(designer_bin)  #anche con questo non cambia da virtualenv non funza
#designer.startDetached()           # da usare insieme a sopra

designer.finished.connect(process_finished)



print('designer.pid() :', designer.pid())

print(env.keys())

print('\nenv PYTHONPATH :', env.value('PYTHONPATH'))

print('\nenv PYQTDESIGNERPATH :', env.value('PYQTDESIGNERPATH'))

sys.exit(app.exec_())

When I launch the script in main enviroment I can see my custom widget:

crop from designer

but when I run the script from inside a virtualenv the led custom widget doesnt show up.

I can install designer in my virtualenv via pip install pyqt5-tools and the run

pyqt5-tools designer -p **PATH_to_both_widget.py_widgetplugin.py** to have my custom

widgets available, but I can't figure out why the first approach doesnt work and is draving me crazy, any idea about it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末骤雨初歇 2025-02-06 01:12:47

关键是要修改include方法,因为导入是从那里构建的,对于我的项目结构:

├── scripts
│   ├── designer.py
│   └── plugins
│       └── ledplugin.py
└── src
    ├── lib
    │   ├── __init__.py
    │   └── ledwidget.py
    ├── main.py
    └── ui
        └── main.ui
def includeFile(self):
    # IMPORT:
    # from lib.ledwidget import LedWidget
    return "lib.ledwidget"

designer.py

import sys
import os.path

from PyQt5.QtCore import QCoreApplication, QLibraryInfo, QProcess, QProcessEnvironment


def main():
    app = QCoreApplication([])
    base_dir = os.path.dirname(__file__)
    plugins_dir = os.path.join(base_dir, "plugins")
    lib_dir = os.path.join(base_dir, "..", "src", "lib")

    env = QProcessEnvironment.systemEnvironment()
    env.insert("PYQTDESIGNERPATH", plugins_dir)
    env.insert("PYTHONPATH", lib_dir)

    # Start Designer.
    designer = QProcess()
    designer.setProcessEnvironment(env)

    designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath)

    if sys.platform == "darwin":
        designer_bin += "/Designer.app/Contents/MacOS/Designer"
    else:
        designer_bin += "/designer"

    designer.start(designer_bin)
    designer.waitForFinished(-1)

    sys.exit(designer.exitCode())


if __name__ == "__main__":
    main()

找到完整的示例在这里

The key is to modify the includeFile method since the import is built from there, for my project structure:

├── scripts
│   ├── designer.py
│   └── plugins
│       └── ledplugin.py
└── src
    ├── lib
    │   ├── __init__.py
    │   └── ledwidget.py
    ├── main.py
    └── ui
        └── main.ui
def includeFile(self):
    # IMPORT:
    # from lib.ledwidget import LedWidget
    return "lib.ledwidget"

designer.py

import sys
import os.path

from PyQt5.QtCore import QCoreApplication, QLibraryInfo, QProcess, QProcessEnvironment


def main():
    app = QCoreApplication([])
    base_dir = os.path.dirname(__file__)
    plugins_dir = os.path.join(base_dir, "plugins")
    lib_dir = os.path.join(base_dir, "..", "src", "lib")

    env = QProcessEnvironment.systemEnvironment()
    env.insert("PYQTDESIGNERPATH", plugins_dir)
    env.insert("PYTHONPATH", lib_dir)

    # Start Designer.
    designer = QProcess()
    designer.setProcessEnvironment(env)

    designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath)

    if sys.platform == "darwin":
        designer_bin += "/Designer.app/Contents/MacOS/Designer"
    else:
        designer_bin += "/designer"

    designer.start(designer_bin)
    designer.waitForFinished(-1)

    sys.exit(designer.exitCode())


if __name__ == "__main__":
    main()

The complete example is found here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文