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

📄 designer-manual-6.html

📁 QT 下载资料仅供参考
💻 HTML
📖 第 1 页 / 共 3 页
字号:
</pre><blockquote><p align="center"><em>From <tt>qt/tools/designer/examples/filechooser/plugin/plugin.h</tt></em></p></blockquote><h5><a name="2-2-2"></a>The QWidgetPlugin Functions</h5><!-- index plugin.cpp --><p>Create your own plugin <tt>.cpp</tt> file by copying our <tt>plugin.cpp</tt> file and changing all occurrences of 'CustomWidgetPlugin' to the name you wish to use for your widget plugin implementation. Most of the other changes are simply replacing the name of our custom control, 'FileChooser', with the name of your custom control. You may need to add extra <tt>else if</tt> clauses if you have more than one custom control in your plugin implementation.</p><p>We'll now look at the constructor.</p><pre>    CustomWidgetPlugin::CustomWidgetPlugin()    {    }</pre> <p>The constructor does not have to do anything. Simply copy ours with the class name you wish to use for your widget plugin implementation.</p><p>No destructor is necessary.</p><!-- index keys() --><p>The <tt>keys</tt> function.</p><pre>    QStringList CustomWidgetPlugin::<a href="qwidgetplugin.html#keys">keys</a>() const    {        <a href="qstringlist.html">QStringList</a> list;        list &lt;&lt; "FileChooser";        return list;    }</pre> <p>For each widget class that you want to wrap in the plugin implementation you should supply a key by which the class can be identified. This key <em>must</em> be your class's name, so in our example we add a single key, 'FileChooser'.</p><!-- index create() --><p>The <tt>create()</tt> function.</p><pre>    QWidget* CustomWidgetPlugin::<a href="qwidgetplugin.html#create">create</a>( const <a href="qstring.html">QString</a> &amp;key, QWidget* parent, const char* name )    {        if ( key == "FileChooser" )            return new FileChooser( parent, name );        return 0;    }</pre> <p>In this function we create an instance of the requested class and return a QWidget pointer to the newly created widget. Copy this function changing the class name and the feature name and create an instance of your widget just as we've done here. (See the <a href="http://doc.trolltech.com/plugins.html">Qt Plugin documentation</a> for more information.)</p><!-- index includeFile() --><p>The <tt>includeFile()</tt> function.</p><pre>    QString CustomWidgetPlugin::<a href="qwidgetplugin.html#includeFile">includeFile</a>( const <a href="qstring.html">QString</a>&amp; feature ) const    {        if ( feature == "FileChooser" )            return "filechooser.h";        return QString::null;    }</pre> <p>This function returns the name of the include file for the custom widget. Copy this function changing the class name, key and include filename to suit your own custom widget.</p><!-- index group() --><!-- index iconSet() --><!-- index includeFile() --><!-- index toolTip() --><!-- index whatsThis() --><p>The <tt>group()</tt>, <tt>iconSet()</tt>, <tt>toolTip()</tt> and <tt>whatsThis()</tt> functions.</p><pre>    QString CustomWidgetPlugin::<a href="qwidgetplugin.html#group">group</a>( const <a href="qstring.html">QString</a>&amp; feature ) const    {        if ( feature == "FileChooser" )            return "Input";        return QString::null;    }    QIconSet CustomWidgetPlugin::<a href="qwidgetplugin.html#iconSet">iconSet</a>( const <a href="qstring.html">QString</a>&amp; ) const    {        return QIconSet( QPixmap( filechooser_pixmap ) );    }    QString CustomWidgetPlugin::<a href="qwidgetplugin.html#includeFile">includeFile</a>( const <a href="qstring.html">QString</a>&amp; feature ) const    {        if ( feature == "FileChooser" )            return "filechooser.h";        return QString::null;    }    QString CustomWidgetPlugin::<a href="qwidgetplugin.html#toolTip">toolTip</a>( const <a href="qstring.html">QString</a>&amp; feature ) const    {        if ( feature == "FileChooser" )            return "File Chooser Widget";        return QString::null;    }    QString CustomWidgetPlugin::<a href="qwidgetplugin.html#whatsThis">whatsThis</a>( const <a href="qstring.html">QString</a>&amp; feature ) const    {        if ( feature == "FileChooser" )            return "A widget to choose a file or directory";        return QString::null;    }</pre> <p>We use the <tt>group()</tt> function to identify which <em>Qt Designer</em> toolbar group this custom widget should be part of. If we use a name that is not in use <em>Qt Designer</em> will create a new toolbar group with the given name. Copy this function, changing the class name, key and group name to suit your own widget plugin implementation.</p><p>The <tt>iconSet()</tt> function returns the pixmap to use in the toolbar to represent the custom widget. The <tt>toolTip()</tt> function returns the tooltip text and the <tt>whatsThis()</tt> function returns the Whats This text. Copy each of these functions changing the class name, key and the string you return to suit your own widget plugin implementation.</p><!-- index isContainer() --><p>The <tt>isContainer()</tt> function.</p><pre>    bool CustomWidgetPlugin::<a href="qwidgetplugin.html#isContainer">isContainer</a>( const <a href="qstring.html">QString</a>&amp; ) const    {        return FALSE;    }</pre> <p>Copy this function changing the class name to suit your widget plugin implementation. It should return <tt>TRUE</tt> if your custom widget can contain other widgets, e.g. like <b>QFrame</b>, or <tt>FALSE</tt> if it must not contain other widgets, e.g. like <b>QPushButton</b>.</p><!-- index Macros!Q_EXPORT_PLUGIN --><!-- index Q_EXPORT_PLUGIN --><p>The <tt>Q_EXPORT_PLUGIN</tt> macro.</p><pre>    Q_EXPORT_PLUGIN( CustomWidgetPlugin )</pre> <p>This macro identifies the module as a plugin -- all the other code simply implements the relevant interface, i.e. wraps the classes you wish to make available.</p><p>This macro must appear once in your plugin. It should be copied with the class name changed to the name of your plugin's class. (See the <a href="http://doc.trolltech.com/plugins.html">Qt Plugin documentation</a> for more information on the plugin entry point.)</p><p>Each widget you wrap in a widget plugin implementation becomes a class that the plugin implementation offers. There is no limit to the number of classes that you may include in an plugin implementation.</p><h5><a name="2-2-3"></a>The Project File</h5><p>The project file for a plugin is somewhat different from an application's project file but in most cases you can use our project file changing only the <tt>HEADERS</tt> and <tt>SOURCES</tt> lines.</p><pre>SOURCES  += plugin.cpp ../widget/filechooser.cppHEADERS  += plugin.h ../widget/filechooser.hDESTDIR   = ../../../../../plugins/designerTARGET    = filechoosertarget.path=$$plugins.pathisEmpty(target.path):target.path=$$QT_PREFIX/pluginsINSTALLS    += targetTEMPLATE     = libCONFIG      += qt warn_on release pluginINCLUDEPATH += $(QTDIR)/tools/designer/interfacesDBFILE       = plugin.dbLANGUAGE     = C++</pre><blockquote><p align="center"><em><tt>qt/tools/designer/examples/filechooser/plugin/plugin.pro</tt></em></p></blockquote><p>Change the <tt>HEADERS</tt> line to list your plugin's header file plus a header file for each of your widgets. Make the equivalent change for the <tt>SOURCES</tt> line. If you create a Makefile with <tt>qmake</tt> and make the project the plugin will be created and placed in a directory where <em>Qt Designer</em> can find it. The next time you run <em>Qt Designer</em> it will detect your new plugin and load it automatically, displaying its icon in the toolbar you specified.</p><h5><a name="2-2-4"></a>Using the Widget Plugin</h5><p>Once the plugin has been compiled it will automatically be found and loaded by <em>Qt Designer</em> the next time <em>Qt Designer</em> is run. Use your custom widget just like any other.</p><p>If you want to use the plugin in another of your projects you can link against it by adding an appropriate line to the project, e.g. by adding a line like this to the project's <tt>.pro</tt> file:</p><pre>LIBS += filechooser.lib</pre><p>When you want to distribute your application, include the compiled plugin with the executable. Install the plugin in <tt>$QTDIR/plugins/widgets</tt>. If you don't want to use the standard plugin path, have your installation process determine the path you want to use for the plugin, and save the path, e.g. using QSettings, for the application to read when it runs. The application can then call QApplication::addLibraryPath() with this path and your plugins will be available to the application. Note that the final part of the path, i.e. <tt>styles</tt>, <tt>widgets</tt>, etc. cannot be changed.</p><blockquote><p align="center"><b> Plugins and Threaded Applications</b></p><p>If you want to build a plugin which you want to use with a threaded Qt library (whether or not the plugin itself uses threads) you must use a threaded environment. Specifically, you must use a threaded Qt library, and you must build <em>Qt Designer</em> with that library. Your <tt>.pro</tt> file for your plugin must include the line:</p><pre>    CONFIG += thread</pre><p><b>Do not</b> mix the normal Qt library and the threaded Qt library in an application. If your application uses the threaded Qt library, you should not link with the normal Qt library. Nor should you dynamically load the normal Qt library or dynamically load another library, e.g. a plugin, that depends on the normal Qt library. On some systems, mixing threaded and non-threaded libraries or plugins will corrupt the static data used in the Qt library.</p></blockquote><!-- eof --><p align="right">[<a href="designer-manual-5.html">Prev: Subclassing and Dynamic Dialogs</a>] [<a href="designer-manual.html">Home</a>] [<a href="designer-manual-7.html">Next: Creating Database Applications</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 + -