qtquickcontrols2-texteditor-example.html 16.3 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qtquickcontrols2-texteditor.qdoc -->
  <title>Qt Quick Controls 2 - Text Editor | Qt Quick Controls 2 5.11</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtquickcontrols2-index.html">Qt Quick Controls 2</a></td><td ><a href="qtquickcontrols2-examples.html">Qt Quick Controls 2 Examples</a></td><td >Qt Quick Controls 2 - Text Editor</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.11.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#desktop-user-interface">Desktop User Interface</a></li>
<li class="level1"><a href="#touch-user-interface">Touch User Interface</a></li>
<li class="level1"><a href="#c-backend">C++ Backend</a></li>
<li class="level1"><a href="#running-the-example">Running the Example</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Quick Controls 2 - Text Editor</h1>
<span class="subtitle"></span>
<!-- $$$texteditor-brief -->
<p>A QML app using Qt Quick Controls 2 and a C++ class to provide a fully-functional rich-text editor application.</p>
<!-- @@@texteditor -->
<!-- $$$texteditor-description -->
<div class="descr"> <a name="details"></a>
<p>The <i>Text Editor Example</i> presents a sample HTML file using the <a href="qml-qtquick-controls2-textarea.html">TextArea</a> control, preserving the HTML formatting. The application comes with two user interfaces; one for traditional desktop platforms with a mouse pointer, and another simpler, touch-oriented version.</p>
<a name="desktop-user-interface"></a>
<h2 id="desktop-user-interface">Desktop User Interface</h2>
<p class="centerAlign"><img src="images/qtquickcontrols2-texteditor-desktop.jpg" alt="" /></p><p>The desktop version is a complete text editor with capabilities for formatting text, and opening and saving HTML and plain text files. It demonstrates the native-looking dialogs and menus using the <a href="../qtlabsplatform/qtlabsplatform-index.html">Qt Labs Platform</a> module. These types are mostly suitable for desktop platforms with support for multiple top-level windows, a mouse pointer, and moderate screen size.</p>
<p>The desktop UI uses <a href="../qtquickdialogs/qml-qtquick-dialogs-filedialog.html">FileDialog</a> for opening and saving files:</p>
<pre class="cpp">

      <span class="type"><a href="../qtquickdialogs/qml-qtquick-dialogs-filedialog.html">FileDialog</a></span> {
          <span class="name">id</span>: <span class="name">openDialog</span>
          <span class="name">fileMode</span>: <span class="name">FileDialog</span>.<span class="name">OpenFile</span>
          <span class="name">selectedNameFilter</span>.index: <span class="number">1</span>
          <span class="name">nameFilters</span>: [<span class="string">&quot;Text files (*.txt)&quot;</span>, <span class="string">&quot;HTML files (*.html *.htm)&quot;</span>]
          <span class="name">folder</span>: <span class="name">StandardPaths</span>.<span class="name">writableLocation</span>(<span class="name">StandardPaths</span>.<span class="name">DocumentsLocation</span>)
          <span class="name">onAccepted</span>: <span class="name">document</span>.<span class="name">load</span>(<span class="name">file</span>)
      }

      <span class="type"><a href="../qtquickdialogs/qml-qtquick-dialogs-filedialog.html">FileDialog</a></span> {
          <span class="name">id</span>: <span class="name">saveDialog</span>
          <span class="name">fileMode</span>: <span class="name">FileDialog</span>.<span class="name">SaveFile</span>
          <span class="name">defaultSuffix</span>: <span class="name">document</span>.<span class="name">fileType</span>
          <span class="name">nameFilters</span>: <span class="name">openDialog</span>.<span class="name">nameFilters</span>
          <span class="name">selectedNameFilter</span>.index: <span class="name">document</span>.<span class="name">fileType</span> <span class="operator">===</span> <span class="string">&quot;txt&quot;</span> ? <span class="number">0</span> : <span class="number">1</span>
          <span class="name">folder</span>: <span class="name">StandardPaths</span>.<span class="name">writableLocation</span>(<span class="name">StandardPaths</span>.<span class="name">DocumentsLocation</span>)
          <span class="name">onAccepted</span>: <span class="name">document</span>.<span class="name">saveAs</span>(<span class="name">file</span>)
      }

</pre>
<p>It uses <a href="../qtquickdialogs/qml-qtquick-dialogs-fontdialog.html">FontDialog</a> and <a href="../qtquickdialogs/qml-qtquick-dialogs-colordialog.html">ColorDialog</a> for choosing fonts and colors:</p>
<pre class="cpp">

      <span class="type"><a href="../qtquickdialogs/qml-qtquick-dialogs-fontdialog.html">FontDialog</a></span> {
          <span class="name">id</span>: <span class="name">fontDialog</span>
          <span class="name">onAccepted</span>: {
              <span class="name">document</span>.<span class="name">fontFamily</span> <span class="operator">=</span> <span class="name">font</span>.<span class="name">family</span>;
              <span class="name">document</span>.<span class="name">fontSize</span> <span class="operator">=</span> <span class="name">font</span>.<span class="name">pointSize</span>;
          }
      }

      <span class="type"><a href="../qtquickdialogs/qml-qtquick-dialogs-colordialog.html">ColorDialog</a></span> {
          <span class="name">id</span>: <span class="name">colorDialog</span>
          <span class="name">currentColor</span>: <span class="string">&quot;black&quot;</span>
      }

</pre>
<p>It also uses <a href="../qtlabsplatform/qml-qt-labs-platform-menu.html">Menu</a> and <a href="../qtlabsplatform/qml-qt-labs-platform-menuitem.html">MenuItem</a> that provide a context menu to format text within:</p>
<pre class="cpp">

      <span class="type"><a href="qml-qtquick-controls2-menu.html">Menu</a></span> {
          <span class="name">id</span>: <span class="name">contextMenu</span>

          <span class="type"><a href="qml-qtquick-controls2-menuitem.html">MenuItem</a></span> {
              <span class="name">text</span>: <span class="name">qsTr</span>(<span class="string">&quot;Copy&quot;</span>)
              <span class="name">enabled</span>: <span class="name">textArea</span>.<span class="name">selectedText</span>
              <span class="name">onTriggered</span>: <span class="name">textArea</span>.<span class="name">copy</span>()
          }
          <span class="type"><a href="qml-qtquick-controls2-menuitem.html">MenuItem</a></span> {
              <span class="name">text</span>: <span class="name">qsTr</span>(<span class="string">&quot;Cut&quot;</span>)
              <span class="name">enabled</span>: <span class="name">textArea</span>.<span class="name">selectedText</span>
              <span class="name">onTriggered</span>: <span class="name">textArea</span>.<span class="name">cut</span>()
          }
          <span class="type"><a href="qml-qtquick-controls2-menuitem.html">MenuItem</a></span> {
              <span class="name">text</span>: <span class="name">qsTr</span>(<span class="string">&quot;Paste&quot;</span>)
              <span class="name">enabled</span>: <span class="name">textArea</span>.<span class="name">canPaste</span>
              <span class="name">onTriggered</span>: <span class="name">textArea</span>.<span class="name">paste</span>()
          }

          <span class="type"><a href="qml-qtquick-controls2-menuseparator.html">MenuSeparator</a></span> {}

          <span class="type"><a href="qml-qtquick-controls2-menuitem.html">MenuItem</a></span> {
              <span class="name">text</span>: <span class="name">qsTr</span>(<span class="string">&quot;Font...&quot;</span>)
              <span class="name">onTriggered</span>: <span class="name">fontDialog</span>.<span class="name">open</span>()
          }

          <span class="type"><a href="qml-qtquick-controls2-menuitem.html">MenuItem</a></span> {
              <span class="name">text</span>: <span class="name">qsTr</span>(<span class="string">&quot;Color...&quot;</span>)
              <span class="name">onTriggered</span>: <span class="name">colorDialog</span>.<span class="name">open</span>()
          }
      }

</pre>
<p><b>Note: </b>There is also a standard menubar with more options than the context menu.</p><a name="touch-user-interface"></a>
<h2 id="touch-user-interface">Touch User Interface</h2>
<p class="centerAlign"><img src="images/qtquickcontrols2-texteditor-touch.jpg" alt="" /></p><p>The touch user interface is a simplified version of the text editor. It is suitable for touch devices with limited screen size. The example uses <a href="qtquickcontrols2-fileselectors.html">file selectors</a> to load the appropriate user interface automatically.</p>
<p>Unlike the desktop version, which uses top-level dialogs, the touch version uses the QML <a href="qml-qtquick-controls2-dialog.html">Dialog</a> type, which is not a top-level window. This type of dialog is fully supported on mobile and embedded platforms that do not support multiple top-level windows.</p>
<pre class="cpp">

      <span class="type"><a href="qml-qtquick-controls2-dialog.html">Dialog</a></span> {
          <span class="name">id</span>: <span class="name">aboutDialog</span>
          <span class="name">standardButtons</span>: <span class="name">Dialog</span>.<span class="name">Ok</span>
          <span class="name">modal</span>: <span class="number">true</span>
          <span class="name">x</span>: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">2</span> <span class="operator">-</span> <span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>
          <span class="name">y</span>: <span class="name">parent</span>.<span class="name">height</span> <span class="operator">/</span> <span class="number">2</span> <span class="operator">-</span> <span class="name">height</span> <span class="operator">/</span> <span class="number">2</span>

          <span class="name">contentItem</span>: <span class="name">Label</span> {
              <span class="name">text</span>: <span class="name">qsTr</span>(<span class="string">&quot;Qt Quick Controls 2 - Text Editor Example&quot;</span>)
          }
      }

</pre>
<a name="c-backend"></a>
<h2 id="c-backend">C++ Backend</h2>
<p>Both user interfaces use the same C++ backend, which supports opening, formatting, and editing a document. The C++ class, <code>DocumentHandler</code>, extends <a href="../qtcore/qobject.html">QObject</a> and is registered as a QML type under the namespace <code>io.qt.examples.texteditor 1.0</code>.</p>
<p>The following snippets show how the type is registered under a namespace and later imported and instantiated by <i>main.qml</i>. For more information about registering C++ classes as QML types, see <a href="../qtqml/qtqml-cppintegration-overview.html#defining-qml-types-from-c">Defining QML Types from C++</a>.</p>
<p>QML type registration:</p>
<pre class="cpp">

  <span class="preprocessor">#include &lt;QtQml/qqml.h&gt;</span>
  <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  qmlRegisterType<span class="operator">&lt;</span>DocumentHandler<span class="operator">&gt;</span>(<span class="string">&quot;io.qt.examples.texteditor&quot;</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="string">&quot;DocumentHandler&quot;</span>);
  <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>

</pre>
<p>QML namespace import:</p>
<pre class="cpp">

  import io<span class="operator">.</span>qt<span class="operator">.</span>examples<span class="operator">.</span>texteditor <span class="number">1.0</span>

</pre>
<p>QML instance:</p>
<pre class="cpp">

      <span class="type">DocumentHandler</span> {
          <span class="name">id</span>: <span class="name">document</span>
          <span class="name">document</span>: <span class="name">textArea</span>.<span class="name">textDocument</span>
          <span class="name">cursorPosition</span>: <span class="name">textArea</span>.<span class="name">cursorPosition</span>
          <span class="name">selectionStart</span>: <span class="name">textArea</span>.<span class="name">selectionStart</span>
          <span class="name">selectionEnd</span>: <span class="name">textArea</span>.<span class="name">selectionEnd</span>
          <span class="name">textColor</span>: <span class="name">colorDialog</span>.<span class="name">color</span>
          <span class="name">Component</span>.onCompleted: <span class="name">document</span>.<span class="name">load</span>(<span class="string">&quot;qrc:/texteditor.html&quot;</span>)
          <span class="name">onLoaded</span>: {
              <span class="name">textArea</span>.<span class="name">text</span> <span class="operator">=</span> <span class="name">text</span>
          }
          <span class="name">onError</span>: {
              <span class="name">errorDialog</span>.<span class="name">text</span> <span class="operator">=</span> <span class="name">message</span>
              <span class="name">errorDialog</span>.<span class="name">visible</span> <span class="operator">=</span> <span class="number">true</span>
          }
      }

</pre>
<a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<p>To run the example from <a href="http://doc.qt.io/qtcreator/index.html">Qt Creator</a>, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit <a href="http://doc.qt.io/qtcreator/creator-build-example-application.html">Building and Running an Example</a>.</p>
<p>Files:</p>
<ul>
<li><a href="qtquickcontrols2-texteditor-documenthandler-cpp.html">texteditor/documenthandler.cpp</a></li>
<li><a href="qtquickcontrols2-texteditor-documenthandler-h.html">texteditor/documenthandler.h</a></li>
<li><a href="qtquickcontrols2-texteditor-qml-touch-texteditor-qml.html">texteditor/qml/+touch/texteditor.qml</a></li>
<li><a href="qtquickcontrols2-texteditor-qml-texteditor-qml.html">texteditor/qml/texteditor.qml</a></li>
<li><a href="qtquickcontrols2-texteditor-qtquickcontrols2-conf.html">texteditor/qtquickcontrols2.conf</a></li>
<li><a href="qtquickcontrols2-texteditor-texteditor-cpp.html">texteditor/texteditor.cpp</a></li>
<li><a href="qtquickcontrols2-texteditor-texteditor-pro.html">texteditor/texteditor.pro</a></li>
<li><a href="qtquickcontrols2-texteditor-texteditor-qrc.html">texteditor/texteditor.qrc</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/texteditor/images/qt-logo.png">texteditor/images/qt-logo.png</a></li>
</ul>
</div>
<!-- @@@texteditor -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2018 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>