Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 1dd17e0d683ef79b4bb6872bbf359d7f > files > 7558

qt4-doc-4.8.7-26.2.mga7.noarch.rpm

<?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/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 &amp; 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>&nbsp;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 &amp; 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>Input Panel 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="#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>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2015 The Qt Company Ltd.
      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>
      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. <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>