Qt Designer is a GPL'ed GUI design editor provided by Trolltech as part of Qt. It generates an XML description of a GUI design. Qt includes uic which generates C++ code from that XML.
PyQt includes pyuic which generates Python code from the same XML. The Python code is self contained and can be executed immediately.
It is sometimes useful to be able to include some specific Python code in the output generated by pyuic. For example, if you are using custom widgets, pyuic has no way of knowing the name of the Python module containing the widget and so cannot generate the required import statement. To help get around this, pyuic will extract any lines entered in the Comment field of Qt Designer's Form Settings dialog that begin with Python: and copies them to the generated output.
Here's a simple example showing the contents of the Comment field.
This comment will be ignored by pyuic. Python: Python:# Import our custom widget. Python:from foo import bar |
Here's the corresponding output from pyuic.
from qt import * # Import our custom widget. from foo import bar |
Thanks to Christian Bird, pyuic will extract Python code entered using Qt Designer to implement slots. In Qt Designer, when you need to edit a slot and the source editor appears, enter Python code between the curly braces. Don't worry about the correct starting indent level, each line is prepended with a correct indentation.
Make sure that the ui.h file is in the same directory as the .ui file when using pyuic. The .ui file implies the name of the .ui.h file so there is no need to specify it on the command line.
Here's an example of a simple slot.
void DebMainWindowFrm::browsePushButtonClicked() { if self.debugging: QMessageBox.critical(self, "Event", "browse pushbutton was clicked!") } |
Here is the resulting code when pyuic is run.
class DebMainWindowFrm(QMainWindow): ...stuff... def browsePushButtonClicked(self): if self.debugging: QMessageBox.critical(self, "Event", "browse pushbutton was clicked!") |
Note that indenting is as normal and that self and all other parameters passed to the slot are available.
If you use this, you will need to turn off all of the fancy options for the C++ editor in Designer as it tries to force C++ syntax and that's naturally annoying when trying to code in Python.
Qt includes the lupdate program which parses C++ source files converting calls to the QT_TR_NOOP() and QT_TRANSLATE_NOOP() macros to .ts language source files. The lrelease program is then used to generate .qm binary language files that are distributed with your application.
Thanks to Detlev Offenbach, PyQt includes the pylupdate program. This generates the same .ts language source files from your PyQt source files.