File: webkit-webplugin.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 (191 lines) | stat: -rw-r--r-- 18,136 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
<?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" />
<!-- webplugin.qdoc -->
  <title>Qt 4.8: Web 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>Web 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="#setting-up-communications">Setting up Communications</a></li>
<li class="level1"><a href="#connecting-components-together">Connecting Components Together</a></li>
<li class="level1"><a href="#linking-things-together">Linking Things Together</a></li>
</ul>
</div>
<h1 class="title">Web Plugin Example</h1>
<span class="subtitle"></span>
<!-- $$$webkit/webplugin-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="webkit-webplugin-csvfactory-cpp.html">webkit/webplugin/csvfactory.cpp</a></li>
<li><a href="webkit-webplugin-csvfactory-h.html">webkit/webplugin/csvfactory.h</a></li>
<li><a href="webkit-webplugin-csvview-cpp.html">webkit/webplugin/csvview.cpp</a></li>
<li><a href="webkit-webplugin-csvview-h.html">webkit/webplugin/csvview.h</a></li>
<li><a href="webkit-webplugin-mainwindow-cpp.html">webkit/webplugin/mainwindow.cpp</a></li>
<li><a href="webkit-webplugin-mainwindow-h.html">webkit/webplugin/mainwindow.h</a></li>
<li><a href="webkit-webplugin-main-cpp.html">webkit/webplugin/main.cpp</a></li>
<li><a href="webkit-webplugin-webplugin-pro.html">webkit/webplugin/webplugin.pro</a></li>
<li><a href="webkit-webplugin-webplugin-qrc.html">webkit/webplugin/webplugin.qrc</a></li>
</ul>
<p>The Web Plugin example shows how to communicate between a Qt widget embedded in a Web page and the page itself.<p class="centerAlign"><img src="images/webkit-webplugin.png" alt="A table widget embedded in a Web page." /></p><p>In this example, we will take the widget described in the <a href="webkit-simplewebplugin.html">Simple Web Plugin Example</a> and show how to set up communications between the widget and the Web environment.</p>
<a name="setting-up-communications"></a>
<h2>Setting up Communications</h2>
<p>There are two ways of interacting with the content in a Web page. The first way involves the use of <a href="qwebelement.html">QWebElement</a> to read and modify the page content and structure; this is useful for certain types of application, as demonstrated by the <a href="webkit-domtraversal.html">DOM Traversal Example</a> and the <a href="webkit-simpleselector.html">Simple Selector Example</a>.</p>
<p>The second way is to add Qt objects to the page, connecting their signals to JavaScript functions, and executing the object's slots directly from JavaScript in the page. We explore this approach in this example.</p>
<p>To perform this communication, we require an updated <tt>CSVView</tt> widget from the <a href="webkit-simplewebplugin.html">Simple Web Plugin Example</a> that can emit a signal whenever a row is selected, a JavaScript function to modify elements on the page, and some glue code to make the connection.</p>
<p>On the page, the plugin is declared like this:</p>
<pre class="cpp"> &lt;object type=&quot;text/csv;header=present;charset=utf8&quot;
         data=&quot;qrc:/data/accounts.csv&quot;
         width=&quot;100%&quot; height=&quot;300&quot;&gt;
 &lt;/object&gt;</pre>
<p>As in the previous example, the <tt>&lt;object&gt;</tt> definition includes information about the data to be displayed, its location, and the dimensions of the plugin in the page.</p>
<p>Later in the document, we include a table that we will update with data from the <tt>CSVView</tt> widget:</p>
<pre class="cpp"> &lt;table&gt;
 &lt;tr&gt;
   &lt;th&gt;Name:&lt;/th&gt;
   &lt;td id=&quot;customers_name&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;&lt;tr&gt;
   &lt;th&gt;Address:&lt;/th&gt;
   &lt;td id=&quot;customers_address&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;&lt;tr&gt;
   &lt;th&gt;Quantity:&lt;/th&gt;
   &lt;td id=&quot;customers_quantity&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/table&gt;</pre>
<p>The <tt>CSVView</tt> widget is similar to the previous version. However, we wish to obtain and export individual rows of data, so we define the <tt>rowSelected()</tt> signal and <tt>exportRow()</tt> slot to perform this task.</p>
<pre class="cpp"> <span class="keyword">class</span> CSVView : <span class="keyword">public</span> <span class="type"><a href="qtableview.html">QTableView</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     CSVView(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>mimeType<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">signals</span>:
     <span class="type">void</span> rowSelected(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>name<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>address<span class="operator">,</span>
                      <span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>quantity);

 <span class="keyword">public</span> <span class="keyword">slots</span>:
     <span class="type">void</span> updateModel();

 <span class="keyword">private</span> <span class="keyword">slots</span>:
     <span class="type">void</span> exportRow(<span class="keyword">const</span> <span class="type"><a href="qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>current);

 <span class="keyword">private</span>:
     <span class="type">void</span> addRow(<span class="type">bool</span> firstLine<span class="operator">,</span> <span class="type"><a href="qstandarditemmodel.html">QStandardItemModel</a></span> <span class="operator">*</span>model<span class="operator">,</span>
                 <span class="keyword">const</span> <span class="type"><a href="qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="qstandarditem.html">QStandardItem</a></span> <span class="operator">*</span><span class="operator">&gt;</span> <span class="operator">&amp;</span>items);

     <span class="type"><a href="qstring.html">QString</a></span> mimeType;
 };</pre>
<p>Since we wish to obtain one row of data at a time, the constructor includes code to restrict how the user can interact with the view:</p>
<pre class="cpp"> CSVView<span class="operator">::</span>CSVView(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>mimeType<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
     : <span class="type"><a href="qtableview.html">QTableView</a></span>(parent)
 {
     <span class="keyword">this</span><span class="operator">-</span><span class="operator">&gt;</span>mimeType <span class="operator">=</span> mimeType;
 }</pre>
<p>The <tt>exportRow()</tt> slot provides a convenient mechanism for obtaining and emitting the values found on the current row of the table:</p>
<pre class="cpp"> <span class="type">void</span> CSVView<span class="operator">::</span>exportRow(<span class="keyword">const</span> <span class="type"><a href="qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>current)
 {
     <span class="type"><a href="qstring.html">QString</a></span> name <span class="operator">=</span> model()<span class="operator">-</span><span class="operator">&gt;</span>index(current<span class="operator">.</span>row()<span class="operator">,</span> <span class="number">0</span>)<span class="operator">.</span>data()<span class="operator">.</span>toString();
     <span class="type"><a href="qstring.html">QString</a></span> address <span class="operator">=</span> model()<span class="operator">-</span><span class="operator">&gt;</span>index(current<span class="operator">.</span>row()<span class="operator">,</span> <span class="number">1</span>)<span class="operator">.</span>data()<span class="operator">.</span>toString();
     <span class="type"><a href="qstring.html">QString</a></span> quantity <span class="operator">=</span> model()<span class="operator">-</span><span class="operator">&gt;</span>index(current<span class="operator">.</span>row()<span class="operator">,</span> <span class="number">2</span>)<span class="operator">.</span>data()<span class="operator">.</span>toString();

     <span class="keyword">emit</span> rowSelected(name<span class="operator">,</span> address<span class="operator">,</span> quantity);
 }</pre>
<p>This slot is connected to a signal belonging to the view's selection model: <a href="qitemselectionmodel.html#currentChanged">currentChanged()</a>. This can be seen by examining the <tt>updateModel()</tt> function in the source code.</p>
<p><tt>exportRow()</tt> emits the <tt>rowSelected()</tt> signal, passing strings containing the name, address and quantity in the current table row. To see how this data is passed to the Web page, we need to look at the <tt>CSVFactory</tt> class.</p>
<a name="connecting-components-together"></a>
<h2>Connecting Components Together</h2>
<p>In the <tt>CSVFactory</tt> class, we reimplement the <a href="qwebpluginfactory.html#create">create()</a> function to create instances of the <tt>CSVView</tt> class, as in the previous example.</p>
<pre class="cpp"> <span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>CSVFactory<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>mimeType<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qurl.html">QUrl</a></span> <span class="operator">&amp;</span>url<span class="operator">,</span>
                             <span class="keyword">const</span> <span class="type"><a href="qstringlist.html">QStringList</a></span> <span class="operator">&amp;</span>argumentNames<span class="operator">,</span>
                             <span class="keyword">const</span> <span class="type"><a href="qstringlist.html">QStringList</a></span> <span class="operator">&amp;</span>argumentValues) <span class="keyword">const</span>
 {
     <span class="keyword">if</span> (mimeType <span class="operator">!</span><span class="operator">=</span> <span class="string">&quot;text/csv&quot;</span>)
         <span class="keyword">return</span> <span class="number">0</span>;

     <span class="type"><a href="qhash.html">QHash</a></span><span class="operator">&lt;</span><span class="type"><a href="qstring.html">QString</a></span><span class="operator">,</span> <span class="type"><a href="qstring.html">QString</a></span><span class="operator">&gt;</span> arguments;
     <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> argumentNames<span class="operator">.</span>count(); <span class="operator">+</span><span class="operator">+</span>i)
         arguments<span class="operator">[</span>argumentNames<span class="operator">[</span>i<span class="operator">]</span><span class="operator">]</span> <span class="operator">=</span> argumentValues<span class="operator">[</span>i<span class="operator">]</span>;

     CSVView <span class="operator">*</span>view <span class="operator">=</span> <span class="keyword">new</span> CSVView(arguments<span class="operator">[</span><span class="string">&quot;type&quot;</span><span class="operator">]</span>);</pre>
<p>We also expose the view widget to the frame in the page that contains the elements, and set up a connection between the view and a JavaScript function defined in the page header:</p>
<pre class="cpp">     <span class="type"><a href="qwebframe.html">QWebFrame</a></span> <span class="operator">*</span>frame <span class="operator">=</span> webView<span class="operator">-</span><span class="operator">&gt;</span>page()<span class="operator">-</span><span class="operator">&gt;</span>mainFrame();
     frame<span class="operator">-</span><span class="operator">&gt;</span>addToJavaScriptWindowObject(<span class="string">&quot;view&quot;</span><span class="operator">,</span> view);
     frame<span class="operator">-</span><span class="operator">&gt;</span>evaluateJavaScript(<span class="string">&quot;view.rowSelected.connect(fillInTable);\n&quot;</span>);</pre>
<p>The view is added to the Web page as <tt>view</tt>, and the connection code we evaluate performs a signal-slot connection from the view's <tt>rowSelected()</tt> signal to a pure JavaScript function:</p>
<pre class="js"> <span class="name">view</span>.<span class="name">rowSelected</span>.<span class="name">connect</span>(<span class="name">fillInTable</span>);</pre>
<p><tt>fillInTable</tt> is the name of the JavaScript function to modify the form's input elements. This function expects three arguments: the name, address and quantity values for a row of data.</p>
<p>Whenever the current row changes in the <tt>view</tt> object, the <tt>exportRow()</tt> slot is called, the data found in the selected row is extracted from the model and emitted in the <tt>rowSelected()</tt> signal as three strings, and the above connection ensures that <tt>fillInTable()</tt> will be called with the current items of data. The appropriate type conversions occur behind the scenes to ensure that each <a href="qstring.html">QString</a> is converted to a JavaScript string object.</p>
<p>The rest of the function is the same as in the previous example:</p>
<pre class="cpp">     <span class="type"><a href="qnetworkrequest.html">QNetworkRequest</a></span> request(url);
     <span class="type"><a href="qnetworkreply.html">QNetworkReply</a></span> <span class="operator">*</span>reply <span class="operator">=</span> manager<span class="operator">-</span><span class="operator">&gt;</span>get(request);
     connect(reply<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> view<span class="operator">,</span> SLOT(updateModel()));
     connect(reply<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> reply<span class="operator">,</span> SLOT(deleteLater()));

     <span class="keyword">return</span> view;
 }</pre>
<p>We now give the JavaScript <tt>fillInForm()</tt> function to show what it does with the strings it is given. The function itself is defined in the HTML page header:</p>
<pre class="cpp"> &lt;script type=&quot;text/javascript&quot;&gt;
 function fillInTable(name, address, quantity)
 {
     var nameElement = document.getElementById(&quot;customers_name&quot;);
     var addressElement = document.getElementById(&quot;customers_address&quot;);
     var quantityElement = document.getElementById(&quot;customers_quantity&quot;);

     nameElement.innerHTML = name;
     addressElement.innerHTML = address;
     quantityElement.innerHTML = quantity;
 }
 &lt;/script&gt;</pre>
<p>We obtain the elements in the page that we wish to update by using the HTML Document Object Model (DOM) API. The values of these elements are updated with the <tt>name</tt>, <tt>address</tt> and <tt>quantity</tt> strings supplied.</p>
<a name="linking-things-together"></a>
<h2>Linking Things Together</h2>
<p>Although we have used the widgets to demonstrate the use of signals and slots for communication between Qt components and JavaScript in the browser, we do not need to embed widgets in Web pages to be able to do this. By inserting objects into pages and evaluating JavaScript, Qt applications can be made to examine and process information found online.</p>
<p>One additional improvement that can be made to this example is to create a relation between the embedded widget and the table to be updated. We could do this by including <tt>&lt;param&gt;</tt> elements within the <tt>&lt;object&gt;</tt> element that refers to the table cells by their <tt>id</tt> attributes. This would help us to avoid hard-coding the <tt>customers_name</tt>, <tt>customers_address</tt> and <tt>customers_quantity</tt> identifiers in the script.</p>
</div>
<!-- @@@webkit/webplugin -->
  <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>