diff options
| author | Oleksii Zbykovskyi <Oleksii.Zbykovskyi@qt.io> | 2025-08-25 10:39:19 +0200 |
|---|---|---|
| committer | Oleksii Zbykovskyi <Oleksii.Zbykovskyi@qt.io> | 2025-08-29 16:36:36 +0200 |
| commit | 931b0fb451162bbd29751dd1c5892024520fd2b2 (patch) | |
| tree | e77156161546d183726b7896f87adaaceb932301 /src/widgets/doc/snippets/code | |
| parent | aa3f9f5dcabdd69e3db7345898335132840901d1 (diff) | |
Widgets snippets code: Fix of files under the dialogs prefix
Only small fixes in code snippets related to indentation and scopes.
Also added them to the build system with the necessary conditions
Task-number: QTBUG-137566
Change-Id: I0a5e3636d501fb33179a940de56f608552c99ce1
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/widgets/doc/snippets/code')
5 files changed, 327 insertions, 199 deletions
diff --git a/src/widgets/doc/snippets/code/CMakeLists.txt b/src/widgets/doc/snippets/code/CMakeLists.txt index 931e439f540..ec672492de4 100644 --- a/src/widgets/doc/snippets/code/CMakeLists.txt +++ b/src/widgets/doc/snippets/code/CMakeLists.txt @@ -36,6 +36,30 @@ if(QT_FEATURE_lineedit) ) endif() +if(QT_FEATURE_filedialog) + target_sources(widgets_snippets_code PRIVATE + src_gui_dialogs_qfiledialog.cpp + ) +endif() + +if(QT_FEATURE_fontdialog) + target_sources(widgets_snippets_code PRIVATE + src_gui_dialogs_qfontdialog.cpp + ) +endif() + +if(QT_FEATURE_messagebox) + target_sources(widgets_snippets_code PRIVATE + src_gui_dialogs_qmessagebox.cpp + ) +endif() + +if(QT_FEATURE_wizard) + target_sources(widgets_snippets_code PRIVATE + src_gui_dialogs_qwizard.cpp + ) +endif() + set_target_properties(widgets_snippets_code PROPERTIES COMPILE_OPTIONS "-w") if ("${CMAKE_CXX_COMPILE_FEATURES}" MATCHES "cxx_std_23") diff --git a/src/widgets/doc/snippets/code/src_gui_dialogs_qfiledialog.cpp b/src/widgets/doc/snippets/code/src_gui_dialogs_qfiledialog.cpp index 197c23f247d..0325109c1f1 100644 --- a/src/widgets/doc/snippets/code/src_gui_dialogs_qfiledialog.cpp +++ b/src/widgets/doc/snippets/code/src_gui_dialogs_qfiledialog.cpp @@ -1,116 +1,155 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -fileName = QFileDialog::getOpenFileName(this, - tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)")); -//! [0] +#include <QFileDialog> +#include <QWidget> -//! [1] -"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)" -//! [1] - - -//! [2] -QFileDialog dialog(this); -dialog.setFileMode(QFileDialog::AnyFile); -//! [2] - - -//! [3] -dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)")); -//! [3] - - -//! [4] -dialog.setViewMode(QFileDialog::Detail); -//! [4] - - -//! [5] -QStringList fileNames; -if (dialog.exec()) - fileNames = dialog.selectedFiles(); -//! [5] +class FileDialogExample : public QWidget +{ + Q_OBJECT +public: + FileDialogExample(); +}; +FileDialogExample::FileDialogExample() +{ + QString fileName; + QFileDialog dialog(this); + { + //! [0] + fileName = QFileDialog::getOpenFileName(this, + tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)")); + //! [0] + } -//! [6] -dialog.setNameFilter("All C++ files (*.cpp *.cc *.C *.cxx *.c++)"); -dialog.setNameFilter("*.cpp *.cc *.C *.cxx *.c++"); -//! [6] + { + /* For convinient quoting. + //! [1] + "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)" + //! [1] + */ + } + { + //! [2] + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::AnyFile); + //! [2] + } -//! [7] -const QStringList filters({"Image files (*.png *.xpm *.jpg)", - "Text files (*.txt)", - "Any files (*)" - }); -QFileDialog dialog(this); -dialog.setNameFilters(filters); -dialog.exec(); -//! [7] + { + //! [3] + dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)")); + //! [3] + } + { + //! [4] + dialog.setViewMode(QFileDialog::Detail); + //! [4] + } -//! [8] -QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), - "/home", - tr("Images (*.png *.xpm *.jpg)")); -//! [8] + { + //! [5] + QStringList fileNames; + if (dialog.exec()) + fileNames = dialog.selectedFiles(); + //! [5] + } + { + //! [6] + dialog.setNameFilter("All C++ files (*.cpp *.cc *.C *.cxx *.c++)"); + dialog.setNameFilter("*.cpp *.cc *.C *.cxx *.c++"); + //! [6] + } -//! [9] -QStringList files = QFileDialog::getOpenFileNames( - this, - "Select one or more files to open", - "/home", - "Images (*.png *.xpm *.jpg)"); -//! [9] + { + //! [7] + const QStringList filters({"Image files (*.png *.xpm *.jpg)", + "Text files (*.txt)", + "Any files (*)" + }); + QFileDialog dialog(this); + dialog.setNameFilters(filters); + dialog.exec(); + //! [7] + } + { + //! [8] + QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), + "/home", + tr("Images (*.png *.xpm *.jpg)")); + //! [8] + } -//! [11] -QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), - "/home/jana/untitled.png", - tr("Images (*.png *.xpm *.jpg)")); -//! [11] + { + //! [9] + QStringList files = QFileDialog::getOpenFileNames( + this, + "Select one or more files to open", + "/home", + "Images (*.png *.xpm *.jpg)"); + //! [9] + } + { + //! [11] + QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), + "/home/jana/untitled.png", + tr("Images (*.png *.xpm *.jpg)")); + //! [11] + } -//! [12] -QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), - "/home", - QFileDialog::ShowDirsOnly - | QFileDialog::DontResolveSymlinks); -//! [12] + { + //! [12] + QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), + "/home", + QFileDialog::ShowDirsOnly + | QFileDialog::DontResolveSymlinks); + //! [12] + } -//! [13] -QStringList mimeTypeFilters({"image/jpeg", // will show "JPEG image (*.jpeg *.jpg *.jpe) - "image/png", // will show "PNG image (*.png)" - "application/octet-stream" // will show "All files (*)" - }); + { + //! [13] + QStringList mimeTypeFilters({"image/jpeg", // will show "JPEG image (*.jpeg *.jpg *.jpe) + "image/png", // will show "PNG image (*.png)" + "application/octet-stream" // will show "All files (*)" + }); + + QFileDialog dialog(this); + dialog.setMimeTypeFilters(mimeTypeFilters); + dialog.exec(); + //! [13] + } -QFileDialog dialog(this); -dialog.setMimeTypeFilters(mimeTypeFilters); -dialog.exec(); -//! [13] + { + /* For convinient quoting + //! [14] + "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)" + //! [14] + */ + } -//! [14] -"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)" -//! [14] + { + //! [15] + auto fileContentReady = [](const QString &fileName, const QByteArray &fileContent) { + if (fileName.isEmpty()) { + // No file was selected + } else { + // Use fileName and fileContent + } + }; + QFileDialog::getOpenFileContent("Images (*.png *.xpm *.jpg)", fileContentReady); + //! [15] + } -//! [15] -auto fileContentReady = [](const QString &fileName, const QByteArray &fileContent) { - if (fileName.isEmpty()) { - // No file was selected - } else { - // Use fileName and fileContent + { + //! [16] + QByteArray imageData; // obtained from e.g. QImage::save() + QFileDialog::saveFileContent(imageData, "myimage.png"); + //! [16] } -}; -QFileDialog::getOpenFileContent("Images (*.png *.xpm *.jpg)", fileContentReady); -//! [15] - -//! [16] -QByteArray imageData; // obtained from e.g. QImage::save() -QFileDialog::saveFileContent(imageData, "myimage.png"); // with filename hint -// OR -QFileDialog::saveFileContent(imageData); // no filename hint -//! [16] +} diff --git a/src/widgets/doc/snippets/code/src_gui_dialogs_qfontdialog.cpp b/src/widgets/doc/snippets/code/src_gui_dialogs_qfontdialog.cpp index ae6b69cbe8d..a3901965748 100644 --- a/src/widgets/doc/snippets/code/src_gui_dialogs_qfontdialog.cpp +++ b/src/widgets/doc/snippets/code/src_gui_dialogs_qfontdialog.cpp @@ -1,48 +1,68 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -bool ok; -QFont font = QFontDialog::getFont( - &ok, QFont("Helvetica [Cronyx]", 10), this); -if (ok) { - // the user clicked OK and font is set to the font the user selected -} else { - // the user canceled the dialog; font is set to the initial - // value, in this case Helvetica [Cronyx], 10 -} -//! [0] - +#include <QFont> +#include <QWidget> +#include <QFontDialog> -//! [1] -myWidget.setFont(QFontDialog::getFont(0, myWidget.font())); -//! [1] +class MyWidget : public QWidget +{ + Q_OBJECT +public: + void FontDialogExample(QWidget &myWidget); +}; +void MyWidget::FontDialogExample(QWidget &myWidget) +{ + { + //! [0] + bool ok; + QFont font = QFontDialog::getFont( + &ok, QFont("Helvetica [Cronyx]", 10), this); + if (ok) { + // the user clicked OK and font is set to the font the user selected + } else { + // the user canceled the dialog; font is set to the initial + // value, in this case Helvetica [Cronyx], 10 + } + //! [0] + } -//! [2] -bool ok; -QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this); -if (ok) { - // font is set to the font the user selected -} else { - // the user canceled the dialog; font is set to the initial - // value, in this case Times, 12. -} -//! [2] - + { + //! [1] + myWidget.setFont(QFontDialog::getFont(0, myWidget.font())); + //! [1] + } -//! [3] -myWidget.setFont(QFontDialog::getFont(0, myWidget.font())); -//! [3] + { + //! [2] + bool ok; + QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this); + if (ok) { + // font is set to the font the user selected + } else { + // the user canceled the dialog; font is set to the initial + // value, in this case Times, 12. + } + //! [2] + } + { + //! [3] + myWidget.setFont(QFontDialog::getFont(0, myWidget.font())); + //! [3] + } -//! [4] -bool ok; -QFont font = QFontDialog::getFont(&ok, this); -if (ok) { - // font is set to the font the user selected -} else { - // the user canceled the dialog; font is set to the default - // application font, QApplication::font() + { + //! [4] + bool ok; + QFont font = QFontDialog::getFont(&ok, this); + if (ok) { + // font is set to the font the user selected + } else { + // the user canceled the dialog; font is set to the default + // application font, QApplication::font() + } + //! [4] + } } -//! [4] diff --git a/src/widgets/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp b/src/widgets/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp index 062e2e5142a..dffc78db43e 100644 --- a/src/widgets/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp +++ b/src/widgets/doc/snippets/code/src_gui_dialogs_qmessagebox.cpp @@ -1,42 +1,9 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -int ret = QMessageBox::warning(this, tr("My Application"), - tr("The document has been modified.\n" - "Do you want to save your changes?"), - QMessageBox::Save | QMessageBox::Discard - | QMessageBox::Cancel, - QMessageBox::Save); -//! [0] - - -//! [2] -QMessageBox msgBox(this); -QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole); -QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort); - -msgBox.exec(); - -if (msgBox.clickedButton() == connectButton) { - // connect -} else if (msgBox.clickedButton() == abortButton) { - // abort -} -//! [2] - - -//! [3] -QMessageBox messageBox(this); -QAbstractButton *disconnectButton = - messageBox.addButton(tr("Disconnect"), QMessageBox::ActionRole); -... -messageBox.exec(); -if (messageBox.clickedButton() == disconnectButton) { - ... -} -//! [3] - +#include <QWidget> +#include <QAbstractButton> +#include <QPushButton> //! [4] #include <QApplication> @@ -47,39 +14,97 @@ int main(int argc, char *argv[]) QT_REQUIRE_VERSION(argc, argv, "4.0.2") QApplication app(argc, argv); - ... + //... return app.exec(); } //! [4] -//! [5] -QMessageBox msgBox(this); -msgBox.setText("The document has been modified."); -msgBox.exec(); -//! [5] +class MyWidget : public QWidget +{ + Q_OBJECT +public: + void QMessageBoxExample(QWidget &myWidget); +}; + +void MyWidget::QMessageBoxExample(QWidget &myWidget) +{ + { + //! [0] + int ret = QMessageBox::warning(this, tr("My Application"), + tr("The document has been modified.\n" + "Do you want to save your changes?"), + QMessageBox::Save | QMessageBox::Discard + | QMessageBox::Cancel, + QMessageBox::Save); + //! [0] + } + + { + //! [2] + QMessageBox msgBox(this); + QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole); + QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort); + + msgBox.exec(); + + if (msgBox.clickedButton() == connectButton) { + // connect + } else if (msgBox.clickedButton() == abortButton) { + // abort + } + //! [2] + } + + { + //! [3] + QMessageBox messageBox(this); + QAbstractButton *disconnectButton = + messageBox.addButton(tr("Disconnect"), QMessageBox::ActionRole); + //... + messageBox.exec(); + if (messageBox.clickedButton() == disconnectButton) { + //... + } + //! [3] + } + + { + //! [5] + QMessageBox msgBox(this); + msgBox.setText("The document has been modified."); + msgBox.exec(); + //! [5] + } + + { + //! [6] + QMessageBox msgBox(this); + msgBox.setText("The document has been modified."); + msgBox.setInformativeText("Do you want to save your changes?"); + msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); + msgBox.setDefaultButton(QMessageBox::Save); + int ret = msgBox.exec(); + //! [6] + } -//! [6] -QMessageBox msgBox(this); -msgBox.setText("The document has been modified."); -msgBox.setInformativeText("Do you want to save your changes?"); -msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); -msgBox.setDefaultButton(QMessageBox::Save); -int ret = msgBox.exec(); -//! [6] + { + int ret; -//! [7] -switch (ret) { - case QMessageBox::Save: - // Save was clicked - break; - case QMessageBox::Discard: - // Don't Save was clicked - break; - case QMessageBox::Cancel: - // Cancel was clicked - break; - default: - // should never be reached - break; + //! [7] + switch (ret) { + case QMessageBox::Save: + // Save was clicked + break; + case QMessageBox::Discard: + // Don't Save was clicked + break; + case QMessageBox::Cancel: + // Cancel was clicked + break; + default: + // should never be reached + break; + } + //! [7] + } } -//! [7] diff --git a/src/widgets/doc/snippets/code/src_gui_dialogs_qwizard.cpp b/src/widgets/doc/snippets/code/src_gui_dialogs_qwizard.cpp index 328b99e108d..277f478d60a 100644 --- a/src/widgets/doc/snippets/code/src_gui_dialogs_qwizard.cpp +++ b/src/widgets/doc/snippets/code/src_gui_dialogs_qwizard.cpp @@ -1,6 +1,20 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QWidget> +#include <QWizard> + +class LicenseWizard : public QWizard +{ + Q_OBJECT + +public: + enum { Page_Intro, Page_Evaluate, Page_Register, Page_Details, + Page_Conclusion }; + + int nextId() const override; +}; + //! [0] int LicenseWizard::nextId() const { @@ -28,16 +42,22 @@ int LicenseWizard::nextId() const } //! [0] +class MyWizard : public QWizard +{ + Q_OBJECT +public: + MyWizard(QWidget *parent = nullptr); +}; //! [1] MyWizard::MyWizard(QWidget *parent) : QWizard(parent) { - ... + //... QList<QWizard::WizardButton> layout; layout << QWizard::Stretch << QWizard::BackButton << QWizard::CancelButton << QWizard::NextButton << QWizard::FinishButton; setButtonLayout(layout); - ... + //... } //! [1] |
