File: tools-inputpanel.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 (304 lines) | stat: -rw-r--r-- 29,426 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?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" />
<!-- inputpanel.qdoc -->
  <title>Qt 4.8: Input Panel 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>Input Panel 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="#main-form-class-definition">Main Form Class Definition</a></li>
<li class="level1"><a href="#myinputpanelcontext-class-definition">MyInputPanelContext Class Definition</a></li>
<li class="level1"><a href="#myinputpanelcontext-class-implementation">MyInputPanelContext Class Implementation</a></li>
<li class="level1"><a href="#myinputpanel-class-definition">MyInputPanel Class Definition</a></li>
<li class="level1"><a href="#myinputpanel-class-implementation">MyInputPanel Class Implementation</a></li>
<li class="level1"><a href="#setting-the-input-context">Setting the Input Context</a></li>
<li class="level1"><a href="#further-reading">Further Reading</a></li>
</ul>
</div>
<h1 class="title">Input Panel Example</h1>
<span class="subtitle"></span>
<!-- $$$tools/inputpanel-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="tools-inputpanel-mainform-ui.html">tools/inputpanel/mainform.ui</a></li>
<li><a href="tools-inputpanel-myinputpanel-cpp.html">tools/inputpanel/myinputpanel.cpp</a></li>
<li><a href="tools-inputpanel-myinputpanel-h.html">tools/inputpanel/myinputpanel.h</a></li>
<li><a href="tools-inputpanel-myinputpanelcontext-cpp.html">tools/inputpanel/myinputpanelcontext.cpp</a></li>
<li><a href="tools-inputpanel-myinputpanelcontext-h.html">tools/inputpanel/myinputpanelcontext.h</a></li>
<li><a href="tools-inputpanel-myinputpanelform-ui.html">tools/inputpanel/myinputpanelform.ui</a></li>
<li><a href="tools-inputpanel-main-cpp.html">tools/inputpanel/main.cpp</a></li>
<li><a href="tools-inputpanel-inputpanel-pro.html">tools/inputpanel/inputpanel.pro</a></li>
</ul>
<p>The Input Panel example shows how to create an input panel that can be used to input text into widgets using only the pointer and no keyboard.<p class="centerAlign"><img src="images/inputpanel-example.png" alt="" /></p><p>The input fields in the main window have no function other than to accept input. The main focus is on how the extra input panel can be used to input text without the need for a real keyboard or keypad.</p>
<a name="main-form-class-definition"></a>
<h2>Main Form Class Definition</h2>
<p>Because the main window has no other function than to accept input, it has no class definition. Instead, its whole layout is made in Qt Designer. This emphasizes the point that no widget specific code is needed to use input panels with Qt.</p>
<a name="myinputpanelcontext-class-definition"></a>
<h2>MyInputPanelContext Class Definition</h2>
<pre class="cpp"> <span class="keyword">class</span> MyInputPanelContext : <span class="keyword">public</span> <span class="type"><a href="qinputcontext.html">QInputContext</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     MyInputPanelContext();
     <span class="operator">~</span>MyInputPanelContext();

     <span class="type">bool</span> filterEvent(<span class="keyword">const</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">*</span> event);

     <span class="type"><a href="qstring.html">QString</a></span> identifierName();
     <span class="type"><a href="qstring.html">QString</a></span> language();

     <span class="type">bool</span> isComposing() <span class="keyword">const</span>;

     <span class="type">void</span> reset();

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> sendCharacter(<span class="type"><a href="qchar.html">QChar</a></span> character);

 <span class="keyword">private</span>:
     <span class="type">void</span> updatePosition();

 <span class="keyword">private</span>:
     MyInputPanel <span class="operator">*</span>inputPanel;
 };</pre>
<p>The <tt>MyInputPanelContext</tt> class inherits <a href="qinputcontext.html">QInputContext</a>, which is Qt's base class for handling input methods. <tt>MyInputPanelContext</tt> is responsible for managing the state of the input panel and sending input method events to the receiving widgets.</p>
<p>The <tt>inputPanel</tt> member is a pointer to the input panel widget itself; in other words, the window that will display the buttons used for input.</p>
<p>The <tt>identifierName()</tt>, <tt>language()</tt>, <tt>isComposing()</tt> and <tt>reset()</tt> functions are there mainly to fill in the pure virtual functions in the base class, <a href="qinputcontext.html">QInputContext</a>, but they can be useful in other scenarios. The important functions and slots are the following:</p>
<ul>
<li><tt>filterEvent()</tt> is where we receive events telling us to open or close the input panel.</li>
<li><tt>sendCharacter()</tt> is a slot which is called when we want to send a character to the focused widget.</li>
<li><tt>updatePosition()</tt> is used to position the input panel relative to the focused widget, and will be used when opening the input panel.</li>
</ul>
<a name="myinputpanelcontext-class-implementation"></a>
<h2>MyInputPanelContext Class Implementation</h2>
<p>In the constructor we connect to the <tt>characterGenerated()</tt> signal of the input panel, in order to receive key presses. We'll see how it works in detail later on.</p>
<pre class="cpp"> MyInputPanelContext<span class="operator">::</span>MyInputPanelContext()
 {
     inputPanel <span class="operator">=</span> <span class="keyword">new</span> MyInputPanel;
     connect(inputPanel<span class="operator">,</span> SIGNAL(characterGenerated(<span class="type"><a href="qchar.html">QChar</a></span>))<span class="operator">,</span> SLOT(sendCharacter(<span class="type"><a href="qchar.html">QChar</a></span>)));
 }</pre>
<p>In the <tt>filterEvent()</tt> function, we must look for the two event types: <tt>RequestSoftwareInputPanel</tt> and <tt>CloseSoftwareInputPanel</tt>.</p>
<pre class="cpp"> <span class="type">bool</span> MyInputPanelContext<span class="operator">::</span>filterEvent(<span class="keyword">const</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">*</span> event)
 {
     <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>RequestSoftwareInputPanel) {
         updatePosition();
         inputPanel<span class="operator">-</span><span class="operator">&gt;</span>show();
         <span class="keyword">return</span> <span class="keyword">true</span>;
     } <span class="keyword">else</span> <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>CloseSoftwareInputPanel) {
         inputPanel<span class="operator">-</span><span class="operator">&gt;</span>hide();
         <span class="keyword">return</span> <span class="keyword">true</span>;
     }
     <span class="keyword">return</span> <span class="keyword">false</span>;
 }</pre>
<p>The first type will be sent whenever an input capable widget wants to ask for an input panel. Qt's input widgets do this automatically. If we receive that type of event, we call <tt>updatePosition()</tt> &mdash; we'll see later on what it does &mdash; then show the actual input panel widget. If we receive the <tt>CloseSoftwareInputPanel</tt> event, we do the opposite, and hide the input panel.</p>
<pre class="cpp"> <span class="type">void</span> MyInputPanelContext<span class="operator">::</span>sendCharacter(<span class="type"><a href="qchar.html">QChar</a></span> character)
 {
     <span class="type"><a href="qpointer.html">QPointer</a></span><span class="operator">&lt;</span><span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">&gt;</span> w <span class="operator">=</span> focusWidget();

     <span class="keyword">if</span> (<span class="operator">!</span>w)
         <span class="keyword">return</span>;

     <span class="type"><a href="qkeyevent.html">QKeyEvent</a></span> keyPress(<span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>KeyPress<span class="operator">,</span> character<span class="operator">.</span>unicode()<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>NoModifier<span class="operator">,</span> <span class="type"><a href="qstring.html">QString</a></span>(character));
     <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>sendEvent(w<span class="operator">,</span> <span class="operator">&amp;</span>keyPress);

     <span class="keyword">if</span> (<span class="operator">!</span>w)
         <span class="keyword">return</span>;

     <span class="type"><a href="qkeyevent.html">QKeyEvent</a></span> keyRelease(<span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>KeyPress<span class="operator">,</span> character<span class="operator">.</span>unicode()<span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>NoModifier<span class="operator">,</span> <span class="type"><a href="qstring.html">QString</a></span>());
     <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>sendEvent(w<span class="operator">,</span> <span class="operator">&amp;</span>keyRelease);
 }</pre>
<p>We implement the <tt>sendCharacter()</tt> function so that it sends the supplied character to the focused widget. All <a href="qinputcontext.html">QInputContext</a> based classes are always supposed to send events to the widget returned by <a href="qinputcontext.html#focusWidget">QInputContext::focusWidget</a>(). Note the <a href="qpointer.html">QPointer</a> guards to make sure that the widget does not get destroyed in between events.</p>
<p>Also note that we chose to use key press events in this example. For more complex use cases with composed text it might be more appropriate to send <a href="qinputmethodevent.html">QInputMethodEvent</a> events.</p>
<p>The <tt>updatePosition()</tt> function is implemented to position the actual input panel window directly below the focused widget.</p>
<pre class="cpp"> <span class="type">void</span> MyInputPanelContext<span class="operator">::</span>updatePosition()
 {
     <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> focusWidget();
     <span class="keyword">if</span> (<span class="operator">!</span>widget)
         <span class="keyword">return</span>;

     <span class="type"><a href="qrect.html">QRect</a></span> widgetRect <span class="operator">=</span> widget<span class="operator">-</span><span class="operator">&gt;</span>rect();
     <span class="type"><a href="qpoint.html">QPoint</a></span> panelPos <span class="operator">=</span> <span class="type"><a href="qpoint.html">QPoint</a></span>(widgetRect<span class="operator">.</span>left()<span class="operator">,</span> widgetRect<span class="operator">.</span>bottom() <span class="operator">+</span> <span class="number">2</span>);
     panelPos <span class="operator">=</span> widget<span class="operator">-</span><span class="operator">&gt;</span>mapToGlobal(panelPos);
     inputPanel<span class="operator">-</span><span class="operator">&gt;</span>move(panelPos);
 }</pre>
<p>It performs the positioning by obtaining the coordinates of the focused widget and translating them to global coordinates.</p>
<a name="myinputpanel-class-definition"></a>
<h2>MyInputPanel Class Definition</h2>
<p>The <tt>MyInputPanel</tt> class inherits <a href="qwidget.html">QWidget</a> and is used to display the input panel widget and its buttons.</p>
<pre class="cpp"> <span class="keyword">class</span> MyInputPanel : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     MyInputPanel();

 <span class="keyword">signals</span>:
     <span class="type">void</span> characterGenerated(<span class="type"><a href="qchar.html">QChar</a></span> character);

 <span class="keyword">protected</span>:
     <span class="type">bool</span> event(<span class="type"><a href="qevent.html">QEvent</a></span> <span class="operator">*</span>e);

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> saveFocusWidget(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>oldFocus<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>newFocus);
     <span class="type">void</span> buttonClicked(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>w);

 <span class="keyword">private</span>:
     Ui<span class="operator">::</span>MyInputPanelForm form;
     <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>lastFocusedWidget;
     <span class="type"><a href="qsignalmapper.html">QSignalMapper</a></span> signalMapper;
 };</pre>
<p>If we look at the member variables first, we see that there is <tt>form</tt>, which is made with Qt Designer, that contains the layout of buttons to click. Note that all the buttons in the layout have been declared with the <tt>NoFocus</tt> focus policy so that we can maintain focus on the window receiving input instead of the window containing buttons.</p>
<p>The <tt>lastFocusedWidget</tt> is a helper variable, which also aids in maintaining focus.</p>
<p><tt>signalMapper</tt> is an instance of the <a href="qsignalmapper.html">QSignalMapper</a> class and is there to help us tell which button was clicked. Since they are all very similar this is a better solution than creating a separate slot for each one.</p>
<p>The functions that we implement in <tt>MyInputPanel</tt> are the following:</p>
<ul>
<li><tt>event()</tt> is used to intercept and manipulate focus events, so we can maintain focus in the main window.</li>
<li><tt>saveFocusWidget()</tt> is a slot which will be called whenever focus changes, and allows us to store the newly focused widget in <tt>lastFocusedWidget</tt>, so that its focus can be restored if it loses it to the input panel.</li>
<li><tt>buttonClicked()</tt> is a slot which will be called by the <tt>signalMapper</tt> whenever it receives a <tt>clicked()</tt> signal from any of the buttons.</li>
</ul>
<a name="myinputpanel-class-implementation"></a>
<h2>MyInputPanel Class Implementation</h2>
<p>If we look at the constructor first, we have a lot of signals to connect to!</p>
<p>We connect the <a href="qapplication.html#focusChanged">QApplication::focusChanged</a>() signal to the <tt>saveFocusWidget()</tt> signal in order to get focus updates. Then comes the interesting part with the signal mapper: the series of <tt>setMapping()</tt> calls sets the mapper up so that each signal from one of the buttons will result in a <a href="qsignalmapper.html#mapped">QSignalMapper::mapped</a>() signal, with the given widget as a parameter. This allows us to do general processing of clicks.</p>
<pre class="cpp"> MyInputPanel<span class="operator">::</span>MyInputPanel()
     : <span class="type"><a href="qwidget.html">QWidget</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>Tool <span class="operator">|</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>WindowStaysOnTopHint)<span class="operator">,</span>
       lastFocusedWidget(<span class="number">0</span>)
 {
     form<span class="operator">.</span>setupUi(<span class="keyword">this</span>);

     connect(qApp<span class="operator">,</span> SIGNAL(focusChanged(<span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">*</span><span class="operator">,</span><span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">*</span>))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(saveFocusWidget(<span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">*</span><span class="operator">,</span><span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">*</span>)));

     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_1<span class="operator">,</span> form<span class="operator">.</span>panelButton_1);
     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_2<span class="operator">,</span> form<span class="operator">.</span>panelButton_2);
     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_3<span class="operator">,</span> form<span class="operator">.</span>panelButton_3);
     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_4<span class="operator">,</span> form<span class="operator">.</span>panelButton_4);
     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_5<span class="operator">,</span> form<span class="operator">.</span>panelButton_5);
     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_6<span class="operator">,</span> form<span class="operator">.</span>panelButton_6);
     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_7<span class="operator">,</span> form<span class="operator">.</span>panelButton_7);
     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_8<span class="operator">,</span> form<span class="operator">.</span>panelButton_8);
     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_9<span class="operator">,</span> form<span class="operator">.</span>panelButton_9);
     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_star<span class="operator">,</span> form<span class="operator">.</span>panelButton_star);
     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_0<span class="operator">,</span> form<span class="operator">.</span>panelButton_0);
     signalMapper<span class="operator">.</span>setMapping(form<span class="operator">.</span>panelButton_hash<span class="operator">,</span> form<span class="operator">.</span>panelButton_hash);

     connect(form<span class="operator">.</span>panelButton_1<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));
     connect(form<span class="operator">.</span>panelButton_2<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));
     connect(form<span class="operator">.</span>panelButton_3<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));
     connect(form<span class="operator">.</span>panelButton_4<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));
     connect(form<span class="operator">.</span>panelButton_5<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));
     connect(form<span class="operator">.</span>panelButton_6<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));
     connect(form<span class="operator">.</span>panelButton_7<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));
     connect(form<span class="operator">.</span>panelButton_8<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));
     connect(form<span class="operator">.</span>panelButton_9<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));
     connect(form<span class="operator">.</span>panelButton_star<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));
     connect(form<span class="operator">.</span>panelButton_0<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));
     connect(form<span class="operator">.</span>panelButton_hash<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
             <span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SLOT(map()));

     connect(<span class="operator">&amp;</span>signalMapper<span class="operator">,</span> SIGNAL(mapped(<span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">*</span>))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(buttonClicked(<span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">*</span>)));
 }</pre>
<p>The next series of connections then connect each button's <tt>clicked()</tt> signal to the signal mapper. Finally, we create a connection from the <tt>mapped()</tt> signal to the <tt>buttonClicked()</tt> slot, where we will handle it.</p>
<pre class="cpp"> <span class="type">void</span> MyInputPanel<span class="operator">::</span>buttonClicked(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>w)
 {
     <span class="type"><a href="qchar.html">QChar</a></span> chr <span class="operator">=</span> qvariant_cast<span class="operator">&lt;</span><span class="type"><a href="qchar.html">QChar</a></span><span class="operator">&gt;</span>(w<span class="operator">-</span><span class="operator">&gt;</span>property(<span class="string">&quot;buttonValue&quot;</span>));
     <span class="keyword">emit</span> characterGenerated(chr);
 }</pre>
<p>In the <tt>buttonClicked()</tt> slot, we extract the value of the &quot;buttonValue&quot; property. This is a custom property which was created in Qt Designer and set to the character that we wish the button to produce. Then we emit the <tt>characterGenerated()</tt> signal, which <tt>MyInputPanelContext</tt> is connected to. This will in turn cause it to send the input to the focused widget.</p>
<p>In the <tt>saveFocusWidget()</tt> slot, we test whether the newly focused widget is a child of the input panel or not, using the <a href="qwidget.html#isAncestorOf">QWidget::isAncestorOf</a>() call.</p>
<pre class="cpp"> <span class="type">void</span> MyInputPanel<span class="operator">::</span>saveFocusWidget(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span> <span class="comment">/*oldFocus*/</span><span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>newFocus)
 {
     <span class="keyword">if</span> (newFocus <span class="operator">!</span><span class="operator">=</span> <span class="number">0</span> <span class="operator">&amp;</span><span class="operator">&amp;</span> <span class="operator">!</span><span class="keyword">this</span><span class="operator">-</span><span class="operator">&gt;</span>isAncestorOf(newFocus)) {
         lastFocusedWidget <span class="operator">=</span> newFocus;
     }
 }</pre>
<p>If it isn't, it means that the widget is outside the input panel, and we store a pointer to that widget for later.</p>
<p>In the <tt>event()</tt> function we handle <a href="qevent.html#Type-enum">QEvent::WindowActivate</a> event, which occurs if the focus switches to the input panel.</p>
<pre class="cpp">     <span class="keyword">case</span> <span class="type"><a href="qevent.html">QEvent</a></span><span class="operator">::</span>WindowActivate:
         <span class="keyword">if</span> (lastFocusedWidget)
             lastFocusedWidget<span class="operator">-</span><span class="operator">&gt;</span>activateWindow();
         <span class="keyword">break</span>;</pre>
<p>Since we want avoid focus on the input panel, we immediately call <a href="qwidget.html#activateWindow">QWidget::activateWindow</a>() on the widget that last had focus, so that input into that widget can continue. We ignore any other events that we receive.</p>
<a name="setting-the-input-context"></a>
<h2>Setting the Input Context</h2>
<p>The main function for the example is very similar to those for other examples. The only real difference is that it creates a <tt>MyInputPanelContext</tt> and sets it as the application-wide input context.</p>
<pre class="cpp"> <span class="preprocessor">#include &quot;myinputpanelcontext.h&quot;</span>
 <span class="preprocessor">#include &quot;ui_mainform.h&quot;</span>

 <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>argv)
 {
     <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);

     MyInputPanelContext <span class="operator">*</span>ic <span class="operator">=</span> <span class="keyword">new</span> MyInputPanelContext;
     app<span class="operator">.</span>setInputContext(ic);

     <span class="type"><a href="qwidget.html">QWidget</a></span> widget;
     Ui<span class="operator">::</span>MainForm form;
     form<span class="operator">.</span>setupUi(<span class="operator">&amp;</span>widget);
     widget<span class="operator">.</span>show();
     <span class="keyword">return</span> app<span class="operator">.</span>exec();
 }</pre>
<p>With the input context in place, we set up and show the user interface made in Qt Designer before running the event loop.</p>
<a name="further-reading"></a>
<h2>Further Reading</h2>
<p>This example shows a specific kind of input context that uses interaction with a widget to provide input for another. Qt's input context system can also be used to create other kinds of input methods. We recommend starting with the <a href="qinputcontext.html">QInputContext</a> documentation if you want to explore further.</p>
</div>
<!-- @@@tools/inputpanel -->
  <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>