[PyKDE] code in forms Qt Designer feature supports Python as well as C++?

Hans-Peter Jansen hpj at urpla.net
Tue Jul 16 12:52:00 BST 2002


Am Montag, 15. Juli 2002 19:03 schrieb Christian Bird:

[...]
> 8.  Normally, you could enter C++ code in here and when the moc file was
> created with uic, the C++ code would be included in the class.  In
> addition, the connections between the signal and this slot would be created
> in the moc file as well.  Instead however, you can now enter python code
> between the two curly braces and it will be placed directly into the python
> file created when you use pyuic.  Enter something trivial between the curly
> braces here:
>
> void Form1::PushButton1_clicked()
> {
> self.clicked = 1
> for i in range(10):
> 	print "I was clicked #", i
> }

Note the indention here. pyuic handles it correctly, if you start coding
in column 0.

>
> 9.  Now save everything and run pyuic on the resulting ui file (note that
> the python code you just entered is saved in a ui.h file and must be in the
> same directory as the ui file when you run pyuic).  The .py file that is
> output should include something looking like this:
>
> class Form1(QDialog):
>
> 	... stuff here ...
>
> 	def PushButton1_clicked(self):
> 		self.clicked = 1
> 		for i in range(10):
> 			print "I was clicked #", i
>
> It is a little weird entering python code where C++ code is expected and
> you should turn off most options in designer that affect the editor window
> because it tries to force C++ syntax which is very much NOT python syntax.

You should note, that it's currently not possible to handle includes
in a sensible way (do it locally, when needed), nor to create your own 
classes this way. OTOH, this feature is very handy in doing simple 
things. More sophisticated demands gets you back to subclassing in a 
separate module, which isn't that bad, either.

> Let me know if you have any questions.
>
> -- Christian Bird

Well done, Christian
  Hans-Peter




More information about the PyQt mailing list