<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>screenshot.cpp Example File | Qt Widgets 5.12.6</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.12</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-desktop-screenshot-example.html">Screenshot Example</a></td><td >screenshot.cpp Example File</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">screenshot.cpp Example File</h1> <span class="subtitle">desktop/screenshot/screenshot.cpp</span> <!-- $$$desktop/screenshot/screenshot.cpp-description --> <div class="descr"> <a name="details"></a> <pre class="cpp"> <span class="comment">/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** BSD License Usage ** Alternatively, you may use this file under the terms of the BSD license ** as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of The Qt Company Ltd nor the names of its ** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/</span> <span class="preprocessor">#include <QtWidgets></span> <span class="preprocessor">#include "screenshot.h"</span> 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">></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">></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">></span>screenGeometry(<span class="keyword">this</span>); screenshotLabel<span class="operator">-</span><span class="operator">></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">></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">"Options"</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">></span>setSuffix(tr(<span class="string">" s"</span>)); delaySpinBox<span class="operator">-</span><span class="operator">></span>setMaximum(<span class="number">60</span>); connect(delaySpinBox<span class="operator">,</span> <span class="type">QOverload</span><span class="operator"><</span><span class="type">int</span><span class="operator">></span><span class="operator">::</span>of(<span class="operator">&</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">&</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">"Hide This Window"</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">></span>addWidget(<span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"Screenshot Delay:"</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">></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">></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">></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">"New Screenshot"</span>)<span class="operator">,</span> <span class="keyword">this</span>); connect(newScreenshotButton<span class="operator">,</span> <span class="operator">&</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">&</span>Screenshot<span class="operator">::</span>newScreenshot); buttonsLayout<span class="operator">-</span><span class="operator">></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">"Save Screenshot"</span>)<span class="operator">,</span> <span class="keyword">this</span>); connect(saveScreenshotButton<span class="operator">,</span> <span class="operator">&</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">&</span>Screenshot<span class="operator">::</span>saveScreenshot); buttonsLayout<span class="operator">-</span><span class="operator">></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">"Quit"</span>)<span class="operator">,</span> <span class="keyword">this</span>); quitScreenshotButton<span class="operator">-</span><span class="operator">></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">&</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">&</span><span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>close); buttonsLayout<span class="operator">-</span><span class="operator">></span>addWidget(quitScreenshotButton); buttonsLayout<span class="operator">-</span><span class="operator">></span>addStretch(); mainLayout<span class="operator">-</span><span class="operator">></span>addLayout(buttonsLayout); shootScreen(); delaySpinBox<span class="operator">-</span><span class="operator">></span>setValue(<span class="number">5</span>); setWindowTitle(tr(<span class="string">"Screenshot"</span>)); resize(<span class="number">300</span><span class="operator">,</span> <span class="number">200</span>); } <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">></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">></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">></span>pixmap()<span class="operator">-</span><span class="operator">></span>size()) updateScreenshotLabel(); } <span class="type">void</span> Screenshot<span class="operator">::</span>newScreenshot() { <span class="keyword">if</span> (hideThisWindowCheckBox<span class="operator">-</span><span class="operator">></span>isChecked()) hide(); newScreenshotButton<span class="operator">-</span><span class="operator">></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">></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">&</span>Screenshot<span class="operator">::</span>shootScreen); } <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">"png"</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">"/untitled."</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">"Save As"</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">&</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">"image/"</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">"Save Error"</span>)<span class="operator">,</span> tr(<span class="string">"The image could not be saved to \"%1\"."</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="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">></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">></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">></span>grabWindow(<span class="number">0</span>); updateScreenshotLabel(); newScreenshotButton<span class="operator">-</span><span class="operator">></span>setDisabled(<span class="keyword">false</span>); <span class="keyword">if</span> (hideThisWindowCheckBox<span class="operator">-</span><span class="operator">></span>isChecked()) show(); } <span class="type">void</span> Screenshot<span class="operator">::</span>updateCheckBox() { <span class="keyword">if</span> (delaySpinBox<span class="operator">-</span><span class="operator">></span>value() <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>) { hideThisWindowCheckBox<span class="operator">-</span><span class="operator">></span>setDisabled(<span class="keyword">true</span>); hideThisWindowCheckBox<span class="operator">-</span><span class="operator">></span>setChecked(<span class="keyword">false</span>); } <span class="keyword">else</span> { hideThisWindowCheckBox<span class="operator">-</span><span class="operator">></span>setDisabled(<span class="keyword">false</span>); } } <span class="type">void</span> Screenshot<span class="operator">::</span>updateScreenshotLabel() { screenshotLabel<span class="operator">-</span><span class="operator">></span>setPixmap(originalPixmap<span class="operator">.</span>scaled(screenshotLabel<span class="operator">-</span><span class="operator">></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> </div> <!-- @@@desktop/screenshot/screenshot.cpp --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 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>