Skip to main content

PyQt6 UI templates resource converter

Project description

pyqt6rc

GitHub_repo

This library is providing scripts to convert resource paths in files generated by pyuic6 command.
PyQt6 does not provide pyrcc6 script to convert resources, that's the purpose of this package.
In current PyQt6 implementation, py files created by pyuic6 script has wrong resource path.

Provided scripts are converting .ui files into .py files using three different ways:

  • Native >= python3.7 solution using importlib [Use pyqt6rc].
  • Use of importlib_resources, for compatibility with Python3.6+ [Use pyqt6rc with -c option]
  • Adding resource search path using QtCore.QDir.addSearchPath() and modifying generated prefixes [Use pyqt6sp]

More on this topic can be found on StackOverflow.

From version 4.0, parameter -p, --package was removed. Pyqt6rc now determines package name automatically by crawling parent folders and looking for \_\_init\_\_.py file.

Conversion

Generated template using pyuic6 script:

icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icons/icon1.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)

Generated template using pyqt6rc script:

from importlib.resources import path

icon = QtGui.QIcon()
with path("myPackage.resources.icons", "icon1.png") as f_path:
    icon.addPixmap(QtGui.QPixmap(str(f_path)), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)

Generated template using pyqt6rc (-c, --compatible) script:

from importlib_resources import path

icon = QtGui.QIcon()
with path("myPackage.resources.icons", "icon1.png") as f_path:
    icon.addPixmap(QtGui.QPixmap(str(f_path)), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)

Generated template using pyqt6sp script:

import os
from os.path import dirname, normpath
from PyQt6.QtCore import QDir

prefix_resources = [('icons', '../resources/')]
for prefix, resource in prefix_resources:
    sp = QDir.searchPaths(prefix)
    QDir.setSearchPaths(prefix, set(sp + [normpath(os.path.join(dirname(__file__), resource))]))

icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icons:icon1.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)

Usage examples

Package structure example

myPackage
│   __init__.py    
│
└───resources
|   |   __init__.py
│   │   image1.png
│   │   image2.png
│   │   resources.qrc
|   |   ...
|   |
|   └───icons
│       │   __init__.py
│       │   icon1.png
│       │   icon2.png
│       │   ...
│   
└───templates
    │   template1.ui
    │   template2.ui

Batch convert all .ui files located in the templates directory

pyqt6rc /myPackage/templates

Convert template1.ui only

pyqt6rc /myPackage/templates/template1.ui

Convert template1.ui and save it in /tmp directory

pyqt6rc /myPackage/templates/template1.ui -o /tmp

Batch convert all .ui files located in templates directory using importlib_resources

pyqt6rc /myPackage/templates -c

Batch convert all .ui files located in templates directory using setSearchPaths method

pyqt6sp /myPackage/templates

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyqt6rc-0.6.1.tar.gz (13.0 kB view hashes)

Uploaded Source

Built Distribution

pyqt6rc-0.6.1-py3-none-any.whl (30.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page