index.lxp@lxpwrap=x4274_252ehtm.htm

来自「GUI Programming with Python」· HTM 代码 · 共 1,446 行 · 第 1/3 页

HTM
1,446 行
字号
          you may follow the dictates of your heart (or of your          primary platform).</P><P>Ultimately, the layout management offered by the          Designer is useful and sufficient for most cases; in certain          cases you might want to experiment with coding the layout          management yourself. This is a lot more flexible, but it          takes a lot more time, too.</P></DIV></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Generating and using Python code with pyuic</A></H2><P>We have already converted a Designer        design to Python code. This can be done using either        BlackAdder, with the menu option <SPANCLASS="GUIMENUITEM">Compile          Form</SPAN> from the <SPANCLASS="GUIMENU">File</SPAN> menu, or        with a stand-alone utility, such as        <TTCLASS="FILENAME">pyuic</TT>.</P><P>The stand-alone utility        <BCLASS="COMMAND">pyuic</B> has an interesting option that is        currently  not present in        the BlackAdder IDE. Using the <TTCLASS="PARAMETER"><I>-x</I></TT> parameter, a small stub is        generated at the bottom of the file that enables you to run        the generated code directly.</P><P>The resulting Python file has all the        hallmarks of generated code. That is to say, it is a mess you        won't want to edit by hand. Especially since it will be        regenerated every time you change your form.      </P><P>The right way to work with these        generated files is to subclass them. If you have created a        form, for example <TTCLASS="FILENAME">frmcomplex.py</TT>, that        contains the generated class        <TTCLASS="CLASSNAME">FrmComplex</TT>, then your next step is to        create a new Python file, <TTCLASS="FILENAME">dlgcomplex.py</TT>,        which contains the following class definition:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 11-1. dlgcomplex.py &#8212; a subclass of frmcomplex.py</B></P><PRECLASS="PROGRAMLISTING">## dglcomplex.py#import sysfrom qt import *from frmcomplex import FrmComplex                          <IMGSRC="images/callouts/1.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(1)"></A>class DlgComplex (FrmComplex):                             <IMGSRC="images/callouts/2.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(2)"></A>    def __init__(self, parent = None,name = None,modal = 0,fl = 0):        FrmComplex.__init__(self, parent, name, fl)        <IMGSRC="images/callouts/3.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(3)"></A>    def accept(self):                                      <IMGSRC="images/callouts/4.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(4)"></A>        print "OK is pressed"        FrmComplex.accept(self)                            <IMGSRC="images/callouts/5.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(5)"></A>    def reject(self):        print "Cancel pressed"        QDialog.reject(self)                               <IMGSRC="images/callouts/6.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(6)"></A>if __name__ == '__main__':                                 <IMGSRC="images/callouts/7.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(7)"></A>    a = QApplication(sys.argv)    QObject.connect(a,SIGNAL('lastWindowClosed()'),a,SLOT('quit()'))    w = DlgComplex()                                       <IMGSRC="images/callouts/8.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(8)"></A>    a.setMainWidget(w)    w.show()    a.exec_loop()        </PRE><DIVCLASS="CALLOUTLIST"><DLCOMPACT="COMPACT"><DT><A href="index.lxp@lxpwrap=x4274_252ehtm.htm#IMPORT"><IMGSRC="images/callouts/1.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(1)"></A></DT><DD>Importing the generated                class</DD><DT><A href="index.lxp@lxpwrap=x4274_252ehtm.htm#CLASSDEF"><IMGSRC="images/callouts/2.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(2)"></A></DT><DD>This form is a subclass of the                generated class</DD><DT><A href="index.lxp@lxpwrap=x4274_252ehtm.htm#PARENTCALL"><IMGSRC="images/callouts/3.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(3)"></A></DT><DD>By passing                <TTCLASS="PARAMETER"><I>self</I></TT> to the parent class, all                references in the parent class will be routed                <SPAN><ICLASS="EMPHASIS">via</I></SPAN> the definitions in the                subclass.</DD><DT><A href="index.lxp@lxpwrap=x4274_252ehtm.htm#SUBACCEPT"><IMGSRC="images/callouts/4.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(4)"></A></DT><DD>Any slot of method of QDialog                can be reimplemented in a subclass of QDialog. In this                case, the <TTCLASS="FUNCTION">accept()</TT> and                <TTCLASS="FUNCTION">reject()</TT> methods are                re-implemented to add custom behavior to the OK and                Cancel actions. Remember that we have already created                the connections between the                <TTCLASS="FUNCTION">clicked()</TT> signals of these                buttons and these methods in the Designer.              </DD><DT><A href="index.lxp@lxpwrap=x4274_252ehtm.htm#CALLTOPARENTACCEPT"><IMGSRC="images/callouts/5.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(5)"></A></DT><DD>However, if you want to make use                of the default functionality of the                <TTCLASS="CLASSNAME">QDialog</TT> class, you must also                call the implementation of the subclassed function in                the parent class.              </DD><DT><A href="index.lxp@lxpwrap=x4274_252ehtm.htm#CALLTOQTDIALOG"><IMGSRC="images/callouts/6.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(6)"></A></DT><DD>Because the generated code in                FrmComplex doesn't really add anything, calling                <TTCLASS="CLASSNAME">QDialog</TT>.<TTCLASS="FUNCTION">reject()</TT>                 works just as well.              </DD><DT><A href="index.lxp@lxpwrap=x4274_252ehtm.htm#STUB"><IMGSRC="images/callouts/7.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(7)"></A></DT><DD>This is a stub main to test the                dialog.              </DD><DT><A href="index.lxp@lxpwrap=x4274_252ehtm.htm#CREATEDIALOG"><IMGSRC="images/callouts/8.gif"HSPACE="0"VSPACE="0"BORDER="0"ALT="(8)"></A></DT><DD>Make sure you instantiate the                right class: the <TTCLASS="CLASSNAME">DlgComplex</TT>,                not the <TTCLASS="CLASSNAME">frmComplex</TT>! Cutting and                pasting can lead to difficult-to-find bugs &#8212;I                have all too often copied the stub from the parent                file and forgot to change the classname...              </DD></DL></DIV></DIV><P>The next move is extending the constructor of        the derived class to set the initial values of the various        widgets.</P><DIVCLASS="EXAMPLE"></A><P><B>Example 11-2. Setting default values</B></P><PRECLASS="PROGRAMLISTING">    def __init__(self, parent = None,name = None,modal = 0,fl = 0):        FrmComplex.__init__(self, parent, name, fl)        self.ListBox2.insertItem("That's a turnip")        self.ListBox2.insertItem("Further nonsense")        self.RadioButton1.setChecked(1)        </PRE></DIV><P>As you can see, it's simply a matter of        remembering what names you gave each widget, and inserting        stuff &#8212; no rocket science here.</P><P>Accessing the values of each widget        after the user has pressed <SPANCLASS="GUIBUTTON">OK</SPAN> is just        as easy. A dialog may disappear from screen when the user        presses <SPANCLASS="GUIBUTTON">OK</SPAN>, but that does not mean that        the dialog has disappeared from memory. As long as there is a        variable that points to the dialog, you can access each and        every field.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Generating C++ code with uic</A></H2><P>Qt is originally a C++ toolkit &#8212;        and if you acquire a license for Qt, be it the free, GPLed        Unix/X11 version or the (non-)commercial Windows/Unix license,        you can take the <TTCLASS="FILENAME">.ui</TT> files you have        created with BlackAdder and compile them to C++ using the        <BCLASS="COMMAND">uic</B> utility.</P><P>C++ is a bit more complicated than        Python, and this is reflected in the more complex procedure        you need to follow when converting a <TTCLASS="FILENAME">.ui</TT>        to C++. First of all, you need to generate the        <SPAN><ICLASS="EMPHASIS">header</I></SPAN> files with the <BCLASS="COMMAND">uic -o          dialog.h dialog.ui</B> command. Next, you generate the        actual C++ implementation with the <BCLASS="COMMAND">uic -i dialog.h -o          dialog.cpp dialog.ui</B> command. The        <TTCLASS="OPTION">-i</TT> tells <BCLASS="COMMAND">uic</B> to        <SPAN><ICLASS="EMPHASIS">include</I></SPAN> the header file,        <TTCLASS="FILENAME">dialog.h</TT>.</P><P>From that moment on, the work is the        same as with Python. You subclass the generated code, adding        real implementation logic. Clever usage will include using        <BCLASS="COMMAND">make</B> to autogenerate the header and        implementation files to ensure that the design of the forms in        the compiled app always corresponds to the latest        designs.</P></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><A accesskey="P" href="index.lxp@lxpwrap=c4079_252ehtm.htm">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="H" href="index.lxp@lxpwrap=book1_252ehtm">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><A accesskey="N" href="index.lxp@lxpwrap=p4627_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Qt Designer, BlackAdder and uic</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c4079_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Creating real applications with PyQt</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?