<?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>mainwindow.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-tools-codecs-example.html">Text Codecs Example</a></td><td >mainwindow.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">mainwindow.cpp Example File</h1> <span class="subtitle">tools/codecs/mainwindow.cpp</span> <!-- $$$tools/codecs/mainwindow.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 "mainwindow.h"</span> <span class="preprocessor">#include "encodingdialog.h"</span> <span class="preprocessor">#include "previewform.h"</span> MainWindow<span class="operator">::</span>MainWindow() { textEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qplaintextedit.html">QPlainTextEdit</a></span>; textEdit<span class="operator">-</span><span class="operator">></span>setLineWrapMode(<span class="type"><a href="qplaintextedit.html">QPlainTextEdit</a></span><span class="operator">::</span>NoWrap); setCentralWidget(textEdit); findCodecs(); previewForm <span class="operator">=</span> <span class="keyword">new</span> PreviewForm(<span class="keyword">this</span>); previewForm<span class="operator">-</span><span class="operator">></span>setCodecList(codecs); createMenus(); setWindowTitle(tr(<span class="string">"Codecs"</span>)); <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>); resize(screenGeometry<span class="operator">.</span>width() <span class="operator">/</span> <span class="number">2</span><span class="operator">,</span> screenGeometry<span class="operator">.</span>height() <span class="operator">*</span> <span class="number">2</span> <span class="operator">/</span> <span class="number">3</span>); } <span class="type">void</span> MainWindow<span class="operator">::</span>open() { <span class="keyword">const</span> <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>getOpenFileName(<span class="keyword">this</span>); <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>ReadOnly)) { <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">"Codecs"</span>)<span class="operator">,</span> tr(<span class="string">"Cannot read file %1:\n%2"</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="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> data <span class="operator">=</span> file<span class="operator">.</span>readAll(); previewForm<span class="operator">-</span><span class="operator">></span>setWindowTitle(tr(<span class="string">"Choose Encoding for %1"</span>)<span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qfileinfo.html">QFileInfo</a></span>(fileName)<span class="operator">.</span>fileName())); previewForm<span class="operator">-</span><span class="operator">></span>setEncodedData(data); <span class="keyword">if</span> (previewForm<span class="operator">-</span><span class="operator">></span>exec()) textEdit<span class="operator">-</span><span class="operator">></span>setPlainText(previewForm<span class="operator">-</span><span class="operator">></span>decodedString()); } <span class="type">void</span> MainWindow<span class="operator">::</span>save() { <span class="keyword">const</span> <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>action <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="keyword">const</span> <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span><span class="operator">></span>(sender()); <span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> codecName <span class="operator">=</span> action<span class="operator">-</span><span class="operator">></span>data()<span class="operator">.</span>toByteArray(); <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> title <span class="operator">=</span> tr(<span class="string">"Save As (%1)"</span>)<span class="operator">.</span>arg(QLatin1String(codecName)); <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> title); <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">"Codecs"</span>)<span class="operator">,</span> tr(<span class="string">"Cannot write file %1:\n%2"</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">&</span>file); out<span class="operator">.</span>setCodec(codecName<span class="operator">.</span>constData()); out <span class="operator"><</span><span class="operator"><</span> textEdit<span class="operator">-</span><span class="operator">></span>toPlainText(); } <span class="type">void</span> MainWindow<span class="operator">::</span>about() { <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>about(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">"About Codecs"</span>)<span class="operator">,</span> tr(<span class="string">"The <b>Codecs</b> example demonstrates how to read and write "</span> <span class="string">"files using various encodings."</span>)); } <span class="type">void</span> MainWindow<span class="operator">::</span>aboutToShowSaveAsMenu() { <span class="type"><a href="../qtcore/qstring.html">QString</a></span> currentText <span class="operator">=</span> textEdit<span class="operator">-</span><span class="operator">></span>toPlainText(); foreach (<span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>action<span class="operator">,</span> saveAsActs) { <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> codecName <span class="operator">=</span> action<span class="operator">-</span><span class="operator">></span>data()<span class="operator">.</span>toByteArray(); <span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span> <span class="operator">*</span>codec <span class="operator">=</span> <span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span><span class="operator">::</span>codecForName(codecName); action<span class="operator">-</span><span class="operator">></span>setVisible(codec <span class="operator">&</span><span class="operator">&</span> codec<span class="operator">-</span><span class="operator">></span>canEncode(currentText)); } } <span class="type">void</span> MainWindow<span class="operator">::</span>findCodecs() { <span class="type"><a href="../qtcore/qmap.html">QMap</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">,</span> <span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span> <span class="operator">*</span><span class="operator">></span> codecMap; <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span> iso8859RegExp(<span class="string">"^ISO[- ]8859-([0-9]+).*$"</span>); <span class="type"><a href="../qtcore/qregularexpressionmatch.html">QRegularExpressionMatch</a></span> match; foreach (<span class="type">int</span> mib<span class="operator">,</span> <span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span><span class="operator">::</span>availableMibs()) { <span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span> <span class="operator">*</span>codec <span class="operator">=</span> <span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span><span class="operator">::</span>codecForMib(mib); <span class="type"><a href="../qtcore/qstring.html">QString</a></span> sortKey <span class="operator">=</span> codec<span class="operator">-</span><span class="operator">></span>name()<span class="operator">.</span>toUpper(); <span class="type">int</span> rank; <span class="keyword">if</span> (sortKey<span class="operator">.</span>startsWith(QLatin1String(<span class="string">"UTF-8"</span>))) { rank <span class="operator">=</span> <span class="number">1</span>; } <span class="keyword">else</span> <span class="keyword">if</span> (sortKey<span class="operator">.</span>startsWith(QLatin1String(<span class="string">"UTF-16"</span>))) { rank <span class="operator">=</span> <span class="number">2</span>; } <span class="keyword">else</span> <span class="keyword">if</span> ((match <span class="operator">=</span> iso8859RegExp<span class="operator">.</span>match(sortKey))<span class="operator">.</span>hasMatch()) { <span class="keyword">if</span> (match<span class="operator">.</span>captured(<span class="number">1</span>)<span class="operator">.</span>size() <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>) rank <span class="operator">=</span> <span class="number">3</span>; <span class="keyword">else</span> rank <span class="operator">=</span> <span class="number">4</span>; } <span class="keyword">else</span> { rank <span class="operator">=</span> <span class="number">5</span>; } sortKey<span class="operator">.</span>prepend(QLatin1Char(<span class="char">'0'</span> <span class="operator">+</span> rank)); codecMap<span class="operator">.</span>insert(sortKey<span class="operator">,</span> codec); } codecs <span class="operator">=</span> codecMap<span class="operator">.</span>values(); } <span class="type">void</span> MainWindow<span class="operator">::</span>createMenus() { <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>fileMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">></span>addMenu(tr(<span class="string">"&File"</span>)); <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>openAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">></span>addAction(tr(<span class="string">"&Open..."</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>MainWindow<span class="operator">::</span>open); openAct<span class="operator">-</span><span class="operator">></span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Open); <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>saveAsMenu <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">></span>addMenu(tr(<span class="string">"&Save As"</span>)); connect(saveAsMenu<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qmenu.html">QMenu</a></span><span class="operator">::</span>aboutToShow<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>MainWindow<span class="operator">::</span>aboutToShowSaveAsMenu); foreach (<span class="keyword">const</span> <span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span> <span class="operator">*</span>codec<span class="operator">,</span> codecs) { <span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> name <span class="operator">=</span> codec<span class="operator">-</span><span class="operator">></span>name(); <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>action <span class="operator">=</span> saveAsMenu<span class="operator">-</span><span class="operator">></span>addAction(tr(<span class="string">"%1..."</span>)<span class="operator">.</span>arg(QLatin1String(name))); action<span class="operator">-</span><span class="operator">></span>setData(<span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span>(name)); connect(action<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>MainWindow<span class="operator">::</span>save); saveAsActs<span class="operator">.</span>append(action); } fileMenu<span class="operator">-</span><span class="operator">></span>addSeparator(); <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>exitAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">></span>addAction(tr(<span class="string">"E&xit"</span>)<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); exitAct<span class="operator">-</span><span class="operator">></span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Quit); <span class="keyword">auto</span> toolMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">></span>addMenu(tr(<span class="string">"&Tools"</span>)); <span class="keyword">auto</span> encodingAction <span class="operator">=</span> toolMenu<span class="operator">-</span><span class="operator">></span>addAction(tr(<span class="string">"Encodings"</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>MainWindow<span class="operator">::</span>encodingDialog); encodingAction<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_E); encodingAction<span class="operator">-</span><span class="operator">></span>setToolTip(tr(<span class="string">"Shows a dialog allowing to convert to common encoding in programming languages."</span>)); menuBar()<span class="operator">-</span><span class="operator">></span>addSeparator(); <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>helpMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">></span>addMenu(tr(<span class="string">"&Help"</span>)); helpMenu<span class="operator">-</span><span class="operator">></span>addAction(tr(<span class="string">"&About"</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>MainWindow<span class="operator">::</span>about); helpMenu<span class="operator">-</span><span class="operator">></span>addAction(tr(<span class="string">"About &Qt"</span>)<span class="operator">,</span> <a href="qapplication.html#qApp">qApp</a><span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>aboutQt); } <span class="type">void</span> MainWindow<span class="operator">::</span>encodingDialog() { <span class="keyword">if</span> (<span class="operator">!</span>m_encodingDialog) { m_encodingDialog <span class="operator">=</span> <span class="keyword">new</span> EncodingDialog(<span class="keyword">this</span>); <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>); m_encodingDialog<span class="operator">-</span><span class="operator">></span>setMinimumWidth(screenGeometry<span class="operator">.</span>width() <span class="operator">/</span> <span class="number">4</span>); } m_encodingDialog<span class="operator">-</span><span class="operator">></span>show(); m_encodingDialog<span class="operator">-</span><span class="operator">></span>raise(); } </pre> </div> <!-- @@@tools/codecs/mainwindow.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>