index.lxp@lxpwrap=x4974_252ehtm.htm
来自「GUI Programming with Python」· HTM 代码 · 共 314 行
HTM
314 行
<table border="0" cellspacing="0" cellpadding="3" width="100%"><tr><td> <div align="center" id="bldcontent"> <a href="../default.htm"><img src="../images/opendocs.png" width="63" height="76" border="0"></a> <br> <div class="symbol">Your OpenSource Publisher™</div> </div> </td></tr></table> <div align="center" class="author"> <a href="../products.lxp">Products</a> | <a href="../wheretobuy.lxp">Where to buy</a> | <a href="../bookstore.lxp">Retailers</a> | <a href="../faq.lxp">FAQ</a> | <a href="../writeforus.lxp">Write for Us.</a> | <a href="#contact">Contact Us.</a> </div> <table border="0" cellspacing="3" cellpadding="0" width="100%"><tr><td width="100%"> <div class="content"> <table border="0" cellspacing="2" cellpadding="0" width="100%"><tr><td width="100%"> <div align="center"><H4 CLASS="AUTHOR"><A NAME="AEN5">Boudewijn Rempt</A><br><a href="../../https@secure.linuxports.com/opendocs/default.htm"><img src=odpyqt125.png></a><br>ISBN: 0-97003300-4-4<br><a href="../../https@secure.linuxports.com/opendocs/default.htm">Available from bookstores everywhere or you can order it here.</a><p>You can download the source files for the book <a href="pyqtsrc.tgz">(code / eps) here.</a><hr></div> <HTML><HEAD><TITLE>Toolbars</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="Actions: menus, toolbars and accelerators"HREF="c4807.htm"><LINKREL="PREVIOUS"TITLE="Menus"HREF="x4954.htm"><LINKREL="NEXT"TITLE="Keyboard accelerators"HREF="x5021.htm"></HEAD><BODYCLASS="SECT1"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"><A accesskey="P" href="index.lxp@lxpwrap=x4954_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 13. Actions: menus, toolbars and accelerators</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x5021_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Toolbars</A></H1><P>As you can see, once you have defined your set of <TTCLASS="CLASSNAME">QAction</TT>s, you have little to worry about concerning the definition of menus and toolbars. However, if you want other widgets beyond simple buttons in your toolbars, you cannot use <TTCLASS="CLASSNAME">QAction</TT> or <TTCLASS="CLASSNAME">QActionGroup</TT>. One popular addition to a toolbar is a combobox. Adding the combobox means quite a bit of work:</P><PRECLASS="PROGRAMLISTING"> self.fileToolbar.addSeparator() self.labelSelector=QLabel("Font: ", self.fileToolbar) self.comboSelector=QComboBox(self.fileToolbar) self.comboSelector.insertStrList(["Times","Arial","Cyberbit"], 1) self.comboSelector.setEditable(FALSE) self.connect(self.comboSelector, SIGNAL("activated(int)"), self.slotFontChanged) QWhatsThis.add(self.comboSelector,"""Font SelectionSelect the font that expresses yourpersonality best.""") </PRE><P>First, we give the widget a bit more room by adding a separator. Then we need a label, so people will know what the contents of the combobox represent. Of course, a combobox must be populated with relevant items (font names in this case). We ensure that people can't add items to the list, nor change the names of existing items, by setting <TTCLASS="VARNAME">editable</TT> to false. Finally, the activated signal is connected to a likely slot and a ‘what's this' text is added. The result looks like this:</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>A toolbar with a combobox added.</P></DIV></P></DIV><P>That ‘what's this' text leads us to a special button offered by PyQt. This is the little arrow-plus-question-mark that activates the ‘What's This' mode. When the user selects this button, he or she can click on any interface element. A yellow note with a bit of explanation is then shown. This is particularly welcome when your toolbar icons are non-intuitive.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Help with an unhelpful icon</P></DIV></P></DIV><P>Achieving this demands two steps: actually setting the help text in the <TTCLASS="CLASSNAME">QAction</TT>, and adding the appropriate button to the toolbar:</P><PRECLASS="PROGRAMLISTING"> QWhatsThis.whatsThisButton(self.fileToolbar) </PRE><P>If you want to embed a small picture of the icon in the yellow note, you use PyQt's <TTCLASS="CLASSNAME">QMimeSourceFactory</TT>. The trick is to first set the text to the widget, with an embedded <SPAN><ICLASS="EMPHASIS">img</I></SPAN> link pointing towards an identifier in the text. This identifier will be used to look up an image in the application-wide default mime source factory.</P><P>You can use source factories for every kind of data for which a mime-type exists, but they are most often used for simple pictures that have to be embedded in rich text. Rich text in Qt is text which is marked up with a subset of html. The <img> tags don't point to an URL, but to the contents of the mime source factory. </P><PRECLASS="PROGRAMLISTING">self.actions["fileQuit"].setWhatsThis( """<img source="filequit">Quit<br><br>By selecting quit you leave the application.If your document was changed, you will beasked to save it, so it's quite safe to do.""")QMimeSourceFactory.defaultFactory().setPixmap('filequit', QPixmap(filequit)) </PRE><P>You can add this code to the <TTCLASS="FUNCTION">initActions()</TT> method of the <TTCLASS="CLASSNAME">DocviewApp</TT> in the document-view framework. Mime source factories are very capable beasts. We can feed the factory just the <TTCLASS="CLASSNAME">QPixmap</TT> which is derived from the XPM data in the resources.</P></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=x4954_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=x5021_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Menus</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c4807_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Keyboard accelerators</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?