[PyKDE] Using UI files in your app with PyQt4

Detlev Offenbach detlev at die-offenbachs.de
Tue Jan 24 21:23:30 GMT 2006


I just installed the latest PyQt4 snapshot, which includes your new 
autoconnect feature. I compiled a .ui file and tried to find some trace of 
this feature. However, I didn't succeed (maybe I am getting tiered). How does 
your autoconnect feature work with compiled .ui files?

Detlev

Am Sonntag, 22. Januar 2006 15:19 schrieb Torsten Marek:
> Hello all,
>
> with the latest pyuic snapshot (should appear somewhere next week) and
> latest PyQt4/sip snapshot, signal autoconnect now works, which was the last
> big point for pyuic.
> If you want to use pyuic code in your own application rather than
> generating the code statically, there are several possibilities, which I
> will list here:
>
> .: Just load a user interface and display it:
>
> import sys
> from PyQt4 import QtGui
> from PyQt4.uic import Loader
>
> app = QtGui.QApplication(sys.argv)
> # this is the hard part
> widget = Loader.loadUi("demo.ui")
> widget.show()
> app.exec_()
>
> ## eos
>
> .: Use a user interface with your own class, incl. signal autoconnect
>
> import sys
> from PyQt4 import QtGui
> from PyQt4.uic import Loader
>
> # the class has to have the same base class as your UI
> class DemoImpl(QtGui.QDialog):
>     def __init__(self, *args):
>         QtGui.QWidget.__init__(self, *args)
> 	# just pass self
>         Loader.loadUi("demo.ui", self)
>
>     def on_button1_clicked(self):
>         for s in "This is a demo".split(" "):
> 	    # self.list is a QListWidget
>             self.list.addItem(s)
>
> app = QtGui.QApplication(sys.argv)
> widget = DemoImpl()
> widget.show()
> app.exec_()
>
> ## eos
>
> .: Create a type on the fly and use it as a base class (slightly more
> involved)
>
> import sys
> import cStringIO
> from PyQt4 import QtCore, QtGui
> # this time, we use the code generator
> from PyQt4.uic import Compiler
>
> def compileToType(filename):
>     code_string = cStringIO.StringIO()
>     widget_info = Compiler.compileUi(file(filename, "r"), code_string)
>     # the buffer code_string now contains the generated Python code
>     widget_module = compile(code_string.getvalue(), filename, "exec")
>     exec widget_module
>     return locals()[widget_info["uiclass"]]
>
> app = QtGui.QApplication(sys.argv)
> # ui is a class (the same you would get with 'pyuic4 demo.ui'
> ui = compileToType("demo.ui")
>
> class DemoImpl(QtGui.QDialog, ui):
>     def __init__(self, *args):
>         QtGui.QWidget.__init__(self, *args)
>         self.setupUi(self)
>
>     def on_button1_clicked(self):
>         for s in "This is a demo".split(" "):
>             self.list.addItem(s)
>
> form = DemoImpl()
> form.show()
> app.exec_()
>
> ## eos
>
> best regards
>
> Torsten

-- 
Detlev Offenbach
detlev at die-offenbachs.de




More information about the PyQt mailing list