⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 designer-manual-5.html

📁 QT 下载资料仅供参考
💻 HTML
📖 第 1 页 / 共 3 页
字号:
    {        Q_OBJECT    public:        CreditForm( <a href="qwidget.html">QWidget</a>* parent = 0, const char* name = 0,                    bool modal = FALSE, WFlags fl = 0 );        ~CreditForm();    public slots:        void setAmount();    };</pre><!-- index Macros!Q_OBJECT --><!-- index Q_OBJECT --> <p>We've declared the slot, <tt>setAmount()</tt>, that we created in <em>Qt Designer</em>. The <tt>Q_OBJECT</tt> macro is included because it is essential for classes that use signals and slots.</p><p>The implementation in <tt>qt/tools/designer/examples/credit/creditform.cpp</tt> is simple:</p><pre>    #include &lt;<a href="qradiobutton-h.html">qradiobutton.h</a>&gt;    #include &lt;<a href="qspinbox-h.html">qspinbox.h</a>&gt;    #include "creditform.h"    CreditForm::CreditForm( <a href="qwidget.html">QWidget</a>* parent, const char* name,                            bool modal, WFlags fl )        : CreditFormBase( parent, name, modal, fl )    {        setAmount();    }    CreditForm::~CreditForm() { /* NOOP */ }    void CreditForm::setAmount()    {        if ( stdRadioButton-&gt;isChecked() )            amountSpinBox-&gt;setValue( amountSpinBox-&gt;maxValue() / 2 );        else if ( noneRadioButton-&gt;isChecked() )            amountSpinBox-&gt;setValue( amountSpinBox-&gt;minValue() );    }</pre> <p>We call <tt>setAmount()</tt> in the constructor to ensure that the correct amount is shown when the form starts based on whichever radio button we checked in <em>Qt Designer</em>. In <tt>setAmount()</tt> we set the amount if the standard or none radio button is checked. If the user has checked the special radio button they are free to change the amount themselves.</p><!-- index Makefiles --><!-- index qmake!HEADERS --><!-- index qmake!SOURCES --><p>To be able to test our subclass we change<!-- index main.cpp --> <tt>main.cpp</tt> to include <tt>creditform.h</tt> rather than <tt>creditformbase.h</tt> and change the instantiation of the creditForm object:</p><pre>    #include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;    #include "creditform.h"    int main( int argc, char *argv[] )    {        <a href="qapplication.html">QApplication</a> app( argc, argv );        CreditForm creditForm;        app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;creditForm );        creditForm.show();        return app.<a href="qapplication.html#exec">exec</a>();    }</pre> <p>If you created the <tt>creditform.h</tt> and <tt>creditform.cpp</tt> files in <em>Qt Designer</em>, they are already in the project file, but if you created them manually you must also update the project file by adding these two new lines at the end:</p><pre>HEADERS += creditform.hSOURCES += creditform.cpp</pre><p>To test the form rerun <tt>qmake</tt> to regenerate the Makefile, then make and run.</p><p>The subclassing example we've used is simple, but this reflects subclassing forms in Qt: it is easy to do.</p><a name="dynamicdialogs"></a><h3><a name="2"></a>Creating Dynamic Dialogs from .ui Files</h3><!-- index Dynamic Dialogs --><!-- index Dialogs!Dynamic --><!-- index Code Editing!Not in Dynamic Dialogs --><p>Qt programs are capable of loading <em>Qt Designer</em><!-- index .ui --> <tt>.ui</tt> files and instantiating the forms represented by the<!-- index .ui --> <tt>.ui</tt> files. Since the<!-- index .ui --> <tt>.ui</tt> file is not compiled it cannot include any C++ code, (e.g. slot implementations). In this section we will explain how to load a dynamic dialog and how to create a class that can be used to implement the dynamic dialog's custom slots.</p><p>We will use the credit form that we created in the subclassing section as our example form. We will start by simply instantiating and running the form and then we'll cover how to implement custom slots.</p><p>We'll create a<!-- index main.cpp --> <tt>main.cpp</tt> file to use as a test harness, and manually create a project file.</p><h4><a name="2-1"></a>Creating the Project File</h4><p>The project file <tt>qt/tools/designer/examples/receiver1/receiver.pro</tt> looks like this:</p><pre>TEMPLATE    = appCONFIG     += qt warn_on releaseTARGET      = receiverSOURCES    += main.cppunix:LIBS  += -lquiwin32:LIBS += $(QTDIR)/lib/qui.libFORMS      = mainform.uiLANGUAGE    = C++INCLUDEPATH += $(QTDIR)/tools/designer/uilib</pre><p>We do <em>not</em> include the <tt>creditformbase.ui</tt> file since this file will be read at runtime, as we'll see shortly. We must include the <tt>qresource</tt> library since the functionality we require is not part of the standard Qt library.</p><h4><a name="2-2"></a>Creating main.cpp</h4><p>The<!-- index main.cpp --> <tt>main.cpp</tt> is quite standard. It will invoke the form we're going to create in <em>Qt Designer</em> as its main form. This form will then load and execute the dynamic dialog.</p><pre>    #include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;    #include "mainform.h"    int main( int argc, char *argv[] )    {        <a href="qapplication.html">QApplication</a> app( argc, argv );        MainForm *mainForm = new MainForm;        app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( mainForm );        mainForm-&gt;show();        return app.<a href="qapplication.html#exec">exec</a>();    }</pre> <p>We create a new instance of our MainForm class, set it to be the main widget, show it and enter the event loop in the <tt>app.exec()</tt> call.</p><h4><a name="2-3"></a>Creating the Main Form</h4><h5><a name="2-3-1"></a>Designing the Form</h5><ol type=1><li><p>Open the<!-- index receiver.pro --> <tt>receiver.pro</tt> project file in <em>Qt Designer</em>. We'll create a dialog as our main window which we'll use to invoke the dynamic dialog. Press <b>Ctrl+N</b> to launch the <em>New File</em> dialog and click <b>OK</b> to get the default which is a dialog. Change the dialog's name to 'MainForm' and its caption to 'Main Form'. Add two buttons, one called 'creditPushButton' with the text '&amp;Credit Dialog', and the other called 'quitPushButton' with the text '&amp;Quit'. (For each button click the <b>Push Button</b> toolbar button, then click the form. Change the properties in the property window to those we've just described.)</p><li><p>We will now add a couple of labels so that we can show the settings the user chose in the dynamic dialog. Click the <b>Text Label</b> toolbar button, then click the form below the Credit Dialog button. Change the label's <em>text</em> to 'Credit Rating'. Add another text label below the Quit button. Change its <em>name</em> to 'ratingTextLabel' and its <em>text</em> to 'Unrated'.</p><li><p>We'll now lay out the widgets. Click the form then press <b>Ctrl+G</b> (lay out in a grid).</p><li><!-- index Signals and Slots!Connecting to Close a Dialog --><p>We'll now handle the signals and slots connections. Press <b>F3</b> (connect signals/slots). Click the Credit Dialog button, drag to the form and release. Click the<!-- index clicked() --> <tt>clicked()</tt> signal. We'll need to implement a custom slot. Click <b>Edit Slots</b> to invoke the <em>Edit Slots</em> dialog. Click <b>New Slot</b> and type in the Slot name 'creditDialog()'. Click <b>OK</b>. The new slot is now in the list of slots; click the <tt>creditDialog()</tt> slot to make the connection then click <b>OK</b>. Connect the Quit button's<!-- index clicked() --> <tt>clicked()</tt> signal to the dialog's<!-- index accept() --> <tt>accept()</tt> function. (Press <b>F3</b>. Click the Quit button and drag to the form; release. Click the<!-- index clicked() --> <tt>clicked()</tt> signal and the<!-- index accept() --> <tt>accept()</tt> slot, then click <b>OK</b>.)</p></ol><p>Save the form and call it <tt>mainform.ui</tt>. (Press <b>Ctrl+S</b> and enter the filename.) In the next section we'll write the code for loading and launching the dynamic dialog directly in <em>Qt Designer</em>.</p><h5><a name="2-3-2"></a>Loading and Executing a Dynamic Dialog</h5><!-- index Dynamic Dialogs!Loading and Executing --><!-- index Dialogs!Dynamic --><!-- index Forms!Forward declarations --><!-- index Forward declarations --><!-- index Includes --><p>We'll now add the code to invoke the credit dialog. Before we can do this we need to add the widget factory's header file to the form. Click the Source tab in the Object Hierarchy. Right click Included (in Implementation), then click <b>New</b>. Type in '<tt>&lt;qwidgetfactory.h&gt;</tt>', then press <b>Enter</b>. Because we will need to access the spin box in the dynamic dialog we must add its header file. Right click Included (in Implmentation), then click New. Type in '<tt>&lt;qspinbox.h&gt;</tt>', then press <b>Enter</b>.</p><p>In our main form we created a slot called <tt>creditDialog()</tt>. We will implement this slot directly in <em>Qt Designer</em> and use it to load and execute the dynamic dialog. The code is taken from <tt>qt/tools/designer/examples/receiver1/mainform.ui.h</tt> which contains the C++ implementation of <tt>mainform.ui</tt>'s slots.</p><pre>    void MainForm::creditDialog()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -