?? tutorial2-09.html
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-win-commercial-3.0.5/doc/tutorial2.doc:1236 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Translator" content="Cavendish">
<meta name="Qt zh_CN Documents Website" content="http://www.qiliang.net/qt">
<title>設置選項</title>
<style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; font-family: "Times New Roman" }
--></style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
<a href="index.html">
<font color="#004faf">主頁</font></a>
| <a href="classes.html">
<font color="#004faf">所有的類</font></a>
| <a href="mainclasses.html">
<font color="#004faf">主要的類</font></a>
| <a href="annotated.html">
<font color="#004faf">注釋的類</font></a>
| <a href="groups.html">
<font color="#004faf">分組的類</font></a>
| <a href="functions.html">
<font color="#004faf">函數</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table>
<h1 align=center>設置選項</h1>
<p>
<p> <center><img src="chart-options.png" alt="The options dialog"></center>
<p> 我們提供了一個選項對話框,這樣用戶就可以在一個地方對所有的數據組設置選項。
<p> (由<tt>optionsform.h</tt>展開。)
<p>
<pre> class OptionsForm : public <a href="qdialog.html">QDialog</a>
{
<a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
public:
OptionsForm( <a href="qwidget.html">QWidget</a>* parent = 0, const char* name = "options form",
bool modal = FALSE, WFlags f = 0 );
~OptionsForm() {}
<a href="qfont.html">QFont</a> font() const { return m_font; }
void setFont( <a href="qfont.html">QFont</a> font );
<a href="qlabel.html">QLabel</a> *chartTypeTextLabel;
<a href="qcombobox.html">QComboBox</a> *chartTypeComboBox;
<a href="qpushbutton.html">QPushButton</a> *fontPushButton;
<a href="qlabel.html">QLabel</a> *fontTextLabel;
<a href="qframe.html">QFrame</a> *addValuesFrame;
<a href="qbuttongroup.html">QButtonGroup</a> *addValuesButtonGroup;
<a href="qradiobutton.html">QRadioButton</a> *noRadioButton;
<a href="qradiobutton.html">QRadioButton</a> *yesRadioButton;
<a href="qradiobutton.html">QRadioButton</a> *asPercentageRadioButton;
<a href="qlabel.html">QLabel</a> *decimalPlacesTextLabel;
<a href="qspinbox.html">QSpinBox</a> *decimalPlacesSpinBox;
<a href="qpushbutton.html">QPushButton</a> *okPushButton;
<a href="qpushbutton.html">QPushButton</a> *cancelPushButton;
protected slots:
void chooseFont();
protected:
<a href="qvboxlayout.html">QVBoxLayout</a> *optionsFormLayout;
<a href="qhboxlayout.html">QHBoxLayout</a> *chartTypeLayout;
<a href="qhboxlayout.html">QHBoxLayout</a> *fontLayout;
<a href="qvboxlayout.html">QVBoxLayout</a> *addValuesFrameLayout;
<a href="qvboxlayout.html">QVBoxLayout</a> *addValuesButtonGroupLayout;
<a href="qhboxlayout.html">QHBoxLayout</a> *decimalPlacesLayout;
<a href="qhboxlayout.html">QHBoxLayout</a> *buttonsLayout;
private:
<a href="qfont.html">QFont</a> m_font;
};
</pre>
<p> 這個對話框的布局比設置數據視窗要更復雜一些,但是我們只需要一個單一的槽。不像“聰明的”設置數據視窗那樣,這是一個“啞的”對話框,它只向窗口部件的調用者提供了讀和寫。調用者有責任基于用戶所作的改變更新事物。
<p> (由<tt>optionsform.cpp</tt>展開。)
<p>
<pre> #include "images/options_horizontalbarchart.xpm"
#include "images/options_piechart.xpm"
#include "images/options_verticalbarchart.xpm"
</pre>
<p> 我們包含了一些在圖表類型組合框中要使用的圖片。
<p> <h2> 構造函數
</h2>
<a name="1"></a><p> <pre> OptionsForm::OptionsForm( <a href="qwidget.html">QWidget</a>* parent, const char* name,
bool modal, WFlags f )
: <a href="qdialog.html">QDialog</a>( parent, name, modal, f )
{
<a href="qwidget.html#setCaption">setCaption</a>( "Chart -- Options" );
<a href="qwidget.html#resize">resize</a>( 320, 290 );
</pre>
<p> 我們把所有的參數傳遞給<a href="qdialog.html">QDialog</a>構造函數,設置一個題目并且設置一個初始大小。
<p> 視窗的布局將是一個包含圖表類型標簽和組合框的水平盒子布局,并且對于字體按鈕和字體標簽、小數點位置標簽和微調框也是相似的。按鈕也會被放在一個水平布局中,但是還會有一個間隔來把它們移到右邊。顯示值的單選按鈕將會豎直地排列在一個框架中。所有地這些都被放在一個豎直盒子布局中。
<p> <pre> optionsFormLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>( this, 11, 6 );
</pre>
<p> 所有的窗口部件都被放在視窗的豎直盒子布局中。
<p> <pre> chartTypeLayout = new <a href="qhboxlayout.html">QHBoxLayout</a>( 0, 0, 6 );
</pre>
<p> 圖表類型標簽和組合框將被并排放置。
<p> <pre> chartTypeTextLabel = new <a href="qlabel.html">QLabel</a>( "&Chart Type", this );
<a name="x2481"></a> chartTypeLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( chartTypeTextLabel );
chartTypeComboBox = new <a href="qcombobox.html">QComboBox</a>( false, this );
<a name="x2483"></a> chartTypeComboBox-><a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_piechart ), "Pie Chart" );
chartTypeComboBox-><a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_verticalbarchart ),
"Vertical Bar Chart" );
chartTypeComboBox-><a href="qcombobox.html#insertItem">insertItem</a>( QPixmap( options_horizontalbarchart ),
"Horizontal Bar Chart" );
chartTypeLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( chartTypeComboBox );
<a name="x2480"></a> optionsFormLayout-><a href="qboxlayout.html#addLayout">addLayout</a>( chartTypeLayout );
</pre>
<p> 我們創建圖表類型標簽(帶有一個加速鍵,稍后我們會把它和圖表類型組合框聯系起來)。我們也創建一個圖表類型組合框,用圖片和文本來填充它。我們把它們兩個添加到水平布局中,并把水平布局添加到視窗的豎直布局中。
<p> <pre> fontLayout = new <a href="qhboxlayout.html">QHBoxLayout</a>( 0, 0, 6 );
fontPushButton = new <a href="qpushbutton.html">QPushButton</a>( "&Font...", this );
fontLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( fontPushButton );
<a href="qspaceritem.html">QSpacerItem</a>* spacer = new <a href="qspaceritem.html">QSpacerItem</a>( 0, 0,
QSizePolicy::Expanding,
QSizePolicy::Minimum );
<a name="x2479"></a> fontLayout-><a href="qboxlayout.html#addItem">addItem</a>( spacer );
fontTextLabel = new <a href="qlabel.html">QLabel</a>( this ); // 必須由調用者通過setFont()來設置
fontLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( fontTextLabel );
optionsFormLayout-><a href="qboxlayout.html#addLayout">addLayout</a>( fontLayout );
</pre>
<p> 我們創建一個水平盒子布局用來保存字體按鈕和字體標簽。字體按鈕是被直接加入的。我們添加了一個間隔用來增加效果。字體文本標簽被初始化為空(因為我們不知道用戶正在使用什么字體)。
<p> <pre> addValuesFrame = new <a href="qframe.html">QFrame</a>( this );
<a name="x2490"></a> addValuesFrame-><a href="qframe.html#setFrameShape">setFrameShape</a>( QFrame::StyledPanel );
<a name="x2489"></a> addValuesFrame-><a href="qframe.html#setFrameShadow">setFrameShadow</a>( QFrame::Sunken );
addValuesFrameLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>( addValuesFrame, 11, 6 );
addValuesButtonGroup = new <a href="qbuttongroup.html">QButtonGroup</a>( "Show Values", addValuesFrame );
<a name="x2491"></a> addValuesButtonGroup-><a href="qgroupbox.html#setColumnLayout">setColumnLayout</a>(0, Qt::Vertical );
<a name="x2497"></a> addValuesButtonGroup-><a href="qwidget.html#layout">layout</a>()->setSpacing( 6 );
addValuesButtonGroup-><a href="qwidget.html#layout">layout</a>()->setMargin( 11 );
addValuesButtonGroupLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>(
addValuesButtonGroup-><a href="qwidget.html#layout">layout</a>() );
<a name="x2494"></a> addValuesButtonGroupLayout-><a href="qlayoutitem.html#setAlignment">setAlignment</a>( Qt::AlignTop );
noRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "&No", addValuesButtonGroup );
<a name="x2495"></a> noRadioButton-><a href="qradiobutton.html#setChecked">setChecked</a>( true );
addValuesButtonGroupLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( noRadioButton );
yesRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "&Yes", addValuesButtonGroup );
addValuesButtonGroupLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( yesRadioButton );
asPercentageRadioButton = new <a href="qradiobutton.html">QRadioButton</a>( "As &Percentage",
addValuesButtonGroup );
addValuesButtonGroupLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( asPercentageRadioButton );
addValuesFrameLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( addValuesButtonGroup );
</pre>
<p> 用戶也許選擇顯示它們自己的標簽或者在每一個標簽的末尾加上值,或者加上百分比。
<p> 我們創建一個框架來存放單選按鈕并且為它們創建了一個布局。我們創建了一個按鈕組(這樣Qt就可以自動地處理專有的單選按鈕行為了)。接下來我們創建單選按鈕,并把“No”作為默認值。
<p> 小數位標簽和微調框被放在另一個水平布局中,并且按鈕和設置數據視窗中的按鈕的排布方式非常相似。
<p> <pre> <a href="qobject.html#connect">connect</a>( fontPushButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( chooseFont() ) );
<a href="qobject.html#connect">connect</a>( okPushButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( <a href="qdialog.html#accept">accept</a>() ) );
<a href="qobject.html#connect">connect</a>( cancelPushButton, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( <a href="qdialog.html#reject">reject</a>() ) );
</pre>
<p> 我們只需要三個連接:
<ol type=1>
<li> 當用戶點擊字體按鈕時,我們執行我們自己的chooseFont()槽。
<li> 如果用戶點擊OK,我們調用<a href="qdialog.html#accept">QDialog::accept</a>(),它會讓調用者來從對話框的窗口部件中讀取數據并且執行任何必要的動作。
<li> 如果用戶點擊Cancel,我們調用<a href="qdialog.html#reject">QDialog::reject</a>()。
</ol>
<p> <pre> <a name="x2492"></a> chartTypeTextLabel-><a href="qlabel.html#setBuddy">setBuddy</a>( chartTypeComboBox );
decimalPlacesTextLabel-><a href="qlabel.html#setBuddy">setBuddy</a>( decimalPlacesSpinBox );
</pre>
<p> 我們使用setBuddy()函數來連接窗口部件和標簽的加速鍵。
<p> <h2> 槽
</h2>
<a name="2"></a><p> <pre> void OptionsForm::chooseFont()
{
bool ok;
<a name="x2488"></a> <a href="qfont.html">QFont</a> font = QFontDialog::<a href="qfontdialog.html#getFont">getFont</a>( &ok, m_font, this );
if ( ok )
<a href="qwidget.html#setFont">setFont</a>( font );
}
</pre>
<p> 當用戶點擊Font按鈕時,這個槽被調用。它簡單地調用靜態的<a href="qfontdialog.html#getFont">QFontDialog::getFont</a>()來獲得用戶選擇的字體。如果他們選擇了一個字體,我們調用我們的setFont()槽在字體標簽中提供一個字體的文本描述。
<p> <pre> void OptionsForm::<a href="qwidget.html#setFont">setFont</a>( <a href="qfont.html">QFont</a> font )
{
<a name="x2485"></a> <a href="qstring.html">QString</a> label = font.<a href="qfont.html#family">family</a>() + " " +
<a name="x2487"></a> QString::<a href="qstring.html#number">number</a>( font.<a href="qfont.html#pointSize">pointSize</a>() ) + "pt";
<a name="x2484"></a> if ( font.<a href="qfont.html#bold">bold</a>() )
label += " Bold";
<a name="x2486"></a> if ( font.<a href="qfont.html#italic">italic</a>() )
label += " Italic";
fontTextLabel-><a href="qlabel.html#setText">setText</a>( label );
m_font = font;
}
</pre>
<p> 這個函數在字體標簽中顯示一個被選字體的文本描述,并且在<tt>m_font</tt>成員中保存一個字體的拷貝。我們需要這個字體為成員,這樣我們就會為chooseFont()提供一個默認字體。
<p> <p align=right>
<a href="tutorial2-08.html">« Taking Data</a> |
<a href="tutorial2.html">目錄</a> |
<a href="tutorial2-10.html">項目文件 »</a>
</p>
<p>
<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright © 2002
<a href="http://www.trolltech.com">Trolltech</a>
<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td><a href="zh_CN.html">譯者:Cavendish</a>
<td align=right><div align=right>Qt 3.0.5版</div>
</table></div></address></body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -