[PyKDE] QT Designer question

Boudewijn Rempt bsarempt at rempt.xs4all.nl
Mon Nov 13 20:32:51 GMT 2000


On Sun, 12 Nov 2000, Jim Sabatke wrote:

> I'm new to Python and QT.  Any advice on how to use QT's Designer and 
> use the needed methods to make programs useful?  All the examples I've 
> seen are in C++, which I don't know.  Can the methods be created in 
> Designer somehow, or do they need to be created externally and loaded 
> into the main Python script?

Basically you generate Python code with pyuic, and then create another
class subclassing the generated code - for instance, creating a new
__init__ that first calls the generated __init__ and then sets some 
fields to proper values. You seldom have to override the done()
or accept() slots since the values in the fields are available until the
reference to the dialog goes out of scope. 

Small example:

----------------------------------mydialog.ui -------------------------------
<!DOCTYPE UI><UI>
<class>MyDialog</class>
<widget>
    <class>QDialog</class>
    <property stdset="1">
        <name>name</name>
        <cstring>MyDialog</cstring>
    </property>
    <property stdset="1">
        <name>geometry</name>
        <rect>
            <x>0</x>
            <y>0</y>
            <width>515</width>
            <height>91</height>
        </rect>
    </property>
    <property stdset="1">
        <name>caption</name>
        <string>MyDialog</string>
    </property>
    <property stdset="1">
        <name>sizeGripEnabled</name>
        <bool>true</bool>
    </property>
    <grid>
        <property stdset="1">
            <name>margin</name>
            <number>11</number>
        </property>
        <property stdset="1">
            <name>spacing</name>
            <number>6</number>
        </property>
        <widget row="1"  column="0"  rowspan="1"  colspan="2" >
            <class>QLayoutWidget</class>
            <property stdset="1">
                <name>name</name>
                <cstring>Layout1</cstring>
            </property>
            <hbox>
                <property stdset="1">
                    <name>margin</name>
                    <number>0</number>
                </property>
                <property stdset="1">
                    <name>spacing</name>
                    <number>6</number>
                </property>
                <spacer>
                    <property>
                        <name>name</name>
                        <cstring>Horizontal Spacing2</cstring>
                    </property>
                    <property stdset="1">
                        <name>orientation</name>
                        <enum>Horizontal</enum>
                    </property>
                    <property stdset="1">
                        <name>sizeType</name>
                        <enum>Expanding</enum>
                    </property>
                    <property>
                        <name>sizeHint</name>
                        <size>
                            <width>20</width>
                            <height>20</height>
                        </size>
                    </property>
                </spacer>
                <widget>
                    <class>QPushButton</class>
                    <property stdset="1">
                        <name>name</name>
                        <cstring>buttonOk</cstring>
                    </property>
                    <property stdset="1">
                        <name>text</name>
                        <string>&amp;OK</string>
                    </property>
                    <property stdset="1">
                        <name>autoDefault</name>
                        <bool>true</bool>
                    </property>
                    <property stdset="1">
                        <name>default</name>
                        <bool>true</bool>
                    </property>
                </widget>
            </hbox>
        </widget>
        <widget row="0"  column="0" >
            <class>QLabel</class>
            <property stdset="1">
                <name>name</name>
                <cstring>TextLabel1</cstring>
            </property>
            <property stdset="1">
                <name>text</name>
                <string>&amp;Some label</string>
            </property>
            <property>
                <name>buddy</name>
                <cstring>LineEdit1</cstring>
            </property>
        </widget>
        <widget row="0"  column="1" >
            <class>QLineEdit</class>
            <property stdset="1">
                <name>name</name>
                <cstring>LineEdit1</cstring>
            </property>
        </widget>
    </grid>
</widget>
<connections>
    <connection>
        <sender>buttonOk</sender>
        <signal>clicked()</signal>
        <receiver>MyDialog</receiver>
        <slot>accept()</slot>
    </connection>
</connections>
</UI>
-------------------------------------- mydialog.py -----------------------
# Form implementation generated from reading ui file 'mydialog.ui'
#
# Created: Mon Nov 13 20:21:40 2000
#      by: The Python User Interface Compiler (pyuic)
#
# WARNING! All changes made in this file will be lost!


from qt import *


class MyDialog(QDialog):
    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        QDialog.__init__(self,parent,name,modal,fl)

        if name == None:
            self.setName('MyDialog')

        self.resize(515,91)
        self.setCaption(self.tr('MyDialog'))
        self.setSizeGripEnabled(1)
        grid = QGridLayout(self)
        grid.setSpacing(6)
        grid.setMargin(11)

        hbox = QHBoxLayout()
        hbox.setSpacing(6)
        hbox.setMargin(0)
        spacer =
QSpacerItem(20,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
        hbox.addItem(spacer)

        self.buttonOk = QPushButton(self,'buttonOk')
        self.buttonOk.setText(self.tr('&OK'))
        self.buttonOk.setAutoDefault(1)
        self.buttonOk.setDefault(1)
        hbox.addWidget(self.buttonOk)

        grid.addMultiCellLayout(hbox,1,1,0,1)

        self.TextLabel1 = QLabel(self,'TextLabel1')
        self.TextLabel1.setText(self.tr('&Some label'))
        self.TextLabel1.setProperty('buddy',QVariant('LineEdit1'))

        grid.addWidget(self.TextLabel1,0,0)

        self.LineEdit1 = QLineEdit(self,'LineEdit1')

        grid.addWidget(self.LineEdit1,0,1)


self.connect(self.buttonOk,SIGNAL('clicked()'),self,SLOT('accept()'))
--------------------------------- thedialog.py ----------------------
import sys
from qt import *
from mydialog import MyDialog

class theDialog(MyDialog):
    def __init__(self,parent = None,name = None,modal = 1,fl = 0):
        MyDialog.__init__(self,parent,name,modal,fl)
        self.LineEdit1.setText("Some useful text")

if __name__ == '__main__':
    a = QApplication(sys.argv)
    QObject.connect(a,SIGNAL('lastWindowClosed()'),a,SLOT('quit()'))
    w = theDialog()
    a.setMainWidget(w)
    w.exec_loop()
    print w.LineEdit1.text()

--------------------------------------------------------------------
run with:

python thedialog.py

Change the text a bit, and stop. (Not that the main is a bit different
from what pyuic -x generates: this actually stops.)





More information about the PyQt mailing list