Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 4124

qtbase5-doc-5.9.4-1.1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- dockwidgets.qdoc -->
  <title>Dock Widgets Example | Qt Widgets 5.9</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 >Qt 5.9</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="examples-mainwindow.html">Main Window Examples</a></td><td >Dock Widgets Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 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="#mainwindow-class-definition">MainWindow Class Definition</a></li>
<li class="level1"><a href="#mainwindow-class-implementation">MainWindow Class Implementation</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Dock Widgets Example</h1>
<span class="subtitle"></span>
<!-- $$$mainwindows/dockwidgets-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/dockwidgets-example.png" alt="Screenshot of the Dock Widgets example" /></p><p>The application presents a simple business letter template, and has a list of customer names and addresses and a list of standard phrases in two dock windows. The user can click a customer to have their name and address inserted into the template, and click one or more of the standard phrases. Errors can be corrected by clicking the Undo button. Once the letter has been prepared it can be printed or saved as HTML.</p>
<a name="mainwindow-class-definition"></a>
<h2 id="mainwindow-class-definition">MainWindow Class Definition</h2>
<p>Here's the class definition:</p>
<pre class="cpp">

  <span class="keyword">class</span> MainWindow : <span class="keyword">public</span> <span class="type"><a href="qmainwindow.html">QMainWindow</a></span>
  {
      Q_OBJECT

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

  <span class="keyword">private</span> <span class="keyword">slots</span>:
      <span class="type">void</span> newLetter();
      <span class="type">void</span> save();
      <span class="type">void</span> print();
      <span class="type">void</span> undo();
      <span class="type">void</span> about();
      <span class="type">void</span> insertCustomer(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>customer);
      <span class="type">void</span> addParagraph(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>paragraph);

  <span class="keyword">private</span>:
      <span class="type">void</span> createActions();
      <span class="type">void</span> createStatusBar();
      <span class="type">void</span> createDockWindows();

      <span class="type"><a href="qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>textEdit;
      <span class="type"><a href="qlistwidget.html">QListWidget</a></span> <span class="operator">*</span>customerList;
      <span class="type"><a href="qlistwidget.html">QListWidget</a></span> <span class="operator">*</span>paragraphsList;

      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>viewMenu;
  };

</pre>
<p>We will now review each function in turn.</p>
<a name="mainwindow-class-implementation"></a>
<h2 id="mainwindow-class-implementation">MainWindow Class Implementation</h2>
<pre class="cpp">

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>
  <span class="preprocessor">#if defined(QT_PRINTSUPPORT_LIB)</span>
  <span class="preprocessor">#include &lt;QtPrintSupport/qtprintsupportglobal.h&gt;</span>
  <span class="preprocessor">#if QT_CONFIG(printdialog)</span>
  <span class="preprocessor">#include &lt;QtPrintSupport&gt;</span>
  <span class="preprocessor">#endif</span>
  <span class="preprocessor">#endif</span>

  <span class="preprocessor">#include &quot;mainwindow.h&quot;</span>

</pre>
<p>We start by including <code>&lt;QtWidgets&gt;</code>, a header file that contains the definition of all classes in the Qt Core, Qt GUI and Qt Widgets modules. This saves us from having to include every class individually and is especially convenient if we add new widgets. We also include <code>mainwindow.h</code>.</p>
<pre class="cpp">

  MainWindow<span class="operator">::</span>MainWindow()
      : textEdit(<span class="keyword">new</span> <span class="type"><a href="qtextedit.html">QTextEdit</a></span>)
  {
      setCentralWidget(textEdit);

      createActions();
      createStatusBar();
      createDockWindows();

      setWindowTitle(tr(<span class="string">&quot;Dock Widgets&quot;</span>));

      newLetter();
      setUnifiedTitleAndToolBarOnMac(<span class="keyword">true</span>);
  }

</pre>
<p>In the constructor, we start by creating a <a href="qtextedit.html">QTextEdit</a> widget. Then we call <a href="qmainwindow.html#setCentralWidget">QMainWindow::setCentralWidget</a>(). This function passes ownership of the <a href="qtextedit.html">QTextEdit</a> to the <code>MainWindow</code> and tells the <code>MainWindow</code> that the <a href="qtextedit.html">QTextEdit</a> will occupy the <code>MainWindow</code>'s central area.</p>
<p>Then we call <code>createActions()</code>, <code>createMenus()</code>, <code>createToolBars()</code>, <code>createStatusBar()</code>, and <code>createDockWindows()</code> to set up the user interface. Finally we call <code>setWindowTitle()</code> to give the application a title, and <code>newLetter()</code> to create a new letter template.</p>
<p>We won't quote the <code>createActions()</code>, <code>createMenus()</code>, <code>createToolBars()</code>, and <code>createStatusBar()</code> functions since they follow the same pattern as all the other Qt examples.</p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>createDockWindows()
  {
      <span class="type"><a href="qdockwidget.html">QDockWidget</a></span> <span class="operator">*</span>dock <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdockwidget.html">QDockWidget</a></span>(tr(<span class="string">&quot;Customers&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      dock<span class="operator">-</span><span class="operator">&gt;</span>setAllowedAreas(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftDockWidgetArea <span class="operator">|</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RightDockWidgetArea);
      customerList <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlistwidget.html">QListWidget</a></span>(dock);
      customerList<span class="operator">-</span><span class="operator">&gt;</span>addItems(<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>()
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;John Doe, Harmony Enterprises, 12 Lakeside, Ambleton&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Jane Doe, Memorabilia, 23 Watersedge, Beaton&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Tammy Shea, Tiblanka, 38 Sea Views, Carlton&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Sally Hobart, Tiroli Tea, 67 Long River, Fedula&quot;</span>);
      dock<span class="operator">-</span><span class="operator">&gt;</span>setWidget(customerList);
      addDockWidget(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RightDockWidgetArea<span class="operator">,</span> dock);
      viewMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(dock<span class="operator">-</span><span class="operator">&gt;</span>toggleViewAction());

      dock <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdockwidget.html">QDockWidget</a></span>(tr(<span class="string">&quot;Paragraphs&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      paragraphsList <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlistwidget.html">QListWidget</a></span>(dock);
      paragraphsList<span class="operator">-</span><span class="operator">&gt;</span>addItems(<span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span>()
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Thank you for your payment which we have received today.&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Your order has been dispatched and should be with you &quot;</span>
                 <span class="string">&quot;within 28 days.&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;We have dispatched those items that were in stock. The &quot;</span>
                 <span class="string">&quot;rest of your order will be dispatched once all the &quot;</span>
                 <span class="string">&quot;remaining items have arrived at our warehouse. No &quot;</span>
                 <span class="string">&quot;additional shipping charges will be made.&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;You made a small overpayment (less than $5) which we &quot;</span>
                 <span class="string">&quot;will keep on account for you, or return at your request.&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;You made a small underpayment (less than $1), but we have &quot;</span>
                 <span class="string">&quot;sent your order anyway. We'll add this underpayment to &quot;</span>
                 <span class="string">&quot;your next bill.&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Unfortunately you did not send enough money. Please remit &quot;</span>
                 <span class="string">&quot;an additional $. Your order will be dispatched as soon as &quot;</span>
                 <span class="string">&quot;the complete amount has been received.&quot;</span>
              <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;You made an overpayment (more than $5). Do you wish to &quot;</span>
                 <span class="string">&quot;buy more items, or should we return the excess to you?&quot;</span>);
      dock<span class="operator">-</span><span class="operator">&gt;</span>setWidget(paragraphsList);
      addDockWidget(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RightDockWidgetArea<span class="operator">,</span> dock);
      viewMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(dock<span class="operator">-</span><span class="operator">&gt;</span>toggleViewAction());

      connect(customerList<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qlistwidget.html">QListWidget</a></span><span class="operator">::</span>currentTextChanged<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>insertCustomer);
      connect(paragraphsList<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qlistwidget.html">QListWidget</a></span><span class="operator">::</span>currentTextChanged<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>addParagraph);
  }

</pre>
<p>We create the customers dock window first, and in addition to a window title, we also pass it a <code>this</code> pointer so that it becomes a child of <code>MainWindow</code>. Normally we don't have to pass a parent because widgets are parented automatically when they are laid out: but dock windows aren't laid out using layouts.</p>
<p>We've chosen to restrict the customers dock window to the left and right dock areas. (So the user cannot drag the dock window to the top or bottom dock areas.) The user can drag the dock window out of the dock areas entirely so that it becomes a free floating window. We can change this (and whether the dock window is moveable or closable) using <a href="qdockwidget.html#features-prop">QDockWidget::setFeatures</a>().</p>
<p>Once we've created the dock window we create a list widget with the dock window as parent, then we populate the list and make it the dock window's widget. Finally we add the dock widget to the <code>MainWindow</code> using <code>addDockWidget()</code>, choosing to put it in the right dock area.</p>
<p>We undertake a similar process for the paragraphs dock window, except that we don't restrict which dock areas it can be dragged to.</p>
<p>Finally we set up the signal-slot connections. If the user clicks a customer or a paragraph their <code>currentTextChanged()</code> signal will be emitted and we connect these to <code>insertCustomer()</code> and addParagraph() passing the text that was clicked.</p>
<p>We briefly discuss the rest of the implementation, but have now covered everything relating to dock windows.</p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>newLetter()
  {
      textEdit<span class="operator">-</span><span class="operator">&gt;</span>clear();

      <span class="type"><a href="../qtgui/qtextcursor.html">QTextCursor</a></span> cursor(textEdit<span class="operator">-</span><span class="operator">&gt;</span>textCursor());
      cursor<span class="operator">.</span>movePosition(<span class="type"><a href="../qtgui/qtextcursor.html">QTextCursor</a></span><span class="operator">::</span>Start);
      <span class="type"><a href="../qtgui/qtextframe.html">QTextFrame</a></span> <span class="operator">*</span>topFrame <span class="operator">=</span> cursor<span class="operator">.</span>currentFrame();
      <span class="type"><a href="../qtgui/qtextframeformat.html">QTextFrameFormat</a></span> topFrameFormat <span class="operator">=</span> topFrame<span class="operator">-</span><span class="operator">&gt;</span>frameFormat();
      topFrameFormat<span class="operator">.</span>setPadding(<span class="number">16</span>);
      topFrame<span class="operator">-</span><span class="operator">&gt;</span>setFrameFormat(topFrameFormat);

      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> textFormat;
      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> boldFormat;
      boldFormat<span class="operator">.</span>setFontWeight(<span class="type"><a href="../qtgui/qfont.html">QFont</a></span><span class="operator">::</span>Bold);
      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> italicFormat;
      italicFormat<span class="operator">.</span>setFontItalic(<span class="keyword">true</span>);

      <span class="type"><a href="../qtgui/qtexttableformat.html">QTextTableFormat</a></span> tableFormat;
      tableFormat<span class="operator">.</span>setBorder(<span class="number">1</span>);
      tableFormat<span class="operator">.</span>setCellPadding(<span class="number">16</span>);
      tableFormat<span class="operator">.</span>setAlignment(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignRight);
      cursor<span class="operator">.</span>insertTable(<span class="number">1</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> tableFormat);
      cursor<span class="operator">.</span>insertText(<span class="string">&quot;The Firm&quot;</span><span class="operator">,</span> boldFormat);
      cursor<span class="operator">.</span>insertBlock();
      cursor<span class="operator">.</span>insertText(<span class="string">&quot;321 City Street&quot;</span><span class="operator">,</span> textFormat);
      cursor<span class="operator">.</span>insertBlock();
      cursor<span class="operator">.</span>insertText(<span class="string">&quot;Industry Park&quot;</span>);
      cursor<span class="operator">.</span>insertBlock();
      cursor<span class="operator">.</span>insertText(<span class="string">&quot;Some Country&quot;</span>);
      cursor<span class="operator">.</span>setPosition(topFrame<span class="operator">-</span><span class="operator">&gt;</span>lastPosition());
      cursor<span class="operator">.</span>insertText(<span class="type"><a href="../qtcore/qdate.html">QDate</a></span><span class="operator">::</span>currentDate()<span class="operator">.</span>toString(<span class="string">&quot;d MMMM yyyy&quot;</span>)<span class="operator">,</span> textFormat);
      cursor<span class="operator">.</span>insertBlock();
      cursor<span class="operator">.</span>insertBlock();
      cursor<span class="operator">.</span>insertText(<span class="string">&quot;Dear &quot;</span><span class="operator">,</span> textFormat);
      cursor<span class="operator">.</span>insertText(<span class="string">&quot;NAME&quot;</span><span class="operator">,</span> italicFormat);
      cursor<span class="operator">.</span>insertText(<span class="string">&quot;,&quot;</span><span class="operator">,</span> textFormat);
      <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> <span class="number">3</span>; <span class="operator">+</span><span class="operator">+</span>i)
          cursor<span class="operator">.</span>insertBlock();
      cursor<span class="operator">.</span>insertText(tr(<span class="string">&quot;Yours sincerely,&quot;</span>)<span class="operator">,</span> textFormat);
      <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> <span class="number">3</span>; <span class="operator">+</span><span class="operator">+</span>i)
          cursor<span class="operator">.</span>insertBlock();
      cursor<span class="operator">.</span>insertText(<span class="string">&quot;The Boss&quot;</span><span class="operator">,</span> textFormat);
      cursor<span class="operator">.</span>insertBlock();
      cursor<span class="operator">.</span>insertText(<span class="string">&quot;ADDRESS&quot;</span><span class="operator">,</span> italicFormat);
  }

</pre>
<p>In this function we clear the <a href="qtextedit.html">QTextEdit</a> so that it is empty. Next we create a <a href="../qtgui/qtextcursor.html">QTextCursor</a> on the <a href="qtextedit.html">QTextEdit</a>. We move the cursor to the start of the document and create and format a frame. We then create some character formats and a table format. We insert a table into the document and insert the company's name and address into a table using the table and character formats we created earlier. Then we insert the skeleton of the letter including two markers <code>NAME</code> and <code>ADDRESS</code>. We will also use the <code>Yours sincerely,</code> text as a marker.</p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>insertCustomer(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>customer)
  {
      <span class="keyword">if</span> (customer<span class="operator">.</span>isEmpty())
          <span class="keyword">return</span>;
      <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> customerList <span class="operator">=</span> customer<span class="operator">.</span>split(<span class="string">&quot;, &quot;</span>);
      <span class="type"><a href="../qtgui/qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>document <span class="operator">=</span> textEdit<span class="operator">-</span><span class="operator">&gt;</span>document();
      <span class="type"><a href="../qtgui/qtextcursor.html">QTextCursor</a></span> cursor <span class="operator">=</span> document<span class="operator">-</span><span class="operator">&gt;</span>find(<span class="string">&quot;NAME&quot;</span>);
      <span class="keyword">if</span> (<span class="operator">!</span>cursor<span class="operator">.</span>isNull()) {
          cursor<span class="operator">.</span>beginEditBlock();
          cursor<span class="operator">.</span>insertText(customerList<span class="operator">.</span>at(<span class="number">0</span>));
          <span class="type"><a href="../qtgui/qtextcursor.html">QTextCursor</a></span> oldcursor <span class="operator">=</span> cursor;
          cursor <span class="operator">=</span> document<span class="operator">-</span><span class="operator">&gt;</span>find(<span class="string">&quot;ADDRESS&quot;</span>);
          <span class="keyword">if</span> (<span class="operator">!</span>cursor<span class="operator">.</span>isNull()) {
              <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">1</span>; i <span class="operator">&lt;</span> customerList<span class="operator">.</span>size(); <span class="operator">+</span><span class="operator">+</span>i) {
                  cursor<span class="operator">.</span>insertBlock();
                  cursor<span class="operator">.</span>insertText(customerList<span class="operator">.</span>at(i));
              }
              cursor<span class="operator">.</span>endEditBlock();
          }
          <span class="keyword">else</span>
              oldcursor<span class="operator">.</span>endEditBlock();
      }
  }

</pre>
<p>If the user clicks a customer we split the customer details into pieces. We then look for the <code>NAME</code> marker using the <code>find()</code> function. This function selects the text it finds, so when we call <code>insertText()</code> with the customer's name the name replaces the marker. We then look for the <code>ADDRESS</code> marker and replace it with each line of the customer's address. Notice that we wrapped all the insertions between a <code>beginEditBlock()</code> and <code>endEditBlock()</code> pair. This means that the entire name and address insertion is treated as a single operation by the <a href="qtextedit.html">QTextEdit</a>, so a single undo will revert all the insertions.</p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>addParagraph(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>paragraph)
  {
      <span class="keyword">if</span> (paragraph<span class="operator">.</span>isEmpty())
          <span class="keyword">return</span>;
      <span class="type"><a href="../qtgui/qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>document <span class="operator">=</span> textEdit<span class="operator">-</span><span class="operator">&gt;</span>document();
      <span class="type"><a href="../qtgui/qtextcursor.html">QTextCursor</a></span> cursor <span class="operator">=</span> document<span class="operator">-</span><span class="operator">&gt;</span>find(tr(<span class="string">&quot;Yours sincerely,&quot;</span>));
      <span class="keyword">if</span> (cursor<span class="operator">.</span>isNull())
          <span class="keyword">return</span>;
      cursor<span class="operator">.</span>beginEditBlock();
      cursor<span class="operator">.</span>movePosition(<span class="type"><a href="../qtgui/qtextcursor.html">QTextCursor</a></span><span class="operator">::</span>PreviousBlock<span class="operator">,</span> <span class="type"><a href="../qtgui/qtextcursor.html">QTextCursor</a></span><span class="operator">::</span>MoveAnchor<span class="operator">,</span> <span class="number">2</span>);
      cursor<span class="operator">.</span>insertBlock();
      cursor<span class="operator">.</span>insertText(paragraph);
      cursor<span class="operator">.</span>insertBlock();
      cursor<span class="operator">.</span>endEditBlock();

  }

</pre>
<p>This function works in a similar way to <code>insertCustomer()</code>. First we look for the marker, in this case, <code>Yours sincerely,</code>, and then replace it with the standard paragraph that the user clicked. Again we use a <code>beginEditBlock()</code> ..&#x2e; <code>endEditBlock()</code> pair so that the insertion can be undone as a single operation.</p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>print()
  {
  <span class="preprocessor">#if QT_CONFIG(printdialog)</span>
      <span class="type"><a href="../qtgui/qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>document <span class="operator">=</span> textEdit<span class="operator">-</span><span class="operator">&gt;</span>document();
      <span class="type">QPrinter</span> printer;

      <span class="type">QPrintDialog</span> dlg(<span class="operator">&amp;</span>printer<span class="operator">,</span> <span class="keyword">this</span>);
      <span class="keyword">if</span> (dlg<span class="operator">.</span>exec() <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>Accepted) {
          <span class="keyword">return</span>;
      }

      document<span class="operator">-</span><span class="operator">&gt;</span>print(<span class="operator">&amp;</span>printer);
      statusBar()<span class="operator">-</span><span class="operator">&gt;</span>showMessage(tr(<span class="string">&quot;Ready&quot;</span>)<span class="operator">,</span> <span class="number">2000</span>);
  <span class="preprocessor">#endif</span>
  }

</pre>
<p>Qt's <a href="../qtgui/qtextdocument.html">QTextDocument</a> class makes printing documents easy. We simply take the <a href="qtextedit.html">QTextEdit</a>'s <a href="../qtgui/qtextdocument.html">QTextDocument</a>, set up the printer and print the document.</p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>save()
  {
      <span class="type"><a href="../qtcore/qmimedatabase.html">QMimeDatabase</a></span> mimeDatabase;
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileName <span class="operator">=</span> <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>getSaveFileName(<span class="keyword">this</span><span class="operator">,</span>
                          tr(<span class="string">&quot;Choose a file name&quot;</span>)<span class="operator">,</span> <span class="string">&quot;.&quot;</span><span class="operator">,</span>
                          mimeDatabase<span class="operator">.</span>mimeTypeForName(<span class="string">&quot;text/html&quot;</span>)<span class="operator">.</span>filterString());
      <span class="keyword">if</span> (fileName<span class="operator">.</span>isEmpty())
          <span class="keyword">return</span>;
      <span class="type"><a href="../qtcore/qfile.html">QFile</a></span> file(fileName);
      <span class="keyword">if</span> (<span class="operator">!</span>file<span class="operator">.</span>open(<span class="type"><a href="../qtcore/qfile.html">QFile</a></span><span class="operator">::</span>WriteOnly <span class="operator">|</span> <span class="type"><a href="../qtcore/qfile.html">QFile</a></span><span class="operator">::</span>Text)) {
          <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>warning(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Dock Widgets&quot;</span>)<span class="operator">,</span>
                               tr(<span class="string">&quot;Cannot write file %1:\n%2.&quot;</span>)
                               <span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName)<span class="operator">,</span> file<span class="operator">.</span>errorString()));
          <span class="keyword">return</span>;
      }

      <span class="type"><a href="../qtcore/qtextstream.html">QTextStream</a></span> out(<span class="operator">&amp;</span>file);
      <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>setOverrideCursor(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>WaitCursor);
      out <span class="operator">&lt;</span><span class="operator">&lt;</span> textEdit<span class="operator">-</span><span class="operator">&gt;</span>toHtml();
      <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>restoreOverrideCursor();

      statusBar()<span class="operator">-</span><span class="operator">&gt;</span>showMessage(tr(<span class="string">&quot;Saved '%1'&quot;</span>)<span class="operator">.</span>arg(fileName)<span class="operator">,</span> <span class="number">2000</span>);
  }

</pre>
<p><a href="qtextedit.html">QTextEdit</a> can output its contents in HTML format, so we prompt the user for the name of an HTML file and if they provide one we simply write the <a href="qtextedit.html">QTextEdit</a>'s contents in HTML format to the file.</p>
<pre class="cpp">

  <span class="type">void</span> MainWindow<span class="operator">::</span>undo()
  {
      <span class="type"><a href="../qtgui/qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>document <span class="operator">=</span> textEdit<span class="operator">-</span><span class="operator">&gt;</span>document();
      document<span class="operator">-</span><span class="operator">&gt;</span>undo();
  }

</pre>
<p>If the focus is in the <a href="qtextedit.html">QTextEdit</a>, pressing <b>Ctrl+Z</b> undoes as expected. But for the user's convenience we provide an application-wide undo function that simply calls the <a href="qtextedit.html">QTextEdit</a>'s undo: this means that the user can undo regardless of where the focus is in the application.</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-mainwindows-dockwidgets-mainwindow-cpp.html">mainwindows/dockwidgets/mainwindow.cpp</a></li>
<li><a href="qtwidgets-mainwindows-dockwidgets-mainwindow-h.html">mainwindows/dockwidgets/mainwindow.h</a></li>
<li><a href="qtwidgets-mainwindows-dockwidgets-main-cpp.html">mainwindows/dockwidgets/main.cpp</a></li>
<li><a href="qtwidgets-mainwindows-dockwidgets-dockwidgets-pro.html">mainwindows/dockwidgets/dockwidgets.pro</a></li>
<li><a href="qtwidgets-mainwindows-dockwidgets-dockwidgets-qrc.html">mainwindows/dockwidgets/dockwidgets.qrc</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/mainwindows/dockwidgets/images/new.png">mainwindows/dockwidgets/images/new.png</a></li>
<li><a href="images/used-in-examples/mainwindows/dockwidgets/images/print.png">mainwindows/dockwidgets/images/print.png</a></li>
<li><a href="images/used-in-examples/mainwindows/dockwidgets/images/save.png">mainwindows/dockwidgets/images/save.png</a></li>
<li><a href="images/used-in-examples/mainwindows/dockwidgets/images/undo.png">mainwindows/dockwidgets/images/undo.png</a></li>
</ul>
</div>
<!-- @@@mainwindows/dockwidgets -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 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>