user2792002
user2792002

Reputation: 1

How to set --resource-suffix for pyside2-uic like PyQt5.uic.pyuic.py does, and set prefix for class name

I switched from pyqt5 to pyside due to the licenses.

I would like to make the program directory sharp so I want to named the same function source file in a same name rules and different suffix name.

pyside2-rcc tool whith -o option will generate resource file as I expectd.

pyside2-uic tool will call function like readResources to import *qrc file according to xml node <resources>, but in the .py source code file it import source file with suffix name _rc which not expected for import no suffix file.


For example in a pyside project as ui file WindowMain.ui referenced to a resource file resources.qrc,

<!-- WindowMain.ui -->
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QWidget" name="MainWindow">
 </widget>
 <resources>
  <include location="resources.qrc"/>
 </resources>
 <connections>
 </connections>
</ui>
    "pyside2-vsc.rcc.compile.filepath": "${qrc_name}.py",
    "pyside2-vsc.uic.compile.filepath": "${ui_name}.py",
    "qtForPython.rcc.options": [
        "-o",
        "${resourceDirname}${pathSeparator}${resourceBasenameNoExtension}.py"
    ],
    "qtForPython.uic.options": [
        "-o",
        "${resourceDirname}${pathSeparator}${resourceBasenameNoExtension}.py"
    ]
pyside2-rcc ${ProjectFolder}\resources.qrc -o ${ProjectFolder}\resources.py
pyside2-uic ${ProjectFolder}\WindowMain.ui -o ${ProjectFolder}\WindowMain.py
# WindowMain.py #
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

import resources_rc

class Ui_WindowMain(object):
    def setupUi(self, WindowMain):
        if not WindowMain.objectName():
            WindowMain.setObjectName(u"WindowMain")
# omitted code #
# main.py #
from WindowMain import Ui_WindowMain

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = WindowMain()
    w.show()
    sys.exit(app.exec_())
  File "WindowMain.py", line 15, in <module>
    import resources_rc
  File "C:\Program Files\Python37\lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'resources_rc'

I alos want to get class named WindowMain not Ui_WindowMain in the file WindowMain.py

I ran pyside2-uic --help but it fails to give the option to set resources suffix, and only --postfix option to add to all generated classnames.

Usage: C:\Program Files\Python37\lib\site-packages\PySide2\uic [options] [uifile]
Qt User Interface Compiler version 5.15.2

Options:
  -?, -h, --help                Displays help on commandline options.
  --help-all                    Displays help including Qt specific options.
  -v, --version                 Displays version information.
  -d, --dependencies            Display the dependencies.
  -o, --output <file>           Place the output into <file>
  -a, --no-autoconnection       Do not generate a call to
                                QObject::connectSlotsByName().
  -p, --no-protection           Disable header protection.
  -n, --no-implicit-includes    Disable generation of #include-directives.
  -s, --no-stringliteral        Deprecated. The use of this option won't take
                                any effect.
  --postfix <postfix>           Postfix to add to all generated classnames.
  --tr, --translate <function>  Use <function> for i18n.
  --include <include-file>      Add #include <include-file> to <file>.
  -g, --generator <python|cpp>  Select generator.
  --idbased                     Use id based function for i18n
  --from-imports                Python: generate imports relative to '.'

Arguments:
  [uifile]                      Input file (*.ui), otherwise stdin.

I looked up for uic in PyQt5 module, '--resource-suffix' may worked but not tested

#pyuic.py
def main():
    # omitted code #
    # omitted code #
    # omitted code #
    g.add_option("--resource-suffix", dest="resource_suffix", action="store",
            type="string", default="_rc", metavar="SUFFIX",
            help="append SUFFIX to the basename of resource files [default: _rc]")
# uiparser.py #
    def readResources(self, elem):
        """
        Read a "resources" tag and add the module to import to the parser's
        list of them.
        """
        try:
            iterator = getattr(elem, 'iter')
        except AttributeError:
            iterator = getattr(elem, 'getiterator')

        for include in iterator("include"):
            loc = include.attrib.get("location")

            # Apply the convention for naming the Python files generated by
            # pyrcc5.
            if loc and loc.endswith('.qrc'):
                mname = os.path.basename(loc[:-4] + self._resource_suffix)
                if mname not in self.resources:
                    self.resources.append(mname)

Upvotes: 0

Views: 173

Answers (0)

Related Questions