<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- styleplugin.qdoc --> <title>Qt 4.8: Style Plugin Example</title> <link rel="stylesheet" type="text/css" href="style/style.css" /> <script src="scripts/jquery.js" type="text/javascript"></script> <script src="scripts/functions.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="style/superfish.css" /> <link rel="stylesheet" type="text/css" href="style/narrow.css" /> <!--[if IE]> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="no"> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie6.css"> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie7.css"> <![endif]--> <!--[if IE 8]> <link rel="stylesheet" type="text/css" href="style/style_ie8.css"> <![endif]--> <script src="scripts/superfish.js" type="text/javascript"></script> <script src="scripts/narrow.js" type="text/javascript"></script> </head> <body class="" onload="CheckEmptyAndLoadList();"> <div class="header" id="qtdocheader"> <div class="content"> <div id="nav-logo"> <a href="index.html">Home</a></div> <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a> <div id="narrowsearch"></div> <div id="nav-topright"> <ul> <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li> <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li> <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/"> DOC</a></li> <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li> </ul> </div> <div id="shortCut"> <ul> <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li> <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li> </ul> </div> <ul class="sf-menu" id="narrowmenu"> <li><a href="#">API Lookup</a> <ul> <li><a href="classes.html">Class index</a></li> <li><a href="functions.html">Function index</a></li> <li><a href="modules.html">Modules</a></li> <li><a href="namespaces.html">Namespaces</a></li> <li><a href="qtglobal.html">Global Declarations</a></li> <li><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </li> <li><a href="#">Qt Topics</a> <ul> <li><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li><a href="supported-platforms.html">Supported Platforms</a></li> <li><a href="technology-apis.html">Qt and Key Technologies</a></li> <li><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </li> <li><a href="#">Examples</a> <ul> <li><a href="all-examples.html">Examples</a></li> <li><a href="tutorials.html">Tutorials</a></li> <li><a href="demos.html">Demos</a></li> <li><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </li> </ul> </div> </div> <div class="wrapper"> <div class="hd"> <span></span> </div> <div class="bd group"> <div class="sidebar"> <div class="searchlabel"> Search index:</div> <div class="search" id="sidebarsearch"> <form id="qtdocsearch" action="" onsubmit="return false;"> <fieldset> <input type="text" name="searchstring" id="pageType" value="" /> <div id="resultdialog"> <a href="#" id="resultclose">Close</a> <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p> <ul id="resultlist" class="all"> </ul> </div> </fieldset> </form> </div> <div class="box first bottombar" id="lookup"> <h2 title="API Lookup"><span></span> API Lookup</h2> <div id="list001" class="list"> <ul id="ul001" > <li class="defaultLink"><a href="classes.html">Class index</a></li> <li class="defaultLink"><a href="functions.html">Function index</a></li> <li class="defaultLink"><a href="modules.html">Modules</a></li> <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li> <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li> <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </div> </div> <div class="box bottombar" id="topics"> <h2 title="Qt Topics"><span></span> Qt Topics</h2> <div id="list002" class="list"> <ul id="ul002" > <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li> <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </div> </div> <div class="box" id="examples"> <h2 title="Examples"><span></span> Examples</h2> <div id="list003" class="list"> <ul id="ul003"> <li class="defaultLink"><a href="all-examples.html">Examples</a></li> <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li> <li class="defaultLink"><a href="demos.html">Demos</a></li> <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </div> </div> </div> <div class="wrap"> <div class="toolbar"> <div class="breadcrumb toolblock"> <ul> <li class="first"><a href="index.html">Home</a></li> <!-- Breadcrumbs go here --> <li><a href="all-examples.html">Examples</a></li> <li>Style Plugin Example</li> </ul> </div> <div class="toolbuttons toolblock"> <ul> <li id="smallA" class="t_button">A</li> <li id="medA" class="t_button active">A</li> <li id="bigA" class="t_button">A</li> <li id="print" class="t_button"><a href="javascript:this.print();"> <span>Print</span></a></li> </ul> </div> </div> <div class="content mainContent"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#simplestyleplugin-class-definition">SimpleStylePlugin Class Definition</a></li> <li class="level1"><a href="#simplestyleplugin-class-implementation">SimpleStylePlugin Class Implementation</a></li> <li class="level1"><a href="#the-function">The <tt>main()</tt> function</a></li> <li class="level1"><a href="#the-simple-style-plugin-profile">The Simple Style Plugin Profile</a></li> <li class="level1"><a href="#related-articles-and-examples">Related articles and examples</a></li> </ul> </div> <h1 class="title">Style Plugin Example</h1> <span class="subtitle"></span> <!-- $$$tools/styleplugin-description --> <div class="descr"> <a name="details"></a> <p>Files:</p> <ul> <li><a href="tools-styleplugin-plugin-simplestyle-cpp.html">tools/styleplugin/plugin/simplestyle.cpp</a></li> <li><a href="tools-styleplugin-plugin-simplestyle-h.html">tools/styleplugin/plugin/simplestyle.h</a></li> <li><a href="tools-styleplugin-plugin-simplestyleplugin-cpp.html">tools/styleplugin/plugin/simplestyleplugin.cpp</a></li> <li><a href="tools-styleplugin-plugin-simplestyleplugin-h.html">tools/styleplugin/plugin/simplestyleplugin.h</a></li> <li><a href="tools-styleplugin-stylewindow-stylewindow-cpp.html">tools/styleplugin/stylewindow/stylewindow.cpp</a></li> <li><a href="tools-styleplugin-stylewindow-stylewindow-h.html">tools/styleplugin/stylewindow/stylewindow.h</a></li> <li><a href="tools-styleplugin-stylewindow-main-cpp.html">tools/styleplugin/stylewindow/main.cpp</a></li> <li><a href="tools-styleplugin-styleplugin-pro.html">tools/styleplugin/styleplugin.pro</a></li> <li><a href="tools-styleplugin-plugin-plugin-pro.html">tools/styleplugin/plugin/plugin.pro</a></li> <li><a href="tools-styleplugin-stylewindow-stylewindow-pro.html">tools/styleplugin/stylewindow/stylewindow.pro</a></li> </ul> <p>The Style Plugin example shows how to create a plugin that extends Qt with a new GUI look and feel.<p class="centerAlign"><img src="images/stylepluginexample.png" alt="" /></p><p>On some platforms, the native style will prevent the button from having a red background. In this case, try to run the example in another style (e.g., plastique).</p> <p>A plugin in Qt is a class stored in a shared library that can be loaded by a <a href="qpluginloader.html">QPluginLoader</a> at run-time. When you create plugins in Qt, they either extend a Qt application or Qt itself. Writing a plugin that extends Qt itself is achieved by inheriting one of the plugin <a href="plugins.html">base classes</a>, reimplementing functions from that class, and adding a macro. In this example we extend Qt by adding a new GUI look and feel (i.e., making a new <a href="qstyle.html">QStyle</a> available). A high-level introduction to plugins is given in the plugin <a href="plugins-howto.html">overview document</a>.</p> <p>Plugins that provide new styles inherit the <a href="qstyleplugin.html">QStylePlugin</a> base class. Style plugins are loaded by Qt and made available through <a href="qstylefactory.html">QStyleFactory</a>; we will look at this later. We have implemented <tt>SimpleStylePlugin</tt>, which provides <tt>SimpleStyle</tt>. The new style inherits <a href="qwindowsstyle.html">QWindowsStyle</a> and contributes to widget styling by drawing button backgrounds in red - not a major contribution, but it still makes a new style. We test the plugin with <tt>StyleWindow</tt>, in which we display a <a href="qpushbutton.html">QPushButton</a>.</p> <p>The <tt>SimpleStyle</tt> and <tt>StyleWindow</tt> classes do not contain any plugin specific functionality and their implementations are trivial; we will therefore leap past them and head on to the <tt>SimpleStylePlugin</tt> and the <tt>main()</tt> function. After we have looked at that, we examine the plugin's profile.</p> <a name="simplestyleplugin-class-definition"></a> <h2>SimpleStylePlugin Class Definition</h2> <p><tt>SimpleStylePlugin</tt> inherits <a href="qstyleplugin.html">QStylePlugin</a> and is the plugin class.</p> <pre class="cpp"> <span class="keyword">class</span> SimpleStylePlugin : <span class="keyword">public</span> <span class="type"><a href="qstyleplugin.html">QStylePlugin</a></span> { Q_OBJECT <span class="keyword">public</span>: SimpleStylePlugin() {}; <span class="type"><a href="qstringlist.html">QStringList</a></span> keys() <span class="keyword">const</span>; <span class="type"><a href="qstyle.html">QStyle</a></span> <span class="operator">*</span>create(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>key); };</pre> <p><tt>keys()</tt> returns a list of style names that this plugin can create, while <tt>create()</tt> takes such a string and returns the <a href="qstyle.html">QStyle</a> corresponding to the key. Both functions are pure virtual functions reimplemented from <a href="qstyleplugin.html">QStylePlugin</a>. When an application requests an instance of the <tt>SimpleStyle</tt> style, which this plugin creates, Qt will create it with this plugin.</p> <a name="simplestyleplugin-class-implementation"></a> <h2>SimpleStylePlugin Class Implementation</h2> <p>Here is the implementation of <tt>keys()</tt>:</p> <pre class="cpp"> <span class="type"><a href="qstringlist.html">QStringList</a></span> SimpleStylePlugin<span class="operator">::</span>keys() <span class="keyword">const</span> { <span class="keyword">return</span> <span class="type"><a href="qstringlist.html">QStringList</a></span>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"SimpleStyle"</span>; }</pre> <p>Since this plugin only supports one style, we return a <a href="qstringlist.html">QStringList</a> with the class name of that style.</p> <p>Here is the <tt>create()</tt> function:</p> <pre class="cpp"> <span class="type"><a href="qstyle.html">QStyle</a></span> <span class="operator">*</span>SimpleStylePlugin<span class="operator">::</span>create(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>key) { <span class="keyword">if</span> (key<span class="operator">.</span>toLower() <span class="operator">=</span><span class="operator">=</span> <span class="string">"simplestyle"</span>) <span class="keyword">return</span> <span class="keyword">new</span> SimpleStyle; <span class="keyword">return</span> <span class="number">0</span>; }</pre> <p>Note that the key for style plugins are case insensitive. The case sensitivity varies from plugin to plugin, so you need to check this when implementing new plugins.</p> <a name="the-function"></a> <h2>The <tt>main()</tt> function</h2> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argv<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>args<span class="operator">[</span><span class="operator">]</span>) { <span class="type"><a href="qapplication.html">QApplication</a></span> app(argv<span class="operator">,</span> args); <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>setStyle(<span class="type"><a href="qstylefactory.html">QStyleFactory</a></span><span class="operator">::</span>create(<span class="string">"simplestyle"</span>)); StyleWindow window; window<span class="operator">.</span>resize(<span class="number">200</span><span class="operator">,</span> <span class="number">50</span>); window<span class="operator">.</span>show(); <span class="keyword">return</span> app<span class="operator">.</span>exec(); }</pre> <p>Qt loads the available style plugins when the <a href="qapplication.html">QApplication</a> object is initialized. The <a href="qstylefactory.html">QStyleFactory</a> class knows about all styles and produces them with <a href="qstylefactory.html#create">create()</a> (it is a wrapper around all the style plugins).</p> <a name="the-simple-style-plugin-profile"></a> <h2>The Simple Style Plugin Profile</h2> <p>The <tt>SimpleStylePlugin</tt> lives in its own directory and have its own profile:</p> <pre class="cpp"> TEMPLATE = lib CONFIG += plugin HEADERS = simplestyle.h \ simplestyleplugin.h SOURCES = simplestyle.cpp \ simplestyleplugin.cpp TARGET = simplestyleplugin</pre> <p>In the plugin profile we need to set the lib template as we are building a shared library instead of an executable. We must also set the config to plugin. We set the library to be stored in the styles folder under stylewindow because this is a path in which Qt will search for style plugins.</p> <a name="related-articles-and-examples"></a> <h2>Related articles and examples</h2> <p>In addition to the plugin <a href="plugins-howto.html">overview document</a>, we have other examples and articles that concern plugins.</p> <p>In the <a href="tools-echoplugin.html">echo plugin example</a> we show how to implement plugins that extends Qt applications rather than Qt itself, which is the case with the style plugin of this example. The <a href="tools-plugandpaint.html">plug & paint</a> example shows how to implement a static plugin as well as being a more involved example on plugins that extend applications.</p> </div> <!-- @@@tools/styleplugin --> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2013 Digia Plc and/or its subsidiaries. Documentation contributions included herein are the copyrights of their respective owners.</p> <br /> <p> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.</p> <p> Documentation sources may be obtained from <a href="http://www.qt-project.org"> www.qt-project.org</a>.</p> <br /> <p> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. <a title="Privacy Policy" href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p> </div> <script src="scripts/functions.js" type="text/javascript"></script> </body> </html>