Sophie

Sophie

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

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" />
<!-- screenshot.qdoc -->
  <title>Screenshot 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-desktop.html">Desktop Examples</a></td><td >Screenshot 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="#screenshot-class-definition">Screenshot Class Definition</a></li>
<li class="level1"><a href="#screenshot-class-implementation">Screenshot Class Implementation</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Screenshot Example</h1>
<span class="subtitle"></span>
<!-- $$$desktop/screenshot-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/screenshot-example.png" alt="" /></p><p>With the application the users can take a screenshot of their desktop. They are provided with a couple of options:</p>
<ul>
<li>Delaying the screenshot, giving them time to rearrange their desktop.</li>
<li>Hiding the application's window while the screenshot is taken.</li>
</ul>
<p>In addition the application allows the users to save their screenshot if they want to.</p>
<a name="screenshot-class-definition"></a>
<h2 id="screenshot-class-definition">Screenshot Class Definition</h2>
<pre class="cpp">

  <span class="keyword">class</span> Screenshot : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
  {
      Q_OBJECT

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

  <span class="keyword">protected</span>:
      <span class="type">void</span> resizeEvent(<span class="type"><a href="../qtgui/qresizeevent.html">QResizeEvent</a></span> <span class="operator">*</span>event) override;

  <span class="keyword">private</span> <span class="keyword">slots</span>:
      <span class="type">void</span> newScreenshot();
      <span class="type">void</span> saveScreenshot();
      <span class="type">void</span> shootScreen();
      <span class="type">void</span> updateCheckBox();

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

      <span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span> originalPixmap;

      <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>screenshotLabel;
      <span class="type"><a href="qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>delaySpinBox;
      <span class="type"><a href="qcheckbox.html">QCheckBox</a></span> <span class="operator">*</span>hideThisWindowCheckBox;
      <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>newScreenshotButton;
  };

</pre>
<p>The <code>Screenshot</code> class inherits <a href="qwidget.html">QWidget</a> and is the application's main widget. It displays the application options and a preview of the screenshot.</p>
<p>We reimplement the <a href="qwidget.html#resizeEvent">QWidget::resizeEvent</a>() function to make sure that the preview of the screenshot scales properly when the user resizes the application widget. We also need several private slots to facilitate the options:</p>
<ul>
<li>The <code>newScreenshot()</code> slot prepares a new screenshot.</li>
<li>The <code>saveScreenshot()</code> slot saves the last screenshot.</li>
<li>The <code>shootScreen()</code> slot takes the screenshot.</li>
<li>The <code>updateCheckBox()</code> slot enables or disables the <b>Hide This Window</b> option.</li>
</ul>
<p>We also declare the private function <code>updateScreenshotLabel()</code> which is called whenever a new screenshot is taken or when a resize event changes the size of the screenshot preview label.</p>
<p>In addition we need to store the screenshot's original pixmap. The reason is that when we display the preview of the screenshot, we need to scale its pixmap, storing the original we make sure that no data are lost in that process.</p>
<a name="screenshot-class-implementation"></a>
<h2 id="screenshot-class-implementation">Screenshot Class Implementation</h2>
<pre class="cpp">

  Screenshot<span class="operator">::</span>Screenshot()
      :  screenshotLabel(<span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(<span class="keyword">this</span>))
  {
      screenshotLabel<span class="operator">-</span><span class="operator">&gt;</span>setSizePolicy(<span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Expanding<span class="operator">,</span> <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span><span class="operator">::</span>Expanding);
      screenshotLabel<span class="operator">-</span><span class="operator">&gt;</span>setAlignment(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignCenter);

      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> screenGeometry <span class="operator">=</span> <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>desktop()<span class="operator">-</span><span class="operator">&gt;</span>screenGeometry(<span class="keyword">this</span>);
      screenshotLabel<span class="operator">-</span><span class="operator">&gt;</span>setMinimumSize(screenGeometry<span class="operator">.</span>width() <span class="operator">/</span> <span class="number">8</span><span class="operator">,</span> screenGeometry<span class="operator">.</span>height() <span class="operator">/</span> <span class="number">8</span>);

      <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>mainLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>(<span class="keyword">this</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(screenshotLabel);

      <span class="type"><a href="qgroupbox.html">QGroupBox</a></span> <span class="operator">*</span>optionsGroupBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgroupbox.html">QGroupBox</a></span>(tr(<span class="string">&quot;Options&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      delaySpinBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qspinbox.html">QSpinBox</a></span>(optionsGroupBox);
      delaySpinBox<span class="operator">-</span><span class="operator">&gt;</span>setSuffix(tr(<span class="string">&quot; s&quot;</span>));
      delaySpinBox<span class="operator">-</span><span class="operator">&gt;</span>setMaximum(<span class="number">60</span>);

      connect(delaySpinBox<span class="operator">,</span> <span class="type">QOverload</span><span class="operator">&lt;</span><span class="type">int</span><span class="operator">&gt;</span><span class="operator">::</span>of(<span class="operator">&amp;</span><span class="type"><a href="qspinbox.html">QSpinBox</a></span><span class="operator">::</span>valueChanged)<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Screenshot<span class="operator">::</span>updateCheckBox);

      hideThisWindowCheckBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcheckbox.html">QCheckBox</a></span>(tr(<span class="string">&quot;Hide This Window&quot;</span>)<span class="operator">,</span> optionsGroupBox);

      <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>optionsGroupBoxLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>(optionsGroupBox);
      optionsGroupBoxLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(<span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Screenshot Delay:&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>)<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>);
      optionsGroupBoxLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(delaySpinBox<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span>);
      optionsGroupBoxLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(hideThisWindowCheckBox<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>);

      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(optionsGroupBox);

      <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span> <span class="operator">*</span>buttonsLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span>;
      newScreenshotButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">&quot;New Screenshot&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      connect(newScreenshotButton<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qpushbutton.html">QPushButton</a></span><span class="operator">::</span>clicked<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Screenshot<span class="operator">::</span>newScreenshot);
      buttonsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(newScreenshotButton);
      <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>saveScreenshotButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">&quot;Save Screenshot&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      connect(saveScreenshotButton<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qpushbutton.html">QPushButton</a></span><span class="operator">::</span>clicked<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Screenshot<span class="operator">::</span>saveScreenshot);
      buttonsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(saveScreenshotButton);
      <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>quitScreenshotButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">&quot;Quit&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);
      quitScreenshotButton<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>CTRL <span class="operator">+</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Key_Q);
      connect(quitScreenshotButton<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qpushbutton.html">QPushButton</a></span><span class="operator">::</span>clicked<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>close);
      buttonsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(quitScreenshotButton);
      buttonsLayout<span class="operator">-</span><span class="operator">&gt;</span>addStretch();
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addLayout(buttonsLayout);

      shootScreen();
      delaySpinBox<span class="operator">-</span><span class="operator">&gt;</span>setValue(<span class="number">5</span>);

      setWindowTitle(tr(<span class="string">&quot;Screenshot&quot;</span>));
      resize(<span class="number">300</span><span class="operator">,</span> <span class="number">200</span>);
  }

</pre>
<p>In the constructor we first create the <a href="qlabel.html">QLabel</a> displaying the screenshot preview.</p>
<p>We set the <a href="qlabel.html">QLabel</a>'s size policy to be <a href="qsizepolicy.html#Policy-enum">QSizePolicy::Expanding</a> both horizontally and vertically. This means that the <a href="qlabel.html">QLabel</a>'s size hint is a sensible size, but the widget can be shrunk and still be useful. Also, the widget can make use of extra space, so it should get as much space as possible. Then we make sure the <a href="qlabel.html">QLabel</a> is aligned in the center of the <code>Screenshot</code> widget, and set its minimum size.</p>
<p>Next, we create a group box that will contain all of the options' widgets. Then we create a <a href="qspinbox.html">QSpinBox</a> and a <a href="qlabel.html">QLabel</a> for the <b>Screenshot Delay</b> option, and connect the spinbox to the <code>updateCheckBox()</code> slot. Finally, we create a <a href="qcheckbox.html">QCheckBox</a> for the <b>Hide This Window</b> option, add all the options' widgets to a <a href="qgridlayout.html">QGridLayout</a> installed on the group box.</p>
<p>We create the applications's buttons and the group box containing the application's options, and put it all into a main layout. Finally we take the initial screenshot, and set the initial delay and the window title, before we resize the widget to a suitable size depending on the screen geometry.</p>
<pre class="cpp">

  <span class="type">void</span> Screenshot<span class="operator">::</span>resizeEvent(<span class="type"><a href="../qtgui/qresizeevent.html">QResizeEvent</a></span> <span class="operator">*</span> <span class="comment">/* event */</span>)
  {
      <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> scaledSize <span class="operator">=</span> originalPixmap<span class="operator">.</span>size();
      scaledSize<span class="operator">.</span>scale(screenshotLabel<span class="operator">-</span><span class="operator">&gt;</span>size()<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>KeepAspectRatio);
      <span class="keyword">if</span> (<span class="operator">!</span>screenshotLabel<span class="operator">-</span><span class="operator">&gt;</span>pixmap() <span class="operator">|</span><span class="operator">|</span> scaledSize <span class="operator">!</span><span class="operator">=</span> screenshotLabel<span class="operator">-</span><span class="operator">&gt;</span>pixmap()<span class="operator">-</span><span class="operator">&gt;</span>size())
          updateScreenshotLabel();
  }

</pre>
<p>The <code>resizeEvent()</code> function is reimplemented to receive the resize events dispatched to the widget. The purpose is to scale the preview screenshot pixmap without deformation of its content, and also make sure that the application can be resized smoothly.</p>
<p>To achieve the first goal, we scale the screenshot pixmap using <a href="../qtcore/qt.html#AspectRatioMode-enum">Qt::KeepAspectRatio</a>. We scale the pixmap to a rectangle as large as possible inside the current size of the screenshot preview label, preserving the aspect ratio. This means that if the user resizes the application window in only one direction, the preview screenshot keeps the same size.</p>
<p>To reach our second goal, we make sure that the preview screenshot only is repainted (using the private <code>updateScreenshotLabel()</code> function) when it actually changes its size.</p>
<pre class="cpp">

  <span class="type">void</span> Screenshot<span class="operator">::</span>newScreenshot()
  {
      <span class="keyword">if</span> (hideThisWindowCheckBox<span class="operator">-</span><span class="operator">&gt;</span>isChecked())
          hide();
      newScreenshotButton<span class="operator">-</span><span class="operator">&gt;</span>setDisabled(<span class="keyword">true</span>);

      <span class="type"><a href="../qtcore/qtimer.html">QTimer</a></span><span class="operator">::</span>singleShot(delaySpinBox<span class="operator">-</span><span class="operator">&gt;</span>value() <span class="operator">*</span> <span class="number">1000</span><span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>Screenshot<span class="operator">::</span>shootScreen);
  }

</pre>
<p>The private <code>newScreenshot()</code> slot is called when the user requests a new screenshot; but the slot only prepares a new screenshot.</p>
<p>First we see if the <b>Hide This Window</b> option is checked, if it is we hide the <code>Screenshot</code> widget. Then we disable the <b>New Screenshot</b> button, to make sure the user only can request one screenshot at a time.</p>
<p>We create a timer using the <a href="../qtcore/qtimer.html">QTimer</a> class which provides repetitive and single-shot timers. We set the timer to time out only once, using the static <a href="../qtcore/qtimer.html#singleShot-4">QTimer::singleShot</a>() function. This function calls the private <code>shootScreen()</code> slot after the time interval specified by the <b>Screenshot Delay</b> option. It is <code>shootScreen()</code> that actually performs the screenshot.</p>
<pre class="cpp">

  <span class="type">void</span> Screenshot<span class="operator">::</span>saveScreenshot()
  {
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> format <span class="operator">=</span> <span class="string">&quot;png&quot;</span>;
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> initialPath <span class="operator">=</span> <span class="type"><a href="../qtcore/qstandardpaths.html">QStandardPaths</a></span><span class="operator">::</span>writableLocation(<span class="type"><a href="../qtcore/qstandardpaths.html">QStandardPaths</a></span><span class="operator">::</span>PicturesLocation);
      <span class="keyword">if</span> (initialPath<span class="operator">.</span>isEmpty())
          initialPath <span class="operator">=</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>currentPath();
      initialPath <span class="operator">+</span><span class="operator">=</span> tr(<span class="string">&quot;/untitled.&quot;</span>) <span class="operator">+</span> format;

      <span class="type"><a href="qfiledialog.html">QFileDialog</a></span> fileDialog(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Save As&quot;</span>)<span class="operator">,</span> initialPath);
      fileDialog<span class="operator">.</span>setAcceptMode(<span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>AcceptSave);
      fileDialog<span class="operator">.</span>setFileMode(<span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>AnyFile);
      fileDialog<span class="operator">.</span>setDirectory(initialPath);
      <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> mimeTypes;
      foreach (<span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> <span class="operator">&amp;</span>bf<span class="operator">,</span> <span class="type"><a href="../qtgui/qimagewriter.html">QImageWriter</a></span><span class="operator">::</span>supportedMimeTypes())
          mimeTypes<span class="operator">.</span>append(QLatin1String(bf));
      fileDialog<span class="operator">.</span>setMimeTypeFilters(mimeTypes);
      fileDialog<span class="operator">.</span>selectMimeTypeFilter(<span class="string">&quot;image/&quot;</span> <span class="operator">+</span> format);
      fileDialog<span class="operator">.</span>setDefaultSuffix(format);
      <span class="keyword">if</span> (fileDialog<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>;
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileName <span class="operator">=</span> fileDialog<span class="operator">.</span>selectedFiles()<span class="operator">.</span>first();
      <span class="keyword">if</span> (<span class="operator">!</span>originalPixmap<span class="operator">.</span>save(fileName)) {
          <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;Save Error&quot;</span>)<span class="operator">,</span> tr(<span class="string">&quot;The image could not be saved to \&quot;%1\&quot;.&quot;</span>)
                               <span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(fileName)));
      }
  }

</pre>
<p>The <code>saveScreenshot()</code> slot is called when the user push the <b>Save</b> button, and it presents a file dialog using the <a href="qfiledialog.html">QFileDialog</a> class.</p>
<p><a href="qfiledialog.html">QFileDialog</a> enables a user to traverse the file system in order to select one or many files or a directory. The easiest way to create a <a href="qfiledialog.html">QFileDialog</a> is to use the convenience static functions. Here, we instantiate the dialog on the stack in order to be able to set up the supported mime types of <a href="../qtgui/qimagewriter.html">QImageWriter</a>, allowing the user to save in a variety of formats.</p>
<p>We define the default file format to be png, and we make the file dialog's initial path the location of pictures as obtained from <a href="../qtcore/qstandardpaths.html">QStandardPaths</a>, defaulting to the path the application is run from.</p>
<p>We run the dialog by invoking <a href="qdialog.html#exec">QDialog::exec</a>() and return if the user canceled the dialog. If the dialog has been accepted, we obtain a file name by calling <a href="qfiledialog.html#selectedFiles">QFileDialog::selectedFiles</a>(). The file does not have to exist. If the file name is valid, we use the <a href="../qtgui/qpixmap.html#save-1">QPixmap::save</a>() function to save the screenshot's original pixmap in that file.</p>
<pre class="cpp">

  <span class="type">void</span> Screenshot<span class="operator">::</span>shootScreen()
  {
      <span class="type"><a href="../qtgui/qscreen.html">QScreen</a></span> <span class="operator">*</span>screen <span class="operator">=</span> <span class="type"><a href="../qtgui/qguiapplication.html">QGuiApplication</a></span><span class="operator">::</span>primaryScreen();
      <span class="keyword">if</span> (<span class="keyword">const</span> <span class="type"><a href="../qtgui/qwindow.html">QWindow</a></span> <span class="operator">*</span>window <span class="operator">=</span> windowHandle())
          screen <span class="operator">=</span> window<span class="operator">-</span><span class="operator">&gt;</span>screen();
      <span class="keyword">if</span> (<span class="operator">!</span>screen)
          <span class="keyword">return</span>;

      <span class="keyword">if</span> (delaySpinBox<span class="operator">-</span><span class="operator">&gt;</span>value() <span class="operator">!</span><span class="operator">=</span> <span class="number">0</span>)
          <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>beep();

      originalPixmap <span class="operator">=</span> screen<span class="operator">-</span><span class="operator">&gt;</span>grabWindow(<span class="number">0</span>);
      updateScreenshotLabel();

      newScreenshotButton<span class="operator">-</span><span class="operator">&gt;</span>setDisabled(<span class="keyword">false</span>);
      <span class="keyword">if</span> (hideThisWindowCheckBox<span class="operator">-</span><span class="operator">&gt;</span>isChecked())
          show();
  }

</pre>
<p>The <code>shootScreen()</code> slot is called to take the screenshot.</p>
<p>First, we find the instance of <a href="../qtgui/qscreen.html">QScreen</a> the window is located by retrieving the <a href="../qtgui/qwindow.html">QWindow</a> and its <a href="../qtgui/qscreen.html">QScreen</a>, defaulting to the primary screen. If no screen can be found, we return. Although this is unlikely to happen, applications should check for null pointers since there might be situations in which no screen is connected.</p>
<p>If the user has chosen to delay the screenshot, we make the application beep when the screenshot is taken using the static <a href="qapplication.html#beep">QApplication::beep</a>() function.</p>
<p>We then take the screenshot using the <a href="../qtgui/qscreen.html#grabWindow">QScreen::grabWindow</a>() function. The function grabs the contents of the window passed as an argument, makes a pixmap out of it and returns that pixmap. The window id can be obtained with <a href="qwidget.html#winId">QWidget::winId</a>() or <a href="../qtgui/qwindow.html#winId">QWindow::winId</a>(). Here, however, we just pass 0 as the window id, indicating that we want to grab the entire screen.</p>
<p>We update the screenshot preview label using the private <code>updateScreenshotLabel()</code> function. Then we enable the <b>New Screenshot</b> button, and finally we make the <code>Screenshot</code> widget visible if it was hidden during the screenshot.</p>
<pre class="cpp">

  <span class="type">void</span> Screenshot<span class="operator">::</span>updateCheckBox()
  {
      <span class="keyword">if</span> (delaySpinBox<span class="operator">-</span><span class="operator">&gt;</span>value() <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>) {
          hideThisWindowCheckBox<span class="operator">-</span><span class="operator">&gt;</span>setDisabled(<span class="keyword">true</span>);
          hideThisWindowCheckBox<span class="operator">-</span><span class="operator">&gt;</span>setChecked(<span class="keyword">false</span>);
      } <span class="keyword">else</span> {
          hideThisWindowCheckBox<span class="operator">-</span><span class="operator">&gt;</span>setDisabled(<span class="keyword">false</span>);
      }
  }

</pre>
<p>The <b>Hide This Window</b> option is enabled or disabled depending on the delay of the screenshot. If there is no delay, the application window cannot be hidden and the option's checkbox is disabled.</p>
<p>The <code>updateCheckBox()</code> slot is called whenever the user changes the delay using the <b>Screenshot Delay</b> option.</p>
<pre class="cpp">

  <span class="type">void</span> Screenshot<span class="operator">::</span>updateScreenshotLabel()
  {
      screenshotLabel<span class="operator">-</span><span class="operator">&gt;</span>setPixmap(originalPixmap<span class="operator">.</span>scaled(screenshotLabel<span class="operator">-</span><span class="operator">&gt;</span>size()<span class="operator">,</span>
                                                       <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>KeepAspectRatio<span class="operator">,</span>
                                                       <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>SmoothTransformation));
  }

</pre>
<p>The private <code>updateScreenshotLabel()</code> function is called whenever the screenshot changes, or when a resize event changes the size of the screenshot preview label. It updates the screenshot preview's label using the <a href="qlabel.html#pixmap-prop">QLabel::setPixmap</a>() and <a href="../qtgui/qpixmap.html#scaled">QPixmap::scaled</a>() functions.</p>
<p><a href="../qtgui/qpixmap.html#scaled">QPixmap::scaled</a>() returns a copy of the given pixmap scaled to a rectangle of the given size according to the given <a href="../qtcore/qt.html#AspectRatioMode-enum">Qt::AspectRatioMode</a> and <a href="../qtcore/qt.html#TransformationMode-enum">Qt::TransformationMode</a>.</p>
<p>We scale the original pixmap to fit the current screenshot label's size, preserving the aspect ratio and giving the resulting pixmap smoothed edges.</p>
<p>Files:</p>
<ul>
<li><a href="qtwidgets-desktop-screenshot-screenshot-cpp.html">desktop/screenshot/screenshot.cpp</a></li>
<li><a href="qtwidgets-desktop-screenshot-screenshot-h.html">desktop/screenshot/screenshot.h</a></li>
<li><a href="qtwidgets-desktop-screenshot-main-cpp.html">desktop/screenshot/main.cpp</a></li>
<li><a href="qtwidgets-desktop-screenshot-screenshot-pro.html">desktop/screenshot/screenshot.pro</a></li>
</ul>
</div>
<!-- @@@desktop/screenshot -->
        </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>