<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- styleplugin.qdoc --> <title>Style Plugin Example | Qt Widgets 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td >Style Plugin Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <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-main-function">The <code>main()</code> 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> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Style Plugin Example</h1> <span class="subtitle"></span> <!-- $$$tools/styleplugin-brief --> <p>This example shows how to create a plugin that extends Qt with a new GUI look and feel.</p> <!-- @@@tools/styleplugin --> <!-- $$$tools/styleplugin-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/stylepluginexample.png" alt="" /></p><p>A plugin in Qt is a class stored in a shared library that can be loaded by a <a href="../qtcore/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="../qtcore/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 overview document.</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 <code>SimpleStylePlugin</code>, which provides <code>SimpleStyle</code>. The new style contributes to widget styling by drawing button backgrounds in red - not a major contribution, but it still makes a new style.</p> <p>The new style is platform agnostic in the sense that it is not based on any specific style implementation, but uses <a href="qproxystyle.html">QProxyStyle</a> to merely tweak the looks in the current application style that defaults to the native system style.</p> <p><b>Note: </b>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., fusion).</p><p>We test the plugin with <code>StyleWindow</code>, in which we display a <a href="qpushbutton.html">QPushButton</a>. The <code>SimpleStyle</code> and <code>StyleWindow</code> classes do not contain any plugin specific functionality and their implementations are trivial; we will therefore leap past them and head on to the <code>SimpleStylePlugin</code> and the <code>main()</code> function. After we have looked at that, we examine the plugin's profile.</p> <a name="simplestyleplugin-class-definition"></a> <h2 id="simplestyleplugin-class-definition">SimpleStylePlugin Class Definition</h2> <p><code>SimpleStylePlugin</code> 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 Q_PLUGIN_METADATA(IID <span class="string">"org.qt-project.Qt.QStyleFactoryInterface"</span> FILE <span class="string">"simplestyle.json"</span>) <span class="keyword">public</span>: SimpleStylePlugin() {} <span class="type"><a href="../qtcore/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="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>key) override; }; </pre> <p><code>keys()</code> returns a list of style names that this plugin can create, while <code>create()</code> 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 <code>SimpleStyle</code> style, which this plugin creates, Qt will create it with this plugin.</p> <a name="simplestyleplugin-class-implementation"></a> <h2 id="simplestyleplugin-class-implementation">SimpleStylePlugin Class Implementation</h2> <p>Here is the implementation of <code>keys()</code>:</p> <pre class="cpp"> <span class="type"><a href="../qtcore/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="../qtcore/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="../qtcore/qstringlist.html">QStringList</a> with the class name of that style.</p> <p>Here is the <code>create()</code> 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="../qtcore/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-main-function"></a> <h2 id="the-main-function">The <code>main()</code> 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 id="the-simple-style-plugin-profile">The Simple Style Plugin Profile</h2> <p>The <code>SimpleStylePlugin</code> lives in its own directory and have its own profile:</p> <pre class="cpp"> TEMPLATE = lib CONFIG += plugin QT += widgets 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 id="related-articles-and-examples">Related Articles and Examples</h2> <p>In addition to the plugin overview document, we have other examples and articles that concern plugins.</p> <p>In the <a href="qtwidgets-tools-echoplugin-example.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="qtwidgets-tools-plugandpaint-app-example.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> <p>Files:</p> <ul> <li><a href="qtwidgets-tools-styleplugin-plugin-plugin-pro.html">tools/styleplugin/plugin/plugin.pro</a></li> <li><a href="qtwidgets-tools-styleplugin-plugin-simplestyle-cpp.html">tools/styleplugin/plugin/simplestyle.cpp</a></li> <li><a href="qtwidgets-tools-styleplugin-plugin-simplestyle-h.html">tools/styleplugin/plugin/simplestyle.h</a></li> <li><a href="qtwidgets-tools-styleplugin-plugin-simplestyleplugin-cpp.html">tools/styleplugin/plugin/simplestyleplugin.cpp</a></li> <li><a href="qtwidgets-tools-styleplugin-plugin-simplestyleplugin-h.html">tools/styleplugin/plugin/simplestyleplugin.h</a></li> <li><a href="qtwidgets-tools-styleplugin-styleplugin-pro.html">tools/styleplugin/styleplugin.pro</a></li> <li><a href="qtwidgets-tools-styleplugin-stylewindow-main-cpp.html">tools/styleplugin/stylewindow/main.cpp</a></li> <li><a href="qtwidgets-tools-styleplugin-stylewindow-stylewindow-cpp.html">tools/styleplugin/stylewindow/stylewindow.cpp</a></li> <li><a href="qtwidgets-tools-styleplugin-stylewindow-stylewindow-h.html">tools/styleplugin/stylewindow/stylewindow.h</a></li> <li><a href="qtwidgets-tools-styleplugin-stylewindow-stylewindow-pro.html">tools/styleplugin/stylewindow/stylewindow.pro</a></li> </ul> </div> <!-- @@@tools/styleplugin --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> 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.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>