0

I am brand new to PyQt so bear with me here...

I create a widget with Qt Designer, then compile it and save it as .py. I edit the .py file to add some additional functionality. But then say I need to go back to Qt Designer and update the layout of the GUI. Is there a way to compile that and then update the original .py file with the changes rather than having to manually copy/paste my custom code?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
griffsterb
  • 131
  • 4
  • 12

2 Answers2

1

If you look at the comments at the top of the generated file, you will see this line:

# WARNING! All changes made in this file will be lost!

This makes it quite clear that the file is not meant to be edited. But this is not a problem, because it is just an ordinary python module, and so it's easy enough to import it into your main application and access it from there.

All you need to do is create a subclass of the top-level object from Qt Designer (which will be a QMainWindow, QDialog or QWidget), and then use the setupUi method provided by the generated module to add all of the widgets to an instance of that subclass.

So if the top-level object from Qt Designer was named "MainWindow", pyuic will create a corresponding Ui_MainWindow class that can be imported into a main.py script and used something like this:

from PyQt4 import QtCore, QtGui
from myui import Ui_MainWindow

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.button.clicked.connect(self.handleButton)

    def handleButton(self):
        print('Hello World!')

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

See Using Qt Designer in the PyQt Documentation for more details.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Can you achieve the same thing by directly subclasing `Ui_MainWindow` and calling `Ui_MainWindow.__init__(self)` as it calls `setupUi()` anyway? – three_pineapples Sep 27 '14 at 02:33
  • @three_pineapples. Yes, there are several different ways to achieve the same thing, which are all outlined in the PyQt documentation (which I've now linked to in my answer). I think the method in my answer is more purely compositional, but I don't actually use it much in my own code (it's all just a matter of taste, really). – ekhumoro Sep 27 '14 at 02:54
0

Personally I hate compiling it. But like ekhumoro said, you can import the class. I still dislike this though. pyuic sometimes imports nasty example_rc things, which lowers productivity levels again. If you absolutely must transform .ui to .py, create a .bat file to do it for you. Code for that:

@ECHO OFF
cd [directory of choice]
pyuic[4/5, depends on pyqt] -x example.ui -o example.py

Or whatever you have for your OS. Personally I like to use ui = uic.loadUi('example.ui') and then ui.setupUi(). It allows you to be much much more efficient!!

Lachlan
  • 1,245
  • 5
  • 18
  • 34