index.lxp@lxpwrap=c6351_252ehtm.htm
来自「GUI Programming with Python」· HTM 代码 · 共 1,439 行 · 第 1/3 页
HTM
1,439 行
><DIVCLASS="SECT2"><H2CLASS="SECT2">Calling the settings dialog window</A></H2><P>In order to be able to call the dialog window, we must first create a new <TTCLASS="CLASSNAME">QAction</TT> and add it to a likely menu. This is done in <TTCLASS="CLASSNAME">KalamApp</TT>: </P><PRECLASS="PROGRAMLISTING"># kalamapp.py def initActions(self): self.actions = {} ... # # Settings actions # self.actions["settingsSettings"] = QAction("Settings", "&Settings", QAccel.stringToKey(""), self) self.connect(self.actions["settingsSettings"], SIGNAL("activated()"), self.slotSettingsSettings) ... def initMenuBar(self): ... self.settingsMenu = QPopupMenu() self.actions["settingsSettings"].addTo(self.settingsMenu) self.menuBar().insertItem("&Settings", self.settingsMenu) ... </PRE><P>The <TTCLASS="VARNAME">settingsSettings</TT> is connected to a new slot in <TTCLASS="CLASSNAME">KalamApp</TT>:</P><PRECLASS="PROGRAMLISTING"> # Settings slots def slotSettingsSettings(self): dlg = DlgSettings(self, "Settings", TRUE, Qt.WStyle_Dialog) </PRE><P>The dialog window is constructed as a function-local variable. That means that if the function reaches its end, the <TTCLASS="VARNAME">dlg</TT> object is deleted. A settings dialog is typically modal. Whether a dialog is created modal or non-modal is determined in the constructor. The first argument to <TTCLASS="FUNCTION">DlgSettings.__init__()</TT> is the parent window, in this case <TTCLASS="CLASSNAME">KalamApp</TT>. The second argument is a name. The third argument determines whether the dialog is modal—TRUE means modal, FALSE means non-modal. FALSE is also the default. The last argument can be any combination of widget flags. For a dialog box, <TTCLASS="VARNAME">Qt.WStyle_Dialog</TT> seems rather appropriate. Note that in Qt 3, this flag is renamed to <TTCLASS="VARNAME">Qt.WType_Dialog</TT> There are a whole lot of flags (the following list is based on Qt 2 - there have been some changes):</P><P></P><UL><LI><P>WType_TopLevel - a toplevel window</P></LI><LI><P>WType_Modal - Makes the widget modal and inplies WStyle_Dialog. </P></LI><LI><P>WType_Popup - this widget is a popup top-level window, it is modal, but has a window system frame appropriate for popup menus. </P></LI><LI><P>WType_Desktop - This widget is the desktop - you can actually use PyQt to paint on you desktop.</P></LI><LI><P>WStyle_NormalBorder - The window has a normal border.</P></LI><LI><P>WStyle_DialogBorder - A thin dialog (if you windowmanager on X11 supports that).</P></LI><LI><P>WStyle_NoBorder - gives a borderless window. However, it is better to use WStyle_NoBorderEx instead, because this flag will make the window completely unusable on X11. </P></LI><LI><P>WStyle_NoBorderEx - gives a borderless window.</P></LI><LI><P>WStyle_Title - The window jas a title bar. </P></LI><LI><P>WStyle_SysMenu - adds a window system menu. </P></LI><LI><P>WStyle_Minimize - adds a minimize button. On Windows this must be combined with WStyle_SysMenu to work. </P></LI><LI><P>WStyle_Maximize - adds a maximize button. See WStyle_Minimize. </P></LI><LI><P>WStyle_MinMax - is equal to WStyle_Minimize|WStyle_Maximize. On Windows this must be combined with WStyle_SysMenu to work. </P></LI><LI><P>WStyle_ContextHelp - adds a context help button to dialogs. </P></LI><LI><P>WStyle_Tool - A tool window is a small window that contains tools (for instance, drawing tools, or the step buttons of a debugger). The tool window will always be kept on top of its parent, if there is one.</P></LI><LI><P>WStyle_StaysOnTop - the window should stay on top of all other windows. </P></LI><LI><P>WStyle_Dialog - indicates that the window is a dialog window. The window will not get its own taskbar entry and be kept on top of its parent by the window system. This is the flag QDialog uses, and it is not necessary for us to explicitly pass it to <TTCLASS="CLASSNAME">DlgSettings</TT>. </P></LI><LI><P>WDestructiveClose - makes Qt delete this object when the object has accepted closeEvent(). Don't use this for dialog windows, or your application will crash.</P></LI><LI><P>WPaintDesktop - gives this widget paint events for the desktop. </P></LI><LI><P>WPaintUnclipped - makes all painters operating on this widget unclipped. Children of this widget, or other widgets in front of it, do not clip the area the painter can paint on. </P></LI><LI><P>WPaintClever - indicates that Qt should not try to optimize repainting for the widget, but instead pass the window system repaint events directly on to the widget.</P></LI><LI><P>WResizeNoErase - indicates that resizing the widget should not erase it. This allows smart-repainting to avoid flicker. </P></LI><LI><P>WMouseNoMask - indicates that even if the widget has a mask, it wants mouse events for its entire rectangle. </P></LI><LI><P>WNorthWestGravity - indicates that the widget contents are north-west aligned and static. On resize, such a widget will receive paint events only for the newly visible part of itself. </P></LI><LI><P>WRepaintNoErase - indicates that the widget paints all its pixels. Updating, scrolling and focus changes should therefore not erase the widget. This allows smart-repainting to avoid flicker. </P></LI><LI><P>WGroupLeader - makes this widget or window a group leader. Modality of secondary windows only affects windows within the same group. </P></LI></UL><P>You can combine these flags with the or (or |) operator. </P><P>Showing a modal dialog is a matter of simply calling <TTCLASS="FUNCTION">exec_loop()</TT>: </P><PRECLASS="PROGRAMLISTING"> dlg.exec_loop() if dlg.result() == QDialog.Accepted: kalamconfig.set("textfont", dlg.textFont) kalamconfig.set("workspace", str(dlg.cmbWindowView.currentText())) kalamconfig.set("style", str(dlg.cmbStyle.currentText())) kalamconfig.set("textbackground", dlg.textBackgroundColor) kalamconfig.set("textforeground", dlg.textForegroundColor) kalamconfig.set("mdibackground", dlg.MDIBackgroundColor) kalamconfig.set("wrapmode", dlg.cmbLineWrapping.currentItem()) kalamconfig.set("linewidth", int(str(dlg.spinLineWidth.text()))) kalamconfig.set("encoding", str(dlg.cmbEncoding.currentText())) kalamconfig.set("forcenewline", dlg.chkAddNewLine.isChecked()) </PRE><P>If the execution loop of a modal dialog terminates, the dialog object is not destroyed, and you can use the reference to the object to retrieve the contents of its widgets. By calling <TTCLASS="FUNCTION">result()</TT> on the dialog object you can determine whether the user pressed OK or Cancel.</P><P>In this example, if the user presses OK, all relevant settings in <TTCLASS="FILENAME">kalamconfig</TT> are updated. This causes <TTCLASS="FILENAME">kalamconfig</TT> to emit change signals that are caught by all relevant objects.</P><P>The <TTCLASS="VARNAME">workspace</TT> object is updated:</P><PRECLASS="PROGRAMLISTING"> def initWorkSpace(self): workspace = kalamconfig.get("workspace")(self) workspace.setBackgroundColor(kalamconfig.get("mdibackground")) self.connect(qApp, PYSIGNAL("sigmdibackgroundChanged"), workspace.setBackgroundColor) self.setCentralWidget(workspace) return workspace </PRE><P>All <TTCLASS="VARNAME">view</TT> objects are updated, too. Some of the changes can be directly connected to the editor widget, the font setting, while others need a bit of processing, like the wrap mode:</P><PRECLASS="PROGRAMLISTING"># kalamview.py - extract...import kalamconfig...class KalamView(QWidget): def __init__(self, parent, doc, *args): ... self.editor.setFont(kalamconfig.get("textfont")) self.setWordWrap(kalamconfig.get("wrapmode")) self.setBackgroundColor(kalamconfig.get("textbackground")) self.setTextColor(kalamconfig.get("textforeground")) ... self.connect(qApp, PYSIGNAL("siglinewidthChanged"), self.editor.setWrapColumnOrWidth) self.connect(qApp, PYSIGNAL("sigwrapmodeChanged"), self.setWordWrap) self.connect(qApp, PYSIGNAL("sigtextfontChanged"), self.editor.setFont) self.connect(qApp, PYSIGNAL("sigtextforegroundChanged"), self.setTextColor) self.connect(qApp, PYSIGNAL("sigtextbackgroundChanged"), self.setBackgroundColor) ... def setTextColor(self, qcolor): pl = self.editor.palette() pl.setColor(QColorGroup.Text, qcolor) self.editor.repaint(TRUE) def setBackgroundColor(self, qcolor): pl = self.editor.palette() pl.setColor(QColorGroup.Base, qcolor) self.editor.setBackgroundColor(qcolor) self.editor.repaint(TRUE) def setWordWrap(self, wrapmode): if wrapmode == 0: self.editor.setWordWrap(QMultiLineEdit.NoWrap) elif wrapmode == 1: self.editor.setWordWrap(QMultiLineEdit.WidgetWidth) else: self.editor.setWordWrap(QMultiLineEdit.FixedColumnWidth) self.editor.setWrapColumnOrWidth(kalamconfig.get("linewidth")) ... </PRE><P>Not all changes can be activated while the application is running. The workspace style is determined when the application is restarted. It is nice and courteous to inform the user so. The best place to do that is in <TTCLASS="FUNCTION">slotSettingsSettings()</TT>:</P><PRECLASS="PROGRAMLISTING"> def slotSettingsSettings(self): ... if dlg.result() == QDialog.Accepted: ... workspace = str(dlg.cmbWindowView.currentText()) if kalamconfig.Config.workspace <> workspace: kalamconfig.set("workspace", workspace) QMessageBox.information(self, "Kalam", "Changes to the interface style " + "will only be activated when you " + "restart the application.") ... </PRE></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"><A accesskey="P" href="index.lxp@lxpwrap=x6342_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=x6734_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Conclusion</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=p4627_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Non-modal: Search and replace</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?