⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c4079.htm

📁 GUI Programming with Python
💻 HTM
字号:
<HTML><HEAD><TITLE>Qt Designer, BlackAdder and uic</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.72"><LINKREL="HOME"TITLE="GUI Programming with Python: QT Edition"HREF="book1.htm"><LINKREL="UP"TITLE="PyQt fundamentals"HREF="p1032.htm"><LINKREL="PREVIOUS"TITLE="Qt Utility classes and their Python equivalents"HREF="x3738.htm"><LINKREL="NEXT"TITLE="Advanced Designer topics"HREF="x4274.htm"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GUI Programming with Python: QT Edition</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="x3738.htm"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="x4274.htm"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="CH7">Chapter 11. Qt Designer, BlackAdder and uic</A></H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT><AHREF="c4079.htm#AEN4107">Introduction</A></DT><DT><AHREF="x4274.htm">Advanced Designer topics</A></DT></DL></DIV><P>BlackAdder is the most powerful Python GUI designer in    existence. In fact, it compares favorably with every other GUI    designer I have ever used. There are other GUI designers for Python,    notably Pythonworks by Secret Labs and Boa Constructor, but    Pythonworks gives you access to a only subset of the relatively    feeble Tkinter GUI toolkit, and Boa Constructor, for wxWindows,    is not integrated into a development environment.</P><P>With BlackAdder's GUI designer you can create dialog    windows, custom widgets and wizards. In the next generation of    BlackAdder, which will be based on Qt 3.0, you can even create    complete main windows with menu's, toolbars and a main widget.    BlackAdder gives you access to a wide range of widgets, and makes    it possible to integrate your own widgets.</P><P>Note that everything mentioned in this chapter    holds equally true for Qt Designer. The combination of Qt, Qt    Designer, pyuic and PyQt gives you exactly the same power&#8212;    just not the same convenience.</P><P>There are a number of unique features to the    GUI designer in BlackAdder:</P><P></P><UL><LI><P>The designer produces XML files that can        be compiled to Python or C++.</P></LI><LI><P>You can create signal/slot connections in        the designer, thus tying together all aspects of interface        logic.</P></LI><LI><P>You can use the layout management classes        of Qt (like <TTCLASS="CLASSNAME">QLayout</TT>). </P></LI><LI><P>You can preview your work in any of the        native styles that Qt supports.</P></LI><LI><P>You can add your own widgets &#8212; even        if they are written in Python instead of C++</P></LI></UL><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="AEN4107">Introduction</A></H1><P>Working with the designer modules includes creating      files with your interface definition, compiling those files to      Python code, and then using that code in your application.</P><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN4110">Starting out with the designer module</A></H2><P>Beginning this process is easy, at least as far as GUI design is        concerned! After choosing <SPANCLASS="GUIMENUITEM">New</SPAN> from the      <SPANCLASS="GUIMENU">File</SPAN> menu, you will be presented with a dialog        that asks you to choose what kind of item you want to create:</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Selecting a template for a new GUI design.</P></DIV></P></DIV><P>This dialog should be moderately familiar to developers        who have worked with other GUI designers, such as Visual Basic        and JBuilder. Currently, the available options are:</P><P></P><UL><LI><P>Dialog</P></LI><LI><P>Wizard</P></LI><LI><P>Widget</P></LI><LI><P>Configuration Dialog</P></LI><LI><P>Dialog with Buttons (bottom)</P></LI><LI><P>Dialog with Buttons (right)</P></LI><LI><P>Tab-Dialog</P></LI></UL><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Adding templates: </B>You are not limited to these choices &#8212; the list is          infinitely extensible, because all valid designer files          (those ending in <TTCLASS="FILENAME">.ui</TT>) are also valid          templates for the designer. You can create a new          template using the Designer, and then copy the          <TTCLASS="FILENAME">.ui</TT> file to the          <TTCLASS="FILENAME">templates</TT> directory in the          <TTCLASS="FILENAME">BlackAdder/properties</TT>  directory. The next time you want to create a          designer file, your template will be among the choices.  Of          the original choices, <SPAN><ICLASS="EMPHASIS">Configuration            Dialog</I></SPAN>, <SPAN><ICLASS="EMPHASIS"> Dialog with Buttons            (Bottom)</I></SPAN>, <SPAN><ICLASS="EMPHASIS">Dialog with Buttons            (Right)</I></SPAN> and <SPAN><ICLASS="EMPHASIS">Tab Dialog</I></SPAN> are          based on <TTCLASS="FILENAME">.ui</TT> files, and are therefore          customizable.</P></BLOCKQUOTE></DIV><P>Dialog is relatively uninteresting. It is a base class for creating        modal and modeless dialog boxes.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>The Dialog template, which appears rather bare.</P></DIV></P></DIV><P>Wizard is more interesting. This template, based on        <TTCLASS="CLASSNAME">QWizard</TT>, offers everything you need to create        these popular "hand-holding" forms.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>The wizard template.</P></DIV></P></DIV><P>Configuration Dialog is interesting, too. It is meant for        application-wide preference dialogs, with a listbox        containing configuration categories on the left, and a new set        of tabbed forms for each configuration category on the right.        Note that you can just as easily put pixmaps in a listbox as        text strings.  It is far more professional to give the user        icons to select from instead of text labels in a        listbox.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>The Configuration dialog template.</P></DIV></P></DIV><P>The dialogs with buttons to the right or to the bottom are        useful, everyday dialogs. The included buttons are already        connected to the methods that close and cancel the dialog, and        the contents are already subject to layout management. Which        constellation you prefer is a matter of taste. For        instance, the KDE desktop standard calls for buttons at the        bottom; but Microsoft often puts the buttons that right-hand        side.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>The dialog-with-the-buttons-on-the-right template.</P></DIV></P></DIV><P>The last default template is for creating a bottom-buttoned dialog        with a tab strip on top.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>The tabbed dialog template.</P></DIV></P></DIV></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN4192">Creating a design</A></H2><P>I'm assuming that you are familiar with the concept of        drawing a gui on a grid. Click on the icon that represents the        widget you want, click on the grid, drag it to where you want,        and alter any properties you want. It's as simple as        that&#8212;I can't make it any more difficult for you.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>A partially filled-in form.</P></DIV></P></DIV><DIVCLASS="SECT3"><H3CLASS="SECT3"><ANAME="AEN4201">Grouping widgets</A></H3><P>One thing to keep in mind is the          essential difference between container widgets and normal          widgets. Container widgets can hold other widgets in a          parent-child relation. One example is the groupbox around a          set of radio buttons. It is essential to create the radio          buttons inside the groupbox to make them a set; otherwise it          would be difficult to keep the selection unique. Thus, you          first create the groupbox, drag it to an agreeable size, and          then place the radiobuttons inside the groupbox.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>A groupbox holding radio buttons.</P></DIV></P></DIV></DIV><DIVCLASS="SECT3"><H3CLASS="SECT3"><ANAME="AEN4213">Layout management</A></H3><P>A layoutmanager is a container, too, but          here the procedure is just the other way around. You first          create the widgets. Then, you select all widgets that should          be managed, and then select one of the layout managers          (horizontal, vertical or grid). Every time you add a new          widget to the container (or a spacer object) you will break          the layout and have to recreate it. You can also nest layout          managers, to create more complicated effects.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>The toolbar buttons for the                layout managers <SPANCLASS="GUIBUTTON">set size</SPAN>,:                <SPANCLASS="GUIBUTTON">horizontal</SPAN>,                <SPANCLASS="GUIBUTTON">vertical</SPAN>,                <SPANCLASS="GUIBUTTON">grid</SPAN>, <SPANCLASS="GUIBUTTON">break                  layout</SPAN> and <SPANCLASS="GUIBUTTON">add a                  spring</SPAN>.</P></DIV></P></DIV><P>Layout management can be further          augmented by adding size hints to each widget. These hints          determine whether the widget should stretch as much as          possible, or stay the same size.</P></DIV><DIVCLASS="SECT3"><H3CLASS="SECT3"><ANAME="AEN4232">Tab order and accelerators</A></H3><P>A good GUI allows the user to do          everything with just the keyboard. For this, it is necessary          to give every control its own accelerator key. There are two          possibilities for creating these accelerators. Either the          widget has a label component of its own, in which case          typing an &#38; before the letter you want to make the          accelerator will suffice. Or, and this is more usual, the          widget is purely graphical, but can be associated with a          <TTCLASS="CLASSNAME">QLabel</TT>. Again, the &#38; defines the          accelerator, but you must still somehow link the label with          the widgets. This is done through the          <SPANCLASS="GUIMENU">buddy</SPAN> option in the properties sheet. If          you select the label, and then enter the name of the          associated widget in the buddy field, a link will be          made.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Selecting a buddy.</P></DIV></P></DIV><P>BlackAdder can check for duplicate          accelerators. In the <SPANCLASS="GUIMENU">Edit</SPAN> menu, select          the option <SPANCLASS="GUIMENUITEM">Check Accelerators</SPAN>.          There's a shortcut for this, too:            <BCLASS="KEYCAP">CTRL</B>-<BCLASS="KEYCAP">R</B> .</P><P>Defining accelerators is one part of creating a GUI that          is usable with the keyboard only. The tab order is          important, too. If the user presses the <BCLASS="KEYCAP">Tab</B>          key, the focus should shift to the next widget (from left to right),          instead of going hoppity-skip all over the form.</P><P>Therefore, fixing the tab order should be the last thing you do           after completing a form. This is very          easy: press the right button on the toolbar, or          choose <SPANCLASS="GUIMENU">Tools</SPAN> <SPANCLASS="GUIMENUITEM">Tab            Order</SPAN> (shortcut:            <BCLASS="KEYCAP">F4</B>).          BlackAdder then superimposes small numbered circles on every          widget. You simply click on these widgets in the order you want the focus to follow, and BlackAdder does the rest. Life could not be          more simple!</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Setting the tab order.</P></DIV></P></DIV><P>Setting the tab order right now becomes one of those          pleasurable little tasks that give a developer a bit of          thinking time.</P></DIV></DIV></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"><AHREF="x3738.htm"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="book1.htm"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="x4274.htm"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Qt Utility classes and their Python equivalents</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="p1032.htm"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Advanced Designer topics</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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