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

Christian Bird cbird at lineo.com
Mon Jul 15 19:04:00 BST 2002


On Monday 15 July 2002 09:31, Miller, Douglas wrote:
> Trolltech announced this feature for their current Qt Designer: code in
> forms Qt Designer provides a code editor so that you can code your slots
> directly; the code is stored in .ui.h files and eliminates the need for
> sub-classing (although you can still subclass if you prefer).
> How does Riverbanks' announcement "... ability of pyuic to extract Python
> code from Qt Designer files ..." from below relate to the above Trolltech
> announcement?  In other words, can the process of entering Python Code in
> the Qt Designer and extracting it out again be outlined for us?  What are
> the considerations and limitations of doing so.  Thanks.
>
> Riverbank Announcement:
> 7 July 2002
> v3.3 of SIP and PyQt have been released. Highlights of the release are a
> greatly enhanced debugger, lots of example scripts demonstrating Qt's SQL
> classes, the ability of pyuic to extract Python code from Qt Designer
> files, and a new build system.
>
>
>
>
> Douglas K. Miller

This is now documented in the PyQt html doc files, but perhaps you've read 
that and it didn't help (I'm not that great at explaining things I guess, but 
I'll give it a try here.)

In QT designer fot qt3 you have the ability to edit C++ code inside of 
designer and it will be added to the moc file when it is created. 

Do the following to see an example:

1.  Open up designer (must be designer for qt3) and create a blank dialog with 
nothing on it (file->new)

2. Create a QPushButton by clicking on the "OK" labelled button on the toolbar 
and then dragging a rectangle on the dialog form.

3. If the Property Editor window is not open, open it by selecting 
Window->views->property editor from the menu.

4. Now click on the button once and the property editor floating window should 
show the info for the current button.

5. Click on the Signal Handlers tab in the property editor window and you'll 
see all of the signals emitted by the button.

6. Double click on the item "clicked()" and a new method signature will be 
created in a new cell below it.  You can change this name if you like, but 
for now leave it like it is and click on it again (or double click, I'm not 
sure.)

7.  A simple text editor window will appear with the cursor at the beginning 
of a C++ method corresponding to the method under the "clicked()" signal in 
the property editor.

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
}


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.

Let me know if you have any questions.  

-- Christian Bird




More information about the PyQt mailing list