diff options
Diffstat (limited to 'examples/widgets/tools')
22 files changed, 555 insertions, 101 deletions
diff --git a/examples/widgets/tools/codecs/CMakeLists.txt b/examples/widgets/tools/codecs/CMakeLists.txt index fbf2e4e1a0..703aa3fb47 100644 --- a/examples/widgets/tools/codecs/CMakeLists.txt +++ b/examples/widgets/tools/codecs/CMakeLists.txt @@ -9,12 +9,18 @@ add_qt_executable(codecs OUTPUT_DIRECTORY "${INSTALL_EXAMPLESDIR}/widgets/tools/codecs" INSTALL_DIRECTORY "${INSTALL_EXAMPLESDIR}/widgets/tools/codecs" SOURCES + encodingdialog.cpp encodingdialog.h main.cpp mainwindow.cpp mainwindow.h previewform.cpp previewform.h - LIBRARIES + PUBLIC_LIBRARIES Qt::Widgets ) -#### Keys ignored in scope 1:.:codecs.pro:<NONE>: +# Resources: +add_qt_resource(codecs "codecs" PREFIX "/" FILES + images/editcopy.png) + + +#### Keys ignored in scope 1:.:.:codecs.pro:<TRUE>: # EXAMPLE_FILES = "encodedfiles" diff --git a/examples/widgets/tools/codecs/codecs.pro b/examples/widgets/tools/codecs/codecs.pro index 13daa79237..6f4b0742a9 100644 --- a/examples/widgets/tools/codecs/codecs.pro +++ b/examples/widgets/tools/codecs/codecs.pro @@ -2,10 +2,15 @@ QT += widgets requires(qtConfig(filedialog)) HEADERS += mainwindow.h \ - previewform.h + previewform.h \ + encodingdialog.h + SOURCES += main.cpp \ mainwindow.cpp \ - previewform.cpp + previewform.cpp \ + encodingdialog.cpp + +RESOURCES += codecs.qrc EXAMPLE_FILES = encodedfiles diff --git a/examples/widgets/tools/codecs/codecs.qrc b/examples/widgets/tools/codecs/codecs.qrc new file mode 100644 index 0000000000..65fa1aa64d --- /dev/null +++ b/examples/widgets/tools/codecs/codecs.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/" > + <file>images/editcopy.png</file> + </qresource> +</RCC> diff --git a/examples/widgets/tools/codecs/encodingdialog.cpp b/examples/widgets/tools/codecs/encodingdialog.cpp new file mode 100644 index 0000000000..ca4b56db9e --- /dev/null +++ b/examples/widgets/tools/codecs/encodingdialog.cpp @@ -0,0 +1,333 @@ +/**************************************************************************** +** +** Copyright (C) 2018 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$ +** +****************************************************************************/ + +#include "encodingdialog.h" + +#if QT_CONFIG(action) +# include <QAction> +#endif +#include <QDialogButtonBox> +#include <QFormLayout> +#include <QLabel> +#include <QLineEdit> +#include <QVBoxLayout> + +#if QT_CONFIG(clipboard) +# include <QGuiApplication> +# include <QClipboard> +#endif + +#include <QTextStream> + +// Helpers for formatting character sequences + +// Format a special character like '\x0a' +template <class Int> +static void formatEscapedNumber(QTextStream &str, Int value, int base, + int width = 0,char prefix = 0) +{ + str << '\\'; + if (prefix) + str << prefix; + const auto oldPadChar = str.padChar(); + const auto oldFieldWidth = str.fieldWidth(); + const auto oldFieldAlignment = str.fieldAlignment(); + const auto oldIntegerBase = str.integerBase(); + str.setPadChar(QLatin1Char('0')); + str.setFieldWidth(width); + str.setFieldAlignment(QTextStream::AlignRight); + str.setIntegerBase(base); + str << value; + str.setIntegerBase(oldIntegerBase); + str.setFieldAlignment(oldFieldAlignment); + str.setFieldWidth(oldFieldWidth); + str.setPadChar(oldPadChar); +} + +template <class Int> +static bool formatSpecialCharacter(QTextStream &str, Int value) +{ + bool result = true; + switch (value) { + case '\\': + str << "\\\\"; + break; + case '\"': + str << "\\\""; + break; + case '\n': + str << "\\n"; + break; + default: + result = false; + break; + } + return result; +} + +// Format a sequence of characters (QChar, ushort (UTF-16), uint (UTF-32) +// or just char (Latin1, Utf-8)) with the help of traits specifying +// how to obtain the code for checking the printable-ness and how to +// stream out the plain ASCII values. + +template <EncodingDialog::Encoding> +struct FormattingTraits +{ +}; + +template <> +struct FormattingTraits<EncodingDialog::Unicode> +{ + static ushort code(QChar c) { return c.unicode(); } + static char toAscii(QChar c) { return c.toLatin1(); } +}; + +template <> +struct FormattingTraits<EncodingDialog::Utf8> +{ + static ushort code(char c) { return uchar(c); } + static char toAscii(char c) { return c; } +}; + +template <> +struct FormattingTraits<EncodingDialog::Utf16> +{ + static ushort code(ushort c) { return c; } + static char toAscii(ushort c) { return char(c); } +}; + +template <> +struct FormattingTraits<EncodingDialog::Utf32> +{ + static uint code(uint c) { return c; } + static char toAscii(uint c) { return char(c); } +}; + +template <> +struct FormattingTraits<EncodingDialog::Latin1> +{ + static uchar code(char c) { return uchar(c); } + static char toAscii(char c) { return c; } +}; + +static bool isHexDigit(char c) +{ + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') + || (c >= 'A' && c <= 'F'); +} + +template <EncodingDialog::Encoding encoding, class Iterator> +static void formatStringSequence(QTextStream &str, Iterator i1, Iterator i2, + int escapeIntegerBase, int escapeWidth, + char escapePrefix = 0) +{ + str << '"'; + bool separateHexEscape = false; + for (; i1 != i2; ++i1) { + const auto code = FormattingTraits<encoding>::code(*i1); + if (code >= 0x80) { + formatEscapedNumber(str, code, escapeIntegerBase, escapeWidth, escapePrefix); + separateHexEscape = escapeIntegerBase == 16 && escapeWidth == 0; + } else { + if (!formatSpecialCharacter(str, code)) { + const char c = FormattingTraits<encoding>::toAscii(*i1); + // For variable width/hex: Terminate the literal to stop digit parsing + // ("\x12" "34..."). + if (separateHexEscape && isHexDigit(c)) + str << "\" \""; + str << c; + } + separateHexEscape = false; + } + } + str << '"'; +} + +static QString encodedString(const QString &value, EncodingDialog::Encoding e) +{ + QString result; + QTextStream str(&result); + switch (e) { + case EncodingDialog::Unicode: + formatStringSequence<EncodingDialog::Unicode>(str, value.cbegin(), value.cend(), + 16, 4, 'u'); + break; + case EncodingDialog::Utf8: { + const QByteArray utf8 = value.toUtf8(); + str << "u8"; + formatStringSequence<EncodingDialog::Utf8>(str, utf8.cbegin(), utf8.cend(), + 8, 3); + } + break; + case EncodingDialog::Utf16: { + auto utf16 = value.utf16(); + auto utf16End = utf16 + value.size(); + str << 'u'; + formatStringSequence<EncodingDialog::Utf16>(str, utf16, utf16End, + 16, 0, 'x'); + } + break; + case EncodingDialog::Utf32: { + auto utf32 = value.toUcs4(); + str << 'U'; + formatStringSequence<EncodingDialog::Utf32>(str, utf32.cbegin(), utf32.cend(), + 16, 0, 'x'); + } + break; + case EncodingDialog::Latin1: { + const QByteArray latin1 = value.toLatin1(); + formatStringSequence<EncodingDialog::Latin1>(str, latin1.cbegin(), latin1.cend(), + 16, 0, 'x'); + } + break; + case EncodingDialog::EncodingCount: + break; + } + return result; +} + +// Dialog helpers + +static const char *encodingLabels[] +{ + QT_TRANSLATE_NOOP("EncodingDialog", "Unicode:"), + QT_TRANSLATE_NOOP("EncodingDialog", "UTF-8:"), + QT_TRANSLATE_NOOP("EncodingDialog", "UTF-16:"), + QT_TRANSLATE_NOOP("EncodingDialog", "UTF-32:"), + QT_TRANSLATE_NOOP("EncodingDialog", "Latin1:") +}; + +static const char *encodingToolTips[] +{ + QT_TRANSLATE_NOOP("EncodingDialog", "Unicode points for use with any encoding (C++, Python)"), + QT_TRANSLATE_NOOP("EncodingDialog", "QString::fromUtf8()"), + QT_TRANSLATE_NOOP("EncodingDialog", "QStringViewLiteral(), wchar_t on Windows"), + QT_TRANSLATE_NOOP("EncodingDialog", "wchar_t on Unix (Ucs4)"), + QT_TRANSLATE_NOOP("EncodingDialog", "QLatin1String") +}; + +// A read-only line edit with a tool button to copy the contents +class DisplayLineEdit : public QLineEdit +{ + Q_OBJECT +public: + explicit DisplayLineEdit(const QIcon &icon, QWidget *parent = nullptr); + +public slots: + void copyAll(); +}; + +DisplayLineEdit::DisplayLineEdit(const QIcon &icon, QWidget *parent) : + QLineEdit(parent) +{ + setReadOnly(true); +#if QT_CONFIG(clipboard) && QT_CONFIG(action) + auto copyAction = addAction(icon, QLineEdit::TrailingPosition); + connect(copyAction, &QAction::triggered, this, &DisplayLineEdit::copyAll); +#endif +} + +void DisplayLineEdit::copyAll() +{ +#if QT_CONFIG(clipboard) + QGuiApplication::clipboard()->setText(text()); +#endif +} + +static void addFormLayoutRow(QFormLayout *formLayout, const QString &text, + QWidget *w, const QString &toolTip) +{ + auto label = new QLabel(text); + label->setToolTip(toolTip); + w->setToolTip(toolTip); + label->setBuddy(w); + formLayout->addRow(label, w); +} + +EncodingDialog::EncodingDialog(QWidget *parent) : + QDialog(parent) +{ + setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); + setWindowTitle(tr("Encodings")); + + auto formLayout = new QFormLayout; + auto sourceLineEdit = new QLineEdit(this); + sourceLineEdit->setClearButtonEnabled(true); + connect(sourceLineEdit, &QLineEdit::textChanged, this, &EncodingDialog::textChanged); + + addFormLayoutRow(formLayout, tr("&Source:"), sourceLineEdit, tr("Enter text")); + + const auto copyIcon = QIcon::fromTheme(QLatin1String("edit-copy"), + QIcon(QLatin1String(":/images/editcopy"))); + for (int i = 0; i < EncodingCount; ++i) { + m_lineEdits[i] = new DisplayLineEdit(copyIcon, this); + addFormLayoutRow(formLayout, tr(encodingLabels[i]), + m_lineEdits[i], tr(encodingToolTips[i])); + } + + auto mainLayout = new QVBoxLayout(this); + mainLayout->addLayout(formLayout); + auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); + mainLayout->addWidget(buttonBox); +} + +void EncodingDialog::textChanged(const QString &t) +{ + if (t.isEmpty()) { + for (auto lineEdit : m_lineEdits) + lineEdit->clear(); + } else { + for (int i = 0; i < EncodingCount; ++i) + m_lineEdits[i]->setText(encodedString(t, static_cast<Encoding>(i))); + } +} + +#include "encodingdialog.moc" diff --git a/examples/widgets/tools/codecs/encodingdialog.h b/examples/widgets/tools/codecs/encodingdialog.h new file mode 100644 index 0000000000..3f82da84af --- /dev/null +++ b/examples/widgets/tools/codecs/encodingdialog.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2018 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$ +** +****************************************************************************/ + +#ifndef ENCODINGDIALOG_H +#define ENCODINGDIALOG_H + +#include <QDialog> + +QT_FORWARD_DECLARE_CLASS(QLineEdit) + +class EncodingDialog : public QDialog +{ + Q_OBJECT +public: + explicit EncodingDialog(QWidget *parent = nullptr); + + enum Encoding { Unicode, Utf8, Utf16, Utf32, Latin1, EncodingCount }; + +private slots: + void textChanged(const QString &t); + +private: + QLineEdit *m_lineEdits[EncodingCount]; +}; + +#endif // ENCODINGDIALOG_H diff --git a/examples/widgets/tools/codecs/images/editcopy.png b/examples/widgets/tools/codecs/images/editcopy.png Binary files differnew file mode 100644 index 0000000000..1121b47d8b --- /dev/null +++ b/examples/widgets/tools/codecs/images/editcopy.png diff --git a/examples/widgets/tools/codecs/mainwindow.cpp b/examples/widgets/tools/codecs/mainwindow.cpp index 28f904d1a7..53db9fe61f 100644 --- a/examples/widgets/tools/codecs/mainwindow.cpp +++ b/examples/widgets/tools/codecs/mainwindow.cpp @@ -51,6 +51,7 @@ #include <QtWidgets> #include "mainwindow.h" +#include "encodingdialog.h" #include "previewform.h" MainWindow::MainWindow() @@ -126,11 +127,10 @@ void MainWindow::about() void MainWindow::aboutToShowSaveAsMenu() { - QString currentText = textEdit->toPlainText(); - - foreach (QAction *action, saveAsActs) { - QByteArray codecName = action->data().toByteArray(); - QTextCodec *codec = QTextCodec::codecForName(codecName); + const QString currentText = textEdit->toPlainText(); + for (QAction *action : qAsConst(saveAsActs)) { + const QByteArray codecName = action->data().toByteArray(); + const QTextCodec *codec = QTextCodec::codecForName(codecName); action->setVisible(codec && codec->canEncode(currentText)); } } @@ -141,7 +141,8 @@ void MainWindow::findCodecs() QRegularExpression iso8859RegExp("^ISO[- ]8859-([0-9]+).*$"); QRegularExpressionMatch match; - foreach (int mib, QTextCodec::availableMibs()) { + const QList<int> mibs = QTextCodec::availableMibs(); + for (int mib : mibs) { QTextCodec *codec = QTextCodec::codecForMib(mib); QString sortKey = codec->name().toUpper(); @@ -176,7 +177,7 @@ void MainWindow::createMenus() QMenu *saveAsMenu = fileMenu->addMenu(tr("&Save As")); connect(saveAsMenu, &QMenu::aboutToShow, this, &MainWindow::aboutToShowSaveAsMenu); - foreach (const QTextCodec *codec, codecs) { + for (const QTextCodec *codec : qAsConst(codecs)) { const QByteArray name = codec->name(); QAction *action = saveAsMenu->addAction(tr("%1...").arg(QLatin1String(name))); action->setData(QVariant(name)); @@ -188,9 +189,27 @@ void MainWindow::createMenus() QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close); exitAct->setShortcuts(QKeySequence::Quit); + auto toolMenu = menuBar()->addMenu(tr("&Tools")); + auto encodingAction = toolMenu->addAction(tr("Encodings"), this, &MainWindow::encodingDialog); + encodingAction->setShortcut(Qt::CTRL + Qt::Key_E); + encodingAction->setToolTip(tr("Shows a dialog allowing to convert to common encoding in programming languages.")); + + menuBar()->addSeparator(); QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); helpMenu->addAction(tr("&About"), this, &MainWindow::about); helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt); } + +void MainWindow::encodingDialog() +{ + if (!m_encodingDialog) { + m_encodingDialog = new EncodingDialog(this); + const QRect screenGeometry = QApplication::desktop()->screenGeometry(this); + m_encodingDialog->setMinimumWidth(screenGeometry.width() / 4); + } + m_encodingDialog->show(); + m_encodingDialog->raise(); + +} diff --git a/examples/widgets/tools/codecs/mainwindow.h b/examples/widgets/tools/codecs/mainwindow.h index 7121c0b122..64494d1960 100644 --- a/examples/widgets/tools/codecs/mainwindow.h +++ b/examples/widgets/tools/codecs/mainwindow.h @@ -59,6 +59,8 @@ class QAction; class QTextCodec; class QPlainTextEdit; QT_END_NAMESPACE + +class EncodingDialog; class PreviewForm; class MainWindow : public QMainWindow @@ -73,6 +75,7 @@ private slots: void save(); void about(); void aboutToShowSaveAsMenu(); + void encodingDialog(); private: void findCodecs(); @@ -82,6 +85,7 @@ private: QPlainTextEdit *textEdit; PreviewForm *previewForm; QList<QTextCodec *> codecs; + EncodingDialog *m_encodingDialog = nullptr; }; #endif diff --git a/examples/widgets/tools/codecs/previewform.cpp b/examples/widgets/tools/codecs/previewform.cpp index d19b9c0833..206b5757cd 100644 --- a/examples/widgets/tools/codecs/previewform.cpp +++ b/examples/widgets/tools/codecs/previewform.cpp @@ -182,7 +182,7 @@ PreviewForm::PreviewForm(QWidget *parent) void PreviewForm::setCodecList(const QList<QTextCodec *> &list) { encodingComboBox->clear(); - foreach (const QTextCodec *codec, list) { + for (const QTextCodec *codec : list) { encodingComboBox->addItem(QLatin1String(codec->name()), QVariant(codec->mibEnum())); } diff --git a/examples/widgets/tools/completer/mainwindow.cpp b/examples/widgets/tools/completer/mainwindow.cpp index d63d523548..114ff0fd7c 100644 --- a/examples/widgets/tools/completer/mainwindow.cpp +++ b/examples/widgets/tools/completer/mainwindow.cpp @@ -102,10 +102,14 @@ MainWindow::MainWindow(QWidget *parent) contentsLabel = new QLabel; contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - connect(modelCombo, SIGNAL(activated(int)), this, SLOT(changeModel())); - connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int))); - connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int))); - connect(maxVisibleSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeMaxVisible(int))); + connect(modelCombo, QOverload<int>::of(&QComboBox::activated), + this, &MainWindow::changeModel); + connect(modeCombo, QOverload<int>::of(&QComboBox::activated), + this, &MainWindow::changeMode); + connect(caseCombo, QOverload<int>::of(&QComboBox::activated), + this, &MainWindow::changeCase); + connect(maxVisibleSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), + this, &MainWindow::changeMaxVisible); //! [2] //! [3] @@ -136,9 +140,9 @@ void MainWindow::createMenu() QAction *aboutAct = new QAction(tr("About"), this); QAction *aboutQtAct = new QAction(tr("About Qt"), this); - connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); - connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); - connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + connect(exitAction, &QAction::triggered, qApp, &QApplication::quit); + connect(aboutAct, &QAction::triggered, this, &MainWindow::about); + connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt); QMenu* fileMenu = menuBar()->addMenu(tr("File")); fileMenu->addAction(exitAction); @@ -159,7 +163,7 @@ QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) //! [6] #ifndef QT_NO_CURSOR - QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); + QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); #endif QStringList words; @@ -170,7 +174,7 @@ QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) } #ifndef QT_NO_CURSOR - QApplication::restoreOverrideCursor(); + QGuiApplication::restoreOverrideCursor(); #endif //! [6] @@ -271,7 +275,7 @@ void MainWindow::changeModel() changeCase(caseCombo->currentIndex()); completer->setWrapAround(wrapCheckBox->isChecked()); lineEdit->setCompleter(completer); - connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool))); + connect(wrapCheckBox, &QAbstractButton::clicked, completer, &QCompleter::setWrapAround); } //! [14] diff --git a/examples/widgets/tools/customcompleter/mainwindow.cpp b/examples/widgets/tools/customcompleter/mainwindow.cpp index 26948d0c8e..39f5f39617 100644 --- a/examples/widgets/tools/customcompleter/mainwindow.cpp +++ b/examples/widgets/tools/customcompleter/mainwindow.cpp @@ -79,9 +79,9 @@ void MainWindow::createMenu() QAction *aboutAct = new QAction(tr("About"), this); QAction *aboutQtAct = new QAction(tr("About Qt"), this); - connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); - connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); - connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + connect(exitAction, &QAction::triggered, qApp, &QApplication::quit); + connect(aboutAct, &QAction::triggered, this, &MainWindow::about); + connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt); QMenu* fileMenu = menuBar()->addMenu(tr("File")); fileMenu->addAction(exitAction); @@ -100,7 +100,7 @@ QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) return new QStringListModel(completer); #ifndef QT_NO_CURSOR - QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); + QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); #endif QStringList words; @@ -111,7 +111,7 @@ QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) } #ifndef QT_NO_CURSOR - QApplication::restoreOverrideCursor(); + QGuiApplication::restoreOverrideCursor(); #endif return new QStringListModel(words, completer); } diff --git a/examples/widgets/tools/customcompleter/textedit.cpp b/examples/widgets/tools/customcompleter/textedit.cpp index 5512e72843..d42f7b38bb 100644 --- a/examples/widgets/tools/customcompleter/textedit.cpp +++ b/examples/widgets/tools/customcompleter/textedit.cpp @@ -88,8 +88,8 @@ void TextEdit::setCompleter(QCompleter *completer) c->setWidget(this); c->setCompletionMode(QCompleter::PopupCompletion); c->setCaseSensitivity(Qt::CaseInsensitive); - QObject::connect(c, SIGNAL(activated(QString)), - this, SLOT(insertCompletion(QString))); + QObject::connect(c, QOverload<const QString &>::of(&QCompleter::activated), + this, &TextEdit::insertCompletion); } //! [2] diff --git a/examples/widgets/tools/echoplugin/echowindow/echowindow.cpp b/examples/widgets/tools/echoplugin/echowindow/echowindow.cpp index 64b59d506e..6886a4cd88 100644 --- a/examples/widgets/tools/echoplugin/echowindow/echowindow.cpp +++ b/examples/widgets/tools/echoplugin/echowindow/echowindow.cpp @@ -113,7 +113,8 @@ bool EchoWindow::loadPlugin() } #endif pluginsDir.cd("plugins"); - foreach (QString fileName, pluginsDir.entryList(QDir::Files)) { + const QStringList entries = pluginsDir.entryList(QDir::Files); + for (const QString &fileName : entries) { QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName)); QObject *plugin = pluginLoader.instance(); if (plugin) { diff --git a/examples/widgets/tools/i18n/languagechooser.cpp b/examples/widgets/tools/i18n/languagechooser.cpp index 58cf9d4047..f07d0ddee3 100644 --- a/examples/widgets/tools/i18n/languagechooser.cpp +++ b/examples/widgets/tools/i18n/languagechooser.cpp @@ -148,14 +148,14 @@ void LanguageChooser::checkBoxToggled() void LanguageChooser::showAll() { - foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys()) - checkBox->setChecked(true); + for (auto it = qmFileForCheckBoxMap.keyBegin(); it != qmFileForCheckBoxMap.keyEnd(); ++it) + (*it)->setChecked(true); } void LanguageChooser::hideAll() { - foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys()) - checkBox->setChecked(false); + for (auto it = qmFileForCheckBoxMap.keyBegin(); it != qmFileForCheckBoxMap.keyEnd(); ++it) + (*it)->setChecked(false); } QStringList LanguageChooser::findQmFiles() diff --git a/examples/widgets/tools/plugandpaint/app/plugindialog.cpp b/examples/widgets/tools/plugandpaint/app/plugindialog.cpp index af5828f67e..84bd364b41 100644 --- a/examples/widgets/tools/plugandpaint/app/plugindialog.cpp +++ b/examples/widgets/tools/plugandpaint/app/plugindialog.cpp @@ -125,7 +125,7 @@ void PluginDialog::populateTreeWidget(QObject *plugin, const QString &text) { auto pluginItem = new QTreeWidgetItem(treeWidget); pluginItem->setText(0, text); - treeWidget->setItemExpanded(pluginItem, true); + pluginItem->setExpanded(true); QFont boldFont = pluginItem->font(0); boldFont.setBold(true); diff --git a/examples/widgets/tools/regexp/regexpdialog.cpp b/examples/widgets/tools/regexp/regexpdialog.cpp index 7b3fee5794..bf61d09974 100644 --- a/examples/widgets/tools/regexp/regexpdialog.cpp +++ b/examples/widgets/tools/regexp/regexpdialog.cpp @@ -135,15 +135,15 @@ RegExpDialog::RegExpDialog(QWidget *parent) } setLayout(mainLayout); - connect(patternComboBox, SIGNAL(editTextChanged(QString)), - this, SLOT(refresh())); - connect(textComboBox, SIGNAL(editTextChanged(QString)), - this, SLOT(refresh())); - connect(caseSensitiveCheckBox, SIGNAL(toggled(bool)), - this, SLOT(refresh())); - connect(minimalCheckBox, SIGNAL(toggled(bool)), this, SLOT(refresh())); - connect(syntaxComboBox, SIGNAL(currentIndexChanged(int)), - this, SLOT(refresh())); + connect(patternComboBox, &QComboBox::editTextChanged, + this, &RegExpDialog::refresh); + connect(textComboBox, &QComboBox::editTextChanged, + this, &RegExpDialog::refresh); + connect(caseSensitiveCheckBox, &QAbstractButton::toggled, + this, &RegExpDialog::refresh); + connect(minimalCheckBox, &QAbstractButton::toggled, this, &RegExpDialog::refresh); + connect(syntaxComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), + this, &RegExpDialog::refresh); patternComboBox->addItem(tr("[A-Za-z_]+([A-Za-z_0-9]*)")); textComboBox->addItem(tr("(10 + delta4) * 32")); diff --git a/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp b/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp index 8fbf143cbd..ea3cb00a02 100644 --- a/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp +++ b/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp @@ -376,7 +376,7 @@ QWidget *RegularExpressionDialog::setupLeftUi() QFormLayout *layout = new QFormLayout(container); layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow); - layout->setMargin(0); + layout->setContentsMargins(QMargins()); QLabel *regexpAndSubjectLabel = new QLabel(tr("<h3>Regular expression and text input</h3>")); layout->addRow(regexpAndSubjectLabel); @@ -448,7 +448,7 @@ QWidget *RegularExpressionDialog::setupRightUi() QFormLayout *layout = new QFormLayout(container); layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow); - layout->setMargin(0); + layout->setContentsMargins(QMargins()); QLabel *matchInfoLabel = new QLabel(tr("<h3>Match information</h3>")); layout->addRow(matchInfoLabel); diff --git a/examples/widgets/tools/settingseditor/settingstree.cpp b/examples/widgets/tools/settingseditor/settingstree.cpp index 4ca843784e..8585792787 100644 --- a/examples/widgets/tools/settingseditor/settingstree.cpp +++ b/examples/widgets/tools/settingseditor/settingstree.cpp @@ -170,7 +170,8 @@ void SettingsTree::updateChildItems(QTreeWidgetItem *parent) { int dividerIndex = 0; - foreach (QString group, settings->childGroups()) { + const QStringList childGroups = settings->childGroups(); + for (const QString &group : childGroups) { QTreeWidgetItem *child; int childIndex = findChild(parent, group, dividerIndex); if (childIndex != -1) { @@ -190,7 +191,8 @@ void SettingsTree::updateChildItems(QTreeWidgetItem *parent) settings->endGroup(); } - foreach (const QString &key, settings->childKeys()) { + const QStringList childKeys = settings->childKeys(); + for (const QString &key : childKeys) { QTreeWidgetItem *child; int childIndex = findChild(parent, key, 0); diff --git a/examples/widgets/tools/treemodelcompleter/mainwindow.cpp b/examples/widgets/tools/treemodelcompleter/mainwindow.cpp index a8b51c7aa0..dec3cb0496 100644 --- a/examples/widgets/tools/treemodelcompleter/mainwindow.cpp +++ b/examples/widgets/tools/treemodelcompleter/mainwindow.cpp @@ -61,8 +61,8 @@ MainWindow::MainWindow(QWidget *parent) completer = new TreeModelCompleter(this); completer->setModel(modelFromFile(":/resources/treemodel.txt")); completer->setSeparator(QLatin1String(".")); - QObject::connect(completer, SIGNAL(highlighted(QModelIndex)), - this, SLOT(highlight(QModelIndex))); + QObject::connect(completer, QOverload<const QModelIndex &>::of(&TreeModelCompleter::highlighted), + this, &MainWindow::highlight); QWidget *centralWidget = new QWidget; @@ -91,18 +91,18 @@ MainWindow::MainWindow(QWidget *parent) QLineEdit *separatorLineEdit = new QLineEdit; separatorLineEdit->setText(completer->separator()); - connect(separatorLineEdit, SIGNAL(textChanged(QString)), - completer, SLOT(setSeparator(QString))); + connect(separatorLineEdit, &QLineEdit::textChanged, + completer, &TreeModelCompleter::setSeparator); QCheckBox *wrapCheckBox = new QCheckBox; wrapCheckBox->setText(tr("Wrap around completions")); wrapCheckBox->setChecked(completer->wrapAround()); - connect(wrapCheckBox, SIGNAL(clicked(bool)), completer, SLOT(setWrapAround(bool))); + connect(wrapCheckBox, &QAbstractButton::clicked, completer, &QCompleter::setWrapAround); contentsLabel = new QLabel; contentsLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - connect(separatorLineEdit, SIGNAL(textChanged(QString)), - this, SLOT(updateContentsLabel(QString))); + connect(separatorLineEdit, &QLineEdit::textChanged, + this, &MainWindow::updateContentsLabel); treeView = new QTreeView; treeView->setModel(completer->model()); @@ -111,8 +111,10 @@ MainWindow::MainWindow(QWidget *parent) //! [1] //! [2] - connect(modeCombo, SIGNAL(activated(int)), this, SLOT(changeMode(int))); - connect(caseCombo, SIGNAL(activated(int)), this, SLOT(changeCase(int))); + connect(modeCombo, QOverload<int>::of(&QComboBox::activated), + this, &MainWindow::changeMode); + connect(caseCombo, QOverload<int>::of(&QComboBox::activated), + this, &MainWindow::changeMode); lineEdit = new QLineEdit; lineEdit->setCompleter(completer); @@ -145,9 +147,9 @@ void MainWindow::createMenu() QAction *aboutAct = new QAction(tr("About"), this); QAction *aboutQtAct = new QAction(tr("About Qt"), this); - connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); - connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); - connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + connect(exitAction, &QAction::triggered, qApp, &QApplication::quit); + connect(aboutAct, &QAction::triggered, this, &MainWindow::about); + connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt); QMenu* fileMenu = menuBar()->addMenu(tr("File")); fileMenu->addAction(exitAction); @@ -180,7 +182,7 @@ QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) return new QStringListModel(completer); #ifndef QT_NO_CURSOR - QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); + QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); #endif QStringList words; @@ -218,7 +220,7 @@ QAbstractItemModel *MainWindow::modelFromFile(const QString& fileName) } #ifndef QT_NO_CURSOR - QApplication::restoreOverrideCursor(); + QGuiApplication::restoreOverrideCursor(); #endif return model; diff --git a/examples/widgets/tools/undo/document.cpp b/examples/widgets/tools/undo/document.cpp index 212656721e..8935f98a7a 100644 --- a/examples/widgets/tools/undo/document.cpp +++ b/examples/widgets/tools/undo/document.cpp @@ -338,9 +338,9 @@ static QGradient gradient(const QColor &color, const QRect &rect) QColor c = color; c.setAlpha(160); QLinearGradient result(rect.topLeft(), rect.bottomRight()); - result.setColorAt(0, c.dark(150)); - result.setColorAt(0.5, c.light(200)); - result.setColorAt(1, c.dark(150)); + result.setColorAt(0, c.darker(150)); + result.setColorAt(0.5, c.lighter(200)); + result.setColorAt(1, c.darker(150)); return result; } diff --git a/examples/widgets/tools/undo/mainwindow.cpp b/examples/widgets/tools/undo/mainwindow.cpp index 5976163f3f..118d604742 100644 --- a/examples/widgets/tools/undo/mainwindow.cpp +++ b/examples/widgets/tools/undo/mainwindow.cpp @@ -68,25 +68,25 @@ MainWindow::MainWindow(QWidget *parent) documentTabs->removeTab(0); delete w; - connect(actionOpen, SIGNAL(triggered()), this, SLOT(openDocument())); - connect(actionClose, SIGNAL(triggered()), this, SLOT(closeDocument())); - connect(actionNew, SIGNAL(triggered()), this, SLOT(newDocument())); - connect(actionSave, SIGNAL(triggered()), this, SLOT(saveDocument())); - connect(actionExit, SIGNAL(triggered()), this, SLOT(close())); - connect(actionRed, SIGNAL(triggered()), this, SLOT(setShapeColor())); - connect(actionGreen, SIGNAL(triggered()), this, SLOT(setShapeColor())); - connect(actionBlue, SIGNAL(triggered()), this, SLOT(setShapeColor())); - connect(actionAddCircle, SIGNAL(triggered()), this, SLOT(addShape())); - connect(actionAddRectangle, SIGNAL(triggered()), this, SLOT(addShape())); - connect(actionAddTriangle, SIGNAL(triggered()), this, SLOT(addShape())); - connect(actionRemoveShape, SIGNAL(triggered()), this, SLOT(removeShape())); - connect(actionAddRobot, SIGNAL(triggered()), this, SLOT(addRobot())); - connect(actionAddSnowman, SIGNAL(triggered()), this, SLOT(addSnowman())); - connect(actionAbout, SIGNAL(triggered()), this, SLOT(about())); - connect(actionAboutQt, SIGNAL(triggered()), this, SLOT(aboutQt())); - - connect(undoLimit, SIGNAL(valueChanged(int)), this, SLOT(updateActions())); - connect(documentTabs, SIGNAL(currentChanged(int)), this, SLOT(updateActions())); + connect(actionOpen, &QAction::triggered, this, &MainWindow::openDocument); + connect(actionClose, &QAction::triggered, this, &MainWindow::closeDocument); + connect(actionNew, &QAction::triggered, this, &MainWindow::newDocument); + connect(actionSave, &QAction::triggered, this, &MainWindow::saveDocument); + connect(actionExit, &QAction::triggered, this, &QWidget::close); + connect(actionRed, &QAction::triggered, this, &MainWindow::setShapeColor); + connect(actionGreen, &QAction::triggered, this, &MainWindow::setShapeColor); + connect(actionBlue, &QAction::triggered, this, &MainWindow::setShapeColor); + connect(actionAddCircle, &QAction::triggered, this, &MainWindow::addShape); + connect(actionAddRectangle, &QAction::triggered, this, &MainWindow::addShape); + connect(actionAddTriangle, &QAction::triggered, this, &MainWindow::addShape); + connect(actionRemoveShape, &QAction::triggered, this, &MainWindow::removeShape); + connect(actionAddRobot, &QAction::triggered, this, &MainWindow::addRobot); + connect(actionAddSnowman, &QAction::triggered, this, &MainWindow::addSnowman); + connect(actionAbout, &QAction::triggered, this, &MainWindow::about); + connect(actionAboutQt, &QAction::triggered, this, &MainWindow::aboutQt); + + connect(undoLimit, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::updateActions); + connect(documentTabs, &QTabWidget::currentChanged, this, &MainWindow::updateActions); actionOpen->setShortcut(QString("Ctrl+O")); actionClose->setShortcut(QString("Ctrl+W")); @@ -226,9 +226,9 @@ void MainWindow::addDocument(Document *doc) return; m_undoGroup->addStack(doc->undoStack()); documentTabs->addTab(doc, fixedWindowTitle(doc)); - connect(doc, SIGNAL(currentShapeChanged(QString)), this, SLOT(updateActions())); - connect(doc->undoStack(), SIGNAL(indexChanged(int)), this, SLOT(updateActions())); - connect(doc->undoStack(), SIGNAL(cleanChanged(bool)), this, SLOT(updateActions())); + connect(doc, &Document::currentShapeChanged, this, &MainWindow::updateActions); + connect(doc->undoStack(), &QUndoStack::indexChanged, this, &MainWindow::updateActions); + connect(doc->undoStack(), &QUndoStack::cleanChanged, this, &MainWindow::updateActions); setCurrentDocument(doc); } @@ -251,9 +251,9 @@ void MainWindow::removeDocument(Document *doc) documentTabs->removeTab(index); m_undoGroup->removeStack(doc->undoStack()); - disconnect(doc, SIGNAL(currentShapeChanged(QString)), this, SLOT(updateActions())); - disconnect(doc->undoStack(), SIGNAL(indexChanged(int)), this, SLOT(updateActions())); - disconnect(doc->undoStack(), SIGNAL(cleanChanged(bool)), this, SLOT(updateActions())); + disconnect(doc, &Document::currentShapeChanged, this, &MainWindow::updateActions); + disconnect(doc->undoStack(), &QUndoStack::indexChanged, this, &MainWindow::updateActions); + disconnect(doc->undoStack(), &QUndoStack::cleanChanged, this, &MainWindow::updateActions); if (documentTabs->count() == 0) { newDocument(); diff --git a/examples/widgets/tools/undoframework/mainwindow.cpp b/examples/widgets/tools/undoframework/mainwindow.cpp index b2f5405b73..e95d50d164 100644 --- a/examples/widgets/tools/undoframework/mainwindow.cpp +++ b/examples/widgets/tools/undoframework/mainwindow.cpp @@ -70,8 +70,8 @@ MainWindow::MainWindow() diagramScene->setBackgroundBrush(pixmapBrush); diagramScene->setSceneRect(QRect(0, 0, 500, 500)); - connect(diagramScene, SIGNAL(itemMoved(DiagramItem*,QPointF)), - this, SLOT(itemMoved(DiagramItem*,QPointF))); + connect(diagramScene, &DiagramScene::itemMoved, + this, &MainWindow::itemMoved); setWindowTitle("Undo Framework"); QGraphicsView *view = new QGraphicsView(diagramScene); @@ -95,18 +95,18 @@ void MainWindow::createActions() { deleteAction = new QAction(tr("&Delete Item"), this); deleteAction->setShortcut(tr("Del")); - connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem())); + connect(deleteAction, &QAction::triggered, this, &MainWindow::deleteItem); //! [2] //! [3] //! [3] //! [4] addBoxAction = new QAction(tr("Add &Box"), this); //! [4] addBoxAction->setShortcut(tr("Ctrl+O")); - connect(addBoxAction, SIGNAL(triggered()), this, SLOT(addBox())); + connect(addBoxAction, &QAction::triggered, this, &MainWindow::addBox); addTriangleAction = new QAction(tr("Add &Triangle"), this); addTriangleAction->setShortcut(tr("Ctrl+T")); - connect(addTriangleAction, SIGNAL(triggered()), this, SLOT(addTriangle())); + connect(addTriangleAction, &QAction::triggered, this, &MainWindow::addTriangle); //! [5] undoAction = undoStack->createUndoAction(this, tr("&Undo")); @@ -118,13 +118,13 @@ void MainWindow::createActions() exitAction = new QAction(tr("E&xit"), this); exitAction->setShortcuts(QKeySequence::Quit); - connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); + connect(exitAction, &QAction::triggered, this, &QWidget::close); aboutAction = new QAction(tr("&About"), this); QList<QKeySequence> aboutShortcuts; aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B"); aboutAction->setShortcuts(aboutShortcuts); - connect(aboutAction, SIGNAL(triggered()), this, SLOT(about())); + connect(aboutAction, &QAction::triggered, this, &MainWindow::about); } //! [6] @@ -140,10 +140,10 @@ void MainWindow::createMenus() editMenu->addAction(redoAction); editMenu->addSeparator(); editMenu->addAction(deleteAction); - connect(editMenu, SIGNAL(aboutToShow()), - this, SLOT(itemMenuAboutToShow())); - connect(editMenu, SIGNAL(aboutToHide()), - this, SLOT(itemMenuAboutToHide())); + connect(editMenu, &QMenu::aboutToShow, + this, &MainWindow::itemMenuAboutToShow); + connect(editMenu, &QMenu::aboutToHide, + this, &MainWindow::itemMenuAboutToHide); //! [7] itemMenu = menuBar()->addMenu(tr("&Item")); |