index.lxp@lxpwrap=x2976_252ehtm.htm
来自「GUI Programming with Python」· HTM 代码 · 共 1,070 行 · 第 1/2 页
HTM
1,070 行
self.buttonGroup=QVButtonGroup("Choose your favourite", self) self.radio1=QRadioButton("&Blackadder I", self.buttonGroup) self.radio2=QRadioButton("B&lackadder II", self.buttonGroup) self.radio3=QRadioButton("Bl&ackadder III", self.buttonGroup) self.radio4=QRadioButton("Bla&ckadder Goes Forth", self.buttonGroup) self.radio1.setChecked(1) self.layout.addWidget(self.buttonGroup)if __name__ == '__main__': app = QApplication(sys.argv) QObject.connect(app, SIGNAL('lastWindowClosed()'), app, SLOT('quit()')) win = dlgRadio() app.setMainWidget(win) win.show() app.exec_loop() </PRE></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>radio.py</P></DIV></P></DIV></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QCheckBox</A></H2><P>Checkboxes are another of those gui features that makes one think of bureaucratic forms. Check any that apply... Checkboxes are as easy to use as radiobuttons, and come with a label attached, like radiobuttons. A variation which makes for instant user confusion is the tri-state checkbox, in which there's a <SPAN><ICLASS="EMPHASIS">checked</I></SPAN>, <SPAN><ICLASS="EMPHASIS">unchecked</I></SPAN> and a <SPAN><ICLASS="EMPHASIS">doesn't apply</I></SPAN> state — the doesn't apply state is usually rendered to look just like a completely disabled checkbox. However, it is sometimes necessary to introduce this behavior. Imagine a dialog box that allows the user to set a filter:</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>A dialog with a tri-state checkbox in the <SPAN><ICLASS="EMPHASIS">‘doesn't apply'</I></SPAN> state.</P></DIV></P></DIV><P>Now the user has three choices: either look for solvent persons, for insolvent persons or for both. The Platinum style makes it very clear which state the checkbox is in, compared to, for instance, the Windows style.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QListBox</A></H2><P>Listboxes are simple containers where a variable number of strings can be added. You can allow the user to select no item, one item, a range of items or a discontinuous set of items. You can enter texts or pixmaps in a listbox, but the listbox, like the listview and the combobox, doesn't let you associate arbitrary data with the items inside it.</P><P>This is something you may often want — you let the user select a certain object by clicking on an item in the listbox, and you want that object to be available in your application, not the string or picture that represents the object, or the index of the item in the listbox. You can achieve this by coding a small associative listbox:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 10-10. listbox.py - A listbox where data can be associated with an entry</B></P><PRECLASS="PROGRAMLISTING">## listbox.py## listbox with key-to-index and index-to-key mapping#import sysfrom qt import *class AssociativeListBox(QListBox): def __init__(self, *args): apply(QListBox.__init__,(self,)+args) self.text2key = {} self.key2text = {} self.connect(self, SIGNAL("selected(int)"), self.slotItemSelected) def insertItem(self, text, key): QListBox.insertItem(self, text) self.text2key [self.count() - 1] = key self.key2text [key]=self.count() - 1 def currentKey(self): return self.text2key[self.currentItem()] def setCurrentItem(self, key): if self.key2text.has_key(key): QListBox.setCurrentItem(self, self.key2text[key]) def slotItemSelected(self, index): key=self.currentKey() self.emit(PYSIGNAL("itemSelected"), (key, self.currentText()) ) def removeItem(self, index): del self.text2key[self.currentItem()] del self.key2text[index] QListView.removeItem(self, index)class MainWindow(QMainWindow): def __init__(self, *args): apply(QMainWindow.__init__, (self,) + args) self.listbox=AssociativeListBox(self) self.listbox.insertItem("Visible text 1", "key1") self.listbox.insertItem("Visible text 2", "key2") self.listbox.insertItem("Visible text 3", "key3") self.setCentralWidget(self.listbox) self.connect(self.listbox,PYSIGNAL( "itemSelected"), self.printSelection) def printSelection(self, key, text): print "Associated with", key, "is", textdef main(args): app=QApplication(args) win=MainWindow() win.show() app.connect(app, SIGNAL("lastWindowClosed()") , app , SLOT("quit()") ) app.exec_loop()if __name__=="__main__": main(sys.argv) </PRE></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>listbox.py</P></DIV></P></DIV><P>Of course, the same trick is needed to get something useful out of a <TTCLASS="CLASSNAME">QComboBox</TT> or a <TTCLASS="CLASSNAME">QListView</TT>.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QComboBox</A></H2><P>A <TTCLASS="CLASSNAME">QComboBox</TT> offers almost the same functionality as a <TTCLASS="CLASSNAME">QListBox</TT>, but folds out and in, preserving screen space. The contents of a combobox can be read-only or editable, and you can check the correctness of user input using a <TTCLASS="CLASSNAME">QValdidator</TT> object.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QLineEdit</A></H2><P>This is a simple one-line edit control, familiar to gui users everywhere. It supports copy, cut and paste and redo, too. There's a special mode for password boxes. </P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QMultiLineEdit</A></H2><P><TTCLASS="CLASSNAME">QMultiLineEdit</TT> provides a very simple multi-line editor. This class does not, in any way, support rich text — all text is in the same font, same size and same color. You can enable word-wrap, but that's about it. There are no limits on the amount of text it can handle (as was the case with the old Windows edit control, which had a limit of about 32 kb), but with megabytes of data, it will become decidedly slow.</P><P>With Qt3 this class has become obsolete. You're supposed to use the new, advanced <TTCLASS="CLASSNAME">QTextEdit</TT>, instead. After creating a <TTCLASS="CLASSNAME">QTextEdit</TT> object, you'd set the textformat to plain with <TTCLASS="FUNCTION">QTextEdit.setFormat(Qt.PlainText)</TT>; no user would notice the difference. <TTCLASS="CLASSNAME">QTextEdit</TT> and <TTCLASS="CLASSNAME">QMultiLineEdit</TT> are quite different to the programmer, though, and you can still use <TTCLASS="CLASSNAME">QMultiLineEdit</TT> if you need compatibility with older versions of Qt.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QPopupMenu</A></H2><P>One of the most useful things you can offer a user in any document-based application is a context menu — press the mouse-button anywhere in the document and a list of useful options pop up. PyQt's <TTCLASS="CLASSNAME">QPopupMenu</TT> can be used both as a stand-alone popup menu, and within a menu bar. Menu items can have a shortcut key associated with them, an accelerator and a small icon. The most useful way of adding items to a menu is by defining a <TTCLASS="CLASSNAME">QAction</TT>. You can nest menus, and make ‘tear-off' menus, where the user can click on a ‘tear-off handle' which puts the menu in a window of its own.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QProgressBar</A></H2><P><TTCLASS="CLASSNAME">QProgressBar</TT> gives you a horizontal progressbar — it's quite simple, even though it can be set to use one of several different styles. There's also <TTCLASS="CLASSNAME">QProgressDialog</TT> which can be used for lengthy actions that completely block access to the application. Since PyQt doesn't really support multi-threading, it's probably best to stick with the blocking dialog.</P><P>If you want to get fancy, you can, with a bit of juggling, get an approximation to threading by using a <TTCLASS="CLASSNAME">QTimer</TT>. Then it's best to place the progress bar in the statusbar of your application, instead of a separate non-modal progress dialog. <A href="index.lxp@lxpwrap=c7391_252ehtm.htm#CH16PAINTINGEXAMPLE">the Section called <I>A painting example</I> in Chapter 21</A> gives an example of the use of a timer.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QSlider and other small fry</A></H2><P>There are several other, simple user-interface widgets: <TTCLASS="CLASSNAME">QDial</TT>, <TTCLASS="CLASSNAME">QLCDNumber</TT>, <TTCLASS="CLASSNAME">QScrollBar</TT>, <TTCLASS="CLASSNAME">QSizeGrip</TT>, <TTCLASS="CLASSNAME">QSpinBox</TT> and <TTCLASS="CLASSNAME">QToolButton</TT>. These widgets are seldom used, mostly because they are rather overspecialized.</P><P><TTCLASS="CLASSNAME">QDial</TT> is a potentio-meter like knob. Twiddling it demands a fair proficiency with the mouse, and the keyboard interface isn't immediately obvious. See <A href="index.lxp@lxpwrap=x1408_252ehtm.htm#CH6-EXAMPLE-QDIAL">Example 7-5</A> for an example of using <TTCLASS="CLASSNAME">QDial</TT>.</P><P><TTCLASS="CLASSNAME">QLCDNumber</TT> is a kind of label which can display numbers in an lcd-like format. It's mostly interesting for historical reasons — the first version was written for the Sinclair ZX-Spectrum, a 1.9 MHz Z80 computer with a rubber keyboard and 48 Kb of ram.</P><P><TTCLASS="CLASSNAME">QScrollBar</TT> looks to be quite useful, because, on the face of it, any gui application is full of scrollbars. But those scrollbars come automatically with the edit-controls, listboxes and other scrollable widgets, and the <TTCLASS="CLASSNAME">QScrollBar</TT> is seldom used in isolation, and then mostly for the same purpose as <TTCLASS="CLASSNAME">QDial</TT>— as a range control. If you want to use it for scrolling a section on screen, use the <TTCLASS="CLASSNAME">QScrollView</TT> class instead. </P><P>The <TTCLASS="CLASSNAME">QSizeGrip</TT> is <SPAN><ICLASS="EMPHASIS">extremely</I></SPAN> obscure, being at its peak form only in statusbars of resizable windows. And those <TTCLASS="CLASSNAME">QStatusBar</TT>s can take care of their sizegrips themselves.</P><P><TTCLASS="CLASSNAME">QSpinBox</TT> is another range control. It's often used to let the user select a size for a font — she can either type the size directly, or use the little arrows to choose a larger or smaller size. Spinboxes are often quite fiddly to use.</P><P><TTCLASS="CLASSNAME">QToolButton</TT> is a special button that carries more often a picture than a text — it's mostly used in toolbars, where you can add buttons without explicitly creating instances of this class.</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=x2765_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=x3270_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Widget foundations: QWidget</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c2591_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Advanced widgets</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?