'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.
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:
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
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | eyllanesc |

