📄 designer-manual-5.html
字号:
{ <a href="qdialog.html">QDialog</a> *creditForm = (QDialog *) QWidgetFactory::<a href="qwidgetfactory.html#create">create</a>( "../credit/creditformbase.ui" ); // Set up the dynamic dialog here if ( creditForm-><a href="qdialog.html#exec">exec</a>() ) { // The user accepted, act accordingly <a href="qspinbox.html">QSpinBox</a> *amount = (QSpinBox *) creditForm-><a href="qobject.html#child">child</a>( "amountSpinBox", "QSpinBox" ); if ( amount ) ratingTextLabel->setText( amount-><a href="qspinbox.html#text">text</a>() ); } delete creditForm; }</pre> <p>The<!-- index create() --> <tt>create()</tt> function is a static <b>QWidgetFactory</b> function. It loads the specified<!-- index .ui --> <tt>.ui</tt> file and returns a pointer to the toplevel <b>QWidget</b> created from the<!-- index .ui --> <tt>.ui</tt> file. We have cast the pointer to <b>QDialog</b> since we know that the <tt>creditformbase.ui</tt> file defines a <b>QDialog</b>. After creating the dialog we<!-- index exec() --> <tt>exec()</tt> it. If the user clicked <b>OK</b> the dialog returns Accepted and we enter the body of the <tt>if</tt> statement. We want to know the amount of credit that the user selected. We call the<!-- index child() --> <tt>child()</tt> function on the dialog passing it the name of the widget we're interested in. The<!-- index child() --> <tt>child()</tt> function returns a pointer to the widget with the name we passed, or returns 0 if no widget of that name was found. In the example we call<!-- index child() --> <tt>child()</tt> to get a pointer to the 'amountSpinBox'. If the pointer we get back is not 0 we set the rating text to the amount in the dialog's spin box. At the end we delete the dynamic dialog. Deleting the dialog ensures that we free up its resources as soon as it is no longer required.</p><p>We used the<!-- index child() --> <tt>child()</tt> to gain access to a widget within the dynamic dialog, passing it the name of the widget we were interested in. In some situations we might not know what a widget is called. We can access the first widget of a specified class by calling<!-- index child() --> <tt>child()</tt> with a null widget name and a classname, e.g. <tt>child(0,"QPushButton")</tt>. This will return a pointer to the first <b>QPushButton</b> it finds (or 0 if there isn't one). If you want pointers to all the widgets of a given class you can call the<!-- index QObject::queryList() --> <tt>QObject::queryList()</tt> function, passing it the name of the class. It returns a <b>QObjectList</b> pointer which points to every object in the dialog that is derived from the given class. See the online <a href="http://doc.trolltech.com/qobject.html">QObject</a> documentation for further details.</p><h5><a name="2-3-3"></a>Implementing Slots for Dynamic Dialogs</h5><!-- index Signals and Slots!Dynamic Dialogs --><!-- index Dynamic Dialogs --><p>There is one outstanding issue that we haven't addressed: the dynamic dialog does not have the behaviour of the original credit dialog because we have not implemented the <tt>setAmount()</tt> slot. We can implement slots for dynamic dialogs by creating a <b>QObject</b> subclass. We then create an instance of this subclass and pass a pointer to it to the<!-- index QWidgetFactory::create() --> <tt>QWidgetFactory::create()</tt> function which will connect the dynamic dialog's signals to the slots implemented in our subclass.</p><p>We need to create a <b>QObject</b> subclass and change our <tt>creditDialog()</tt> to create an instance of our subclass that can be passed to the<!-- index QWidgetFactory::create() --> <tt>QWidgetFactory::create()</tt> function. Here is the modified <tt>creditDialog()</tt> function from the <tt>qt/tools/designer/examples/receiver2/mainform.ui.h</tt> file that contains the code for <tt>mainform.ui</tt>'s slots:</p><pre> void MainForm::creditDialog() { Receiver *receiver = new Receiver; <a href="qdialog.html">QDialog</a> *creditForm = (QDialog *) QWidgetFactory::<a href="qwidgetfactory.html#create">create</a>( "../credit/creditformbase.ui", receiver ); receiver->setParent( creditForm ); // Set up the dynamic dialog here if ( creditForm-><a href="qdialog.html#exec">exec</a>() ) { // The user accepted, act accordingly <a href="qspinbox.html">QSpinBox</a> *amount = (QSpinBox *) creditForm-><a href="qobject.html#child">child</a>( "amountSpinBox", "QSpinBox" ); if ( amount ) ratingTextLabel->setText( amount-><a href="qspinbox.html#text">text</a>() ); } delete receiver; delete creditForm; }</pre> <p>We create a new instance of our 'Receiver' subclass. (We'll write the code for this class shortly.) We then create the <b>QDialog</b> using<!-- index QWidgetFactory::create() --> <tt>QWidgetFactory::create()</tt>. This call differs from our previous example because we pass in the subclass object so that the<!-- index create() --> <tt>create()</tt> function can set up the signals/slots connections automatically for us. Since our slot must access the widgets in the dynamic form we pass a pointer to the form to the receiver object through our <tt>setParent()</tt> function. The remainder of the function is the same as before except that we delete our receiver object.</p><!-- index Dynamic Dialogs --><p>We'll now look at the implementation of our 'Receiver' subclass. The code is taken from <tt>qt/tools/designer/examples/receiver2/receiver.h</tt> and the corresponding <tt>receiver.cpp</tt> file. We'll start with the header file.</p><pre>#include <<a href="qobject-h.html">qobject.h</a>>#include <<a href="qdialog-h.html">qdialog.h</a>>class Receiver : public <a href="qobject.html">QObject</a>{ Q_OBJECTpublic: void setParent( <a href="qdialog.html">QDialog</a> *parent );public slots: void setAmount();private: <a href="qdialog.html">QDialog</a> *p;};</pre><!-- index Macros!Q_OBJECT --><!-- index Q_OBJECT!Macros --><p>Our class must be a <b>QObject</b> subclass and because we're using signals and slots it must include the <tt>Q_OBJECT</tt> macro. We declare a function and the <tt>setAmount()</tt> slot that we wish to implement as well as a private <b>QDialog</b> pointer.</p><p>We'll discuss the implementation of each function in <tt>receiver.cpp</tt> separately.</p><pre> void Receiver::setParent( <a href="qdialog.html">QDialog</a> *parent ) { p = parent; setAmount(); }</pre> <p>The <tt>setParent()</tt> function assigns a pointer to the dynamic dialog to our private pointer. We could not do this in a constructor call because we have to construct our Receiver object before we call<!-- index QWidgetFactory::create() --> <tt>QWidgetFactory::create()</tt>, since we must pass the Receiver object to the<!-- index create() --> <tt>create()</tt> function. Once we've called<!-- index create() --> <tt>create()</tt> we then have a pointer to the dynamic dialog which we can then pass via <tt>setParent()</tt> to our Receiver class. In the subclass version of this example we called <tt>setAmount()</tt> in the constructor; but we cannot do that here because the implementation of <tt>setAmount()</tt> depends on knowledge of the dynamic dialog which is not available at construction time. Because of this we call <tt>setAmount()</tt> in the <tt>setParent()</tt> function.</p><pre> void Receiver::setAmount() { <a href="qspinbox.html">QSpinBox</a> *amount = (QSpinBox *) p->child( "amountSpinBox", "QSpinBox" ); <a href="qradiobutton.html">QRadioButton</a> *radio = (QRadioButton *) p->child( "stdRadioButton", "QRadioButton" ); if ( radio && radio-><a href="qradiobutton.html#isChecked">isChecked</a>() ) { if ( amount ) amount-><a href="qspinbox.html#setValue">setValue</a>( amount-><a href="qspinbox.html#maxValue">maxValue</a>() / 2 ); return; } radio = (QRadioButton *) p->child( "noneRadioButton", "QRadioButton" ); if ( radio && radio-><a href="qradiobutton.html#isChecked">isChecked</a>() ) if ( amount ) amount-><a href="qspinbox.html#setValue">setValue</a>( amount-><a href="qspinbox.html#minValue">minValue</a>() ); }</pre> <p>Since we may be updating the amount spin box we need to get a pointer to it. We call<!-- index child() --> <tt>child()</tt> on the pointer <tt>p</tt> which points to the dynamic dialog assigned in the <tt>setParent()</tt> call. We cast the resulting pointer to the correct type so that we can call any functions relevant to that type. In the example we call<!-- index child() --> <tt>child()</tt> to get a pointer to the amount spin box, and then call<!-- index child() --> <tt>child()</tt> again to get a pointer to the 'stdRadioButton'. If we get a pointer to the radio button and the button is checked we set the amount providing we have a pointer to the amount spin box. If this radio button was checked we're finished so we return. If the 'stdRadioButton' isn't checked we get a pointer to the 'noneRadioButton' and set the amount if this button is checked. We do nothing if the 'specialRadioButton' is checked because the user is free to enter a value of their choice.</p><blockquote><p align="center"><b> Compiling vs Dynamically Loading Dialogs</b></p><!-- index Dynamic Dialogs!Compared with Compiling --><!-- index Dynamic Dialogs!Subclassing --><!-- index Subclassing!Dynamic Dialogs --><p>The differences between using a 'compiled in'<!-- index .ui --> <tt>.ui</tt> file and a dynamically loaded<!-- index .ui --> <tt>.ui</tt> file are these:</p><ul><li><p>Dynamic dialogs cannot have any C++ code in the<!-- index .ui --> <tt>.ui</tt> file; any custom slots must be implemented via a <b>QObject</b> subclass. Compiled dialogs can contain code either in the<!-- index .ui --> <tt>.ui</tt> file or in a subclass.</p><li><p>Dynamic dialogs will load slower because the<!-- index .ui --> <tt>.ui</tt> file must be read and a <b>QWidget</b> instance instantiated based on the<!-- index .ui --> <tt>.ui</tt> file's parse tree. Compiled code will load much faster because no file reading or parsing is necessary. Note that the user may not notice any difference in speed since the difference may be mere fractions of a second.</p><li><p>Dynamic dialogs allow you to change the<!-- index .ui --> <tt>.ui</tt> file independently of the code so long as none of the changes impact the code. This means that you can change the appearance of the form, e.g. move widgets and lay them out differently. If you want to change a compiled dialog you must change the<!-- index .ui --> <tt>.ui</tt> file and recompile. If you are building an application and want your customers to be able to customize aspects of the user interface you can give them a copy of <em>Qt Designer</em> and use dynamic dialogs.</p></ul></blockquote><!-- eof --><p align="right">[<a href="designer-manual-4.html">Prev: The Designer Approach</a>] [<a href="designer-manual.html">Home</a>] [<a href="designer-manual-6.html">Next: Creating Custom Widgets</a>]</p><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 align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -