Search code examples
pyqtpyqt5qt-designerpyuicpyrcc

Change the import path of rcc module from Qt Designer


I'm looking for a way to change the import path of the rcc module in the pyuic generated python file.

An example pyuic generated python code from a ui file:

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(249, 103)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
            self.horizontalLayout.setObjectName("horizontalLayout")
            self.pushButton = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton.setText("")
            icon = QtGui.QIcon()
            icon.addPixmap(QtGui.QPixmap(":/gui_icons/res/play.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            self.pushButton.setIcon(icon)
            self.pushButton.setIconSize(QtCore.QSize(32, 32))
            self.pushButton.setObjectName("pushButton")
            self.horizontalLayout.addWidget(self.pushButton)
            MainWindow.setCentralWidget(self.centralwidget)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    import icons_mw_rc

I want to change icons_mw_rc import path to another location to better organize my source files [Example: import qrc_res.icons_mw_rc]. Is there a way to do that using Qt Designer without manually modifying the pyuic generated src file.


Solution

  • The pyuic tool has some options for adjusting the resource import statement. You can achieve the equivalent of your example using the --import-from option:

    pyuic5 --import-from=qrc_res -o mw.py mw.ui
    

    which would add the following line to the generated python module:

    from qrc_res import icons_mw_rc