Reputation: 131
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?
Upvotes: 0
Views: 1638
Reputation: 1282
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!!
Upvotes: 0
Reputation: 120678
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.
Upvotes: 1