File: tools-styleplugin.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (142 lines) | stat: -rw-r--r-- 11,391 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?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/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
  </div>
  <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>
<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&#x2e;, 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&#x2e;, 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">&amp;</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">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;SimpleStyle&quot;</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">&amp;</span>key)
 {
     <span class="keyword">if</span> (key<span class="operator">.</span>toLower() <span class="operator">=</span><span class="operator">=</span> <span class="string">&quot;simplestyle&quot;</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">&quot;simplestyle&quot;</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 &amp; 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 class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2012 Nokia Corporation 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>
      Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
      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>
</body>
</html>