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

📄 designer-manual-2.html

📁 QT 下载资料仅供参考
💻 HTML
📖 第 1 页 / 共 4 页
字号:
            for ( ; i &lt; (int)clippingsListBox-&gt;count(); i++ ) {                if ( clippingsListBox-&gt;text( i ) == text ) {                    i = -1; // Do not add duplicates                    break;                }            }            if ( i != -1 )                clippingsListBox-&gt;insertItem( text, 0 );        }    }</pre> <p>If there is some text we change the LCD's value to the length of the text. We then iterate over all the items in the list box to see if we have the same text already. If the text is not already in the list box we insert it.</p><!-- index Signals and Slots --><p>To make the Add Clipping button functional we need to connect the button's<!-- index clicked() \SignalOrSlot clicked() signal to our --> <tt>addClipping()</tt> slot. Click the <b>Connect Signals/Slots</b> toolbar button. Click the Add Clipping button, drag to the form and release. (Make sure you drag to the form rather than another widget -- the form will have a thin pink border during the drag. If you make a mistake simply change the name in the Slots combobox.) The <em>Edit Connections</em> dialog will appear. Click the<!-- index clicked() \SignalOrSlot clicked() signal and our --> <tt>addClipping()</tt> slot. Click <b>OK</b> to confirm the connection.</p><p>The Copy Previous button is used to copy the selected clipping from the list box into the line edit. The clipping is also placed on the clipboard. The procedure is the same as for the Add Clipping button: first we create the slot, then we implement it and finally we connect to it:</p><ol type=1><li><p>Create the slot.</p><!-- index Signals and Slots --><p>Click the <b>Edit|Slots</b> menu item to invoke the <em>Edit Slots</em> dialog. Click <b>New Slot</b> and replace the default 'new_slot()' name with 'copyPrevious()'. Click <b>OK</b>.</p><li><p>Implement the slot.</p><pre>    void MulticlipForm::copyPrevious()    {        if ( clippingsListBox-&gt;currentItem() != -1 ) {            cb-&gt;setText( clippingsListBox-&gt;currentText() );            if ( cb-&gt;supportsSelection() ) {                cb-&gt;setSelectionMode( TRUE );                cb-&gt;setText( clippingsListBox-&gt;currentText() );                cb-&gt;setSelectionMode( FALSE );            }        }    }</pre> <p>The code for Copy Previous checks to see if there is a selected item in the list box. If there is the item is copied to the line edit. If we are using a system that supports selection we have to repeat the copy, this time with selection mode set. We don't explicitly update the clipboard. When the line edit's text is changed it emits a<!-- index dataChanged() --> <tt>dataChanged()</tt> signal which our<!-- index dataChanged() --> <tt>dataChanged()</tt> slot receives. Our<!-- index dataChanged() --> <tt>dataChanged()</tt> slot updates the clipboard.</p><li><!-- index Signals and Slots --><p>Connect to the slot.</p><p>Click the <b>Connect Signals/Slots</b> toolbar button. Click the Copy Previous button, drag to the form and release. The <em>Edit Connections</em> dialog will pop up. Click the<!-- index clicked() --> <tt>clicked()</tt> signal and the <tt>copyPrevious()</tt> slot. Click <b>OK</b>.</p></ol><p>We take the same approach to the Delete Clipping button.</p><ol type=1><li><!-- index Signals and Slots --><p>Click <b>Edit|Slots</b> to invoke the <em>Edit Slots</em> dialog. Click <b>New Slot</b> and replace the default name with 'deleteClipping()'. Click <b>OK</b>.</p><li><p>The Delete button must delete the current item in the list box and clear the line edit.</p><pre>    void MulticlipForm::deleteClipping()    {        clippingChanged( "" );        clippingsListBox-&gt;removeItem( clippingsListBox-&gt;currentItem() );    }</pre> <p>We call our own <tt>clippingChanged()</tt> slot with an empty string and use the list box's <tt>removeItem()</tt> function to remove the current item.</p><li><!-- index Signals and Slots --><p>Connect the Delete Clipping button's<!-- index clicked() --> <tt>clicked()</tt> signal to our <tt>deleteClipping()</tt> slot. (Press <b>F3</b> -- which is the same as clicking the <b>Connect Signals/Slots</b> toolbar button. Click the Delete Clipping button and drag to the form; release. The <em>Edit Connections</em> dialog will appear. Click the<!-- index clicked() --> <tt>clicked()</tt> signal and the <tt>deleteClipping()</tt> slot. Click <b>OK</b>.)</p></ol><h3><a name="7"></a>Compiling and Building an Application</h3><!-- index Compiling and Building Applications --><!-- index Makefiles --><!-- index Adding!Files to Projects --><!-- index Projects!Adding Files --><p>So far we have written about 99% of a Qt application entirely in <em>Qt Designer</em>. To make the application compile and run we must create a<!-- index main.cpp --> <tt>main.cpp</tt> file from which we can call our form.</p><!-- index Adding!Source Files to Project Files --><p>The simplest way to create a new source file is by clicking <b>File|New</b> to invoke the 'New File' dialog, then click 'C++ Source' or 'C++ Header' as appropriate, then click <b>OK</b>. A new empty source window will appear. Click <b>File|Save</b> to invoke the <em>Save As</em> dialog, enter 'main.cpp', then click <b>Save</b>.</p><p>Enter the following code in the <tt>main.cpp</tt> C++ editor window:</p><pre>    #include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;    #include "multiclip.h"    int main( int argc, char *argv[] )    {        <a href="qapplication.html">QApplication</a> app( argc, argv );        MulticlipForm clippingForm;        app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;clippingForm );        clippingForm.show();        return app.<a href="qapplication.html#exec">exec</a>();    }</pre> <p>The program creates a <b>QApplication</b> object and an instance of our MulticlipForm, sets the form to be the main widget and shows the form. The <tt>app.exec()</tt> call starts off the event loop.</p><p>Now start up a console (or xterm), change directory to the multiclip application and run <tt>qmake</tt>. A Makefile compatible with your system will be generated:</p><pre>qmake -o Makefile multiclip.pro</pre><p>You can now make the application, e.g. by running <tt>make</tt> or <tt>nmake</tt>. Try compililng and running multiclip. There are many improvement you could make and experimenting with both the layout and the code will help you learn more about Qt and <em>Qt Designer</em>.</p><p>This chapter has introduced you to creating multiplatform applications with <em>Qt Designer</em>. We've created a form, populated it with widgets and laid the widgets out neatly and scalably. We've used Qt's signals and slots mechanism to make the application functional and generated the Makefile. These techniques for adding widgets to a form and laying them out with the layout tools; and for creating, coding and connecting slots will be used time and again as you create applications with <em>Qt Designer</em>. The following chapters will present further examples and explore more techniques for using <em>Qt Designer</em>.</p><!-- eof --><p align="right">[<a href="designer-manual-1.html">Prev: Preface</a>] [<a href="designer-manual.html">Home</a>] [<a href="designer-manual-3.html">Next: Creating Main Windows with Actions, Toolbars and Menus</a>]</p><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright &copy; 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 + -