简体   繁体   English

无法在 Virtualenv(Ubuntu)中运行的 Qt 设计器中显示自定义 PyQt5 小部件插件

[英]Can't get custom PyQt5 widget plugin to show up in Qt designer running in Virtualenv (Ubuntu)

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 .我正在尝试在 PyQt Designer 上使用自定义小部件,使用以下代码: https ://github.com/baoboa/pyqt5/blob/master/examples/designer/plugins/plugins.py 以及来自https://的插件和小部件github.com/tranter/blogs/tree/master/PythonPlugin

My Designer is installed win ubuntu via apt-get designer and as per我的设计师是通过apt-get designer Designer 安装 win ubuntu 并按照

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

the led widget and plugin are in two directories at same level of the run script (see the code): 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_())

When I launch the script in main enviroment I can see my custom widget:当我在主环境中启动脚本时,我可以看到我的自定义小部件:

设计师裁剪

but when I run the script from inside a virtualenv the led custom widget doesnt show up.但是当我从 virtualenv 内部运行脚本时,led 自定义小部件不会出现。

I can install designer in my virtualenv via pip install pyqt5-tools and the run我可以通过pip install pyqt5-tools在我的 virtualenv 中安装设计器并运行

pyqt5-tools designer -p **PATH_to_both_widget.py_widgetplugin.py** to have my custom pyqt5-tools designer -p **PATH_to_both_widget.py_widgetplugin.py**有我的自定义

widgets available, but I can't figure out why the first approach doesnt work and is draving me crazy, any idea about it小部件可用,但我不知道为什么第一种方法不起作用并且让我发疯,关于它的任何想法

The key is to modify the includeFile method since the import is built from there, for my project structure:关键是修改includeFile方法,因为导入是从那里构建的,对于我的项目结构:

├── 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设计器.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 .完整示例可在此处找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM