diff options
| author | Oleksii Zbykovskyi <Oleksii.Zbykovskyi@qt.io> | 2025-07-30 18:46:08 +0200 |
|---|---|---|
| committer | Oleksii Zbykovskyi <Oleksii.Zbykovskyi@qt.io> | 2025-08-01 20:47:43 +0200 |
| commit | 4b022c0fe08d31e39a074d12833a29cf02c67040 (patch) | |
| tree | 93175a119770350da60fe47c74fb393c62254044 /src/gui/doc/snippets/code | |
| parent | 4f3a8a8d68ce804f5d999559ccf11a42a79e3f8f (diff) | |
Make a target for code folder and add each file to the build system
Added each file to the buid system and fixed related issues.
Task-number: QTBUG-137566
Change-Id: I1824beeb176e7ace800e25f28e486349e8df5a2b
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/gui/doc/snippets/code')
8 files changed, 411 insertions, 281 deletions
diff --git a/src/gui/doc/snippets/code/CMakeLists.txt b/src/gui/doc/snippets/code/CMakeLists.txt new file mode 100644 index 00000000000..7f8897ec8fa --- /dev/null +++ b/src/gui/doc/snippets/code/CMakeLists.txt @@ -0,0 +1,76 @@ +# Copyright (C) 2025 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +add_library(gui_snippets_code OBJECT + src_gui_accessible_qaccessible.cpp + src_gui_image_qimage.cpp + src_gui_image_qimagereader.cpp + src_gui_image_qimagewriter.cpp + src_gui_image_qpixmap.cpp + src_gui_image_qpixmapcache.cpp + src_gui_kernel_qevent.cpp + src_gui_kernel_qguiapplication_x11.cpp + src_gui_kernel_qshortcut.cpp + src_gui_kernel_qshortcutmap.cpp + src_gui_math3d_qquaternion.cpp + src_gui_painting_qbrush.cpp + src_gui_painting_qcolor.cpp + src_gui_painting_qpainterpath.cpp + src_gui_painting_qpen.cpp + src_gui_painting_qregion_unix.cpp + src_gui_painting_qregion.cpp + src_gui_painting_qpen.cpp + src_gui_painting_qregion_unix.cpp + src_gui_painting_qregion.cpp + src_gui_text_qfont.cpp + src_gui_text_qfontmetrics.cpp + src_gui_text_qtextcursor.cpp + src_gui_text_qtextdocumentwriter.cpp + src_gui_text_qtextlayout.cpp + src_gui_util_qdesktopservices.cpp + src_gui_util_qundostack.cpp +) + +target_link_libraries(gui_snippets_code PRIVATE + Qt::Core + Qt::Gui +) + +qt_internal_extend_target(gui_snippets_code CONDITION QT_FEATURE_widgets + LIBRARIES + Qt::Widgets + SOURCES + doc_src_coordsys.cpp + doc_src_richtext.cpp + src_gui_image_qicon.cpp + src_gui_image_qmovie.cpp + src_gui_itemviews_qstandarditemmodel.cpp + src_gui_kernel_qaction.cpp + src_gui_kernel_qapplication.cpp + src_gui_kernel_qguiapplication.cpp + src_gui_kernel_qkeysequence.cpp + src_gui_opengl_qopenglfunctions.cpp + src_gui_painting_qpainter.cpp + src_gui_text_qsyntaxhighlighter.cpp + src_gui_util_qvalidator.cpp +) + +qt_internal_extend_target(gui_snippets_code CONDITION QT_FEATURE_vulkan + SOURCES + src_gui_vulkan_qvulkanfunctions.cpp + src_gui_vulkan_qvulkaninstance.cpp + src_gui_vulkan_qvulkanwindow.cpp +) + +qt_internal_extend_target(gui_snippets_code CONDITION QT_FEATURE_clipboard + SOURCES + src_gui_kernel_qclipboard.cpp +) + +set_target_properties(gui_snippets_code PROPERTIES COMPILE_OPTIONS "-w") + +if ("${CMAKE_CXX_COMPILE_FEATURES}" MATCHES "cxx_std_23") + set_property(TARGET gui_snippets_code PROPERTY CXX_STANDARD 23) +endif() + +set_target_properties(gui_snippets_code PROPERTIES UNITY_BUILD OFF) diff --git a/src/gui/doc/snippets/code/src_gui_image_qicon.cpp b/src/gui/doc/snippets/code/src_gui_image_qicon.cpp index 2901ddd592b..66eb7b9c187 100644 --- a/src/gui/doc/snippets/code/src_gui_image_qicon.cpp +++ b/src/gui/doc/snippets/code/src_gui_image_qicon.cpp @@ -3,6 +3,7 @@ #include <QIcon> #include <QPainter> #include <QToolButton> +#include <QSize> namespace src_gui_image_qicon { @@ -20,9 +21,11 @@ QToolButton *button = new QToolButton; button->setIcon(QIcon("open.png")); //! [0] +QSize size(1, 1); + //! [addFile] QIcon openIcon("open.png"); -openIcon.addFile("open-disabled.png", QIcon::Disabled); +openIcon.addFile("open-disabled.png", size ,QIcon::Disabled); //! [addFile] //! [1] @@ -42,6 +45,7 @@ void MyWidget::drawIcon(QPainter *painter, const QRect &rect) } //! [2] +using namespace Qt::StringLiterals; void wrapper1() { diff --git a/src/gui/doc/snippets/code/src_gui_kernel_qaction.cpp b/src/gui/doc/snippets/code/src_gui_kernel_qaction.cpp index 054b40d7c07..479c1882a25 100644 --- a/src/gui/doc/snippets/code/src_gui_kernel_qaction.cpp +++ b/src/gui/doc/snippets/code/src_gui_kernel_qaction.cpp @@ -1,12 +1,21 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QApplication app(argc, argv); -app.setAttribute(Qt::AA_DontShowIconsInMenus); // Icons are *no longer shown* in menus -// ... -QAction *myAction = new QAction(); -// ... -myAction->setIcon(SomeIcon); -myAction->setIconVisibleInMenu(true); // Icon *will* be shown in menus for *this* action. -//! [0] +#include <QApplication> +#include <QAction> +#include <QIcon> + +int main(int argc, char **argv) +{ + QIcon SomeIcon(":/path/to/icon.png"); + + //! [0] + QApplication app(argc, argv); + app.setAttribute(Qt::AA_DontShowIconsInMenus); // Icons are *no longer shown* in menus + // ... + QAction *myAction = new QAction(); + // ... + myAction->setIcon(SomeIcon); + myAction->setIconVisibleInMenu(true); // Icon *will* be shown in menus for *this* action. + //! [0] +} diff --git a/src/gui/doc/snippets/code/src_gui_kernel_qkeysequence.cpp b/src/gui/doc/snippets/code/src_gui_kernel_qkeysequence.cpp index 0f335dd0a04..b20d3301c5e 100644 --- a/src/gui/doc/snippets/code/src_gui_kernel_qkeysequence.cpp +++ b/src/gui/doc/snippets/code/src_gui_kernel_qkeysequence.cpp @@ -6,34 +6,38 @@ namespace src_gui_kernel_qkeysequence { -struct Wrapper : public QWidget -{ void wrapper(); }; -/* Wrap non-compilable code snippet - -//! [0] -QKeySequence(QKeySequence::Print); -QKeySequence(tr("Ctrl+P")); -QKeySequence(tr("Ctrl+p")); -QKeySequence(Qt::CTRL | Qt::Key_P); -QKeySequence(Qt::CTRL + Qt::Key_P); // deprecated -//! [0] - - -//! [1] -QKeySequence(tr("Ctrl+X, Ctrl+C")); -QKeySequence(Qt::CTRL | Qt::Key_X, Qt::CTRL | Qt::Key_C); -QKeySequence(Qt::CTRL + Qt::Key_X, Qt::CTRL + Qt::Key_C); // deprecated -//! [1] - -*/ // Wrap non-compilable code snippet - -void Wrapper::wrapper() { -//! [2] -QMenu *file = new QMenu(this); -file->addAction(tr("&Open..."), QKeySequence(tr("Ctrl+O", "File|Open")), - this, &MainWindow::open); -//! [2] - -} // Wrapper::wrapper -} // src_gui_kernel_qkeysequence + struct MainWindow : public QWidget + { + void wrapper(); + void open(); + }; + + /* Wrap non-compilable code snippet + + //! [0] + QKeySequence(QKeySequence::Print); + QKeySequence(tr("Ctrl+P")); + QKeySequence(tr("Ctrl+p")); + QKeySequence(Qt::CTRL | Qt::Key_P); + QKeySequence(Qt::CTRL + Qt::Key_P); // deprecated + //! [0] + + + //! [1] + QKeySequence(tr("Ctrl+X, Ctrl+C")); + QKeySequence(Qt::CTRL | Qt::Key_X, Qt::CTRL | Qt::Key_C); + QKeySequence(Qt::CTRL + Qt::Key_X, Qt::CTRL + Qt::Key_C); // deprecated + //! [1] + + */ // Wrap non-compilable code snippet + + void MainWindow::wrapper() + { + //! [2] + QMenu *file = new QMenu(this); + file->addAction(tr("&Open..."), QKeySequence(tr("Ctrl+O", "File|Open")), + this, &MainWindow::open); + //! [2] + } +} diff --git a/src/gui/doc/snippets/code/src_gui_kernel_qshortcut.cpp b/src/gui/doc/snippets/code/src_gui_kernel_qshortcut.cpp index d67aea12880..f7dd9380679 100644 --- a/src/gui/doc/snippets/code/src_gui_kernel_qshortcut.cpp +++ b/src/gui/doc/snippets/code/src_gui_kernel_qshortcut.cpp @@ -1,18 +1,29 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -shortcut = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")), - parent); -//! [0] +#include <QShortcut> +#include <QKeySequence> +class MyShortcut : public QShortcut +{ + void example() + { + QShortcut *shortcut; + QObject *parent; + //! [0] + shortcut = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")), + parent); + //! [0] -//! [1] -setKey(0); // no signal emitted -setKey(QKeySequence()); // no signal emitted -setKey(0x3b1); // Greek letter alpha -setKey(Qt::Key_D); // 'd', e.g. to delete -setKey('q'); // 'q', e.g. to quit -setKey(Qt::CTRL | Qt::Key_P); // Ctrl+P, e.g. to print document -setKey("Ctrl+P"); // Ctrl+P, e.g. to print document -//! [1] + + //! [1] + setKey(0); // no signal emitted + setKey(QKeySequence()); // no signal emitted + setKey(0x3b1); // Greek letter alpha + setKey(Qt::Key_D); // 'd', e.g. to delete + setKey('q'); // 'q', e.g. to quit + setKey(Qt::CTRL | Qt::Key_P); // Ctrl+P, e.g. to print document + setKey(tr("Ctrl+P")); // Ctrl+P, e.g. to print document + //! [1] + } +}; diff --git a/src/gui/doc/snippets/code/src_gui_painting_qpainter.cpp b/src/gui/doc/snippets/code/src_gui_painting_qpainter.cpp index cfbf3e44a2d..135d7555dad 100644 --- a/src/gui/doc/snippets/code/src_gui_painting_qpainter.cpp +++ b/src/gui/doc/snippets/code/src_gui_painting_qpainter.cpp @@ -76,112 +76,111 @@ void MyWidget::paintEvent(QPaintEvent *) //! [2] -void wrapper0() { -QPainter *painter = nullptr; -QPainter *painter2 = nullptr; -QPaintDevice *myWidget = nullptr; -//! [3] -painter->begin(0); // impossible - paint device cannot be 0 - -QPixmap image(0, 0); -painter->begin(&image); // impossible - image.isNull() == true; - -painter->begin(myWidget); -painter2->begin(myWidget); // impossible - only one painter at a time -//! [3] - +void wrapper0() +{ + QPainter *painter = nullptr; + QPainter *painter2 = nullptr; + QPaintDevice *myWidget = nullptr; + //! [3] + painter->begin(nullptr); // impossible - paint device cannot be null + + QPixmap image(0, 0); + painter->begin(&image); // impossible - image.isNull() == true; + + painter->begin(myWidget); + painter2->begin(myWidget); // impossible - only one painter at a time + //! [3] } // wrapper0 -namespace QPainterWrapper { -struct QPainter { - void rotate(qreal angle); - void setWorldTransform(QTransform matrix, bool); -}; - +namespace QPainterWrapper +{ + struct QPainter { + void rotate(qreal angle); + void setWorldTransform(QTransform matrix, bool); + }; } // QPainterWrapper -void MyWidget::wrapper1() { -//! [5] -QPainterPath path; -path.moveTo(20, 80); -path.lineTo(20, 30); -path.cubicTo(80, 0, 50, 50, 80, 80); - -QPainter painter(this); -painter.drawPath(path); -//! [5] +void MyWidget::wrapper1() +{ + { + //! [5] + QPainterPath path; + path.moveTo(20, 80); + path.lineTo(20, 30); + path.cubicTo(80, 0, 50, 50, 80, 80); + QPainter painter(this); + painter.drawPath(path); + //! [5] + } -//! [6] -QLineF line(10.0, 80.0, 90.0, 20.0); + { + //! [6] + QLineF line(10.0, 80.0, 90.0, 20.0); -QPainter painter(this); -painter.drawLine(line); -//! [6] + QPainter painter(this); + painter.drawLine(line); + //! [6] + } } // MyWidget::wrapper1() -void MyWidget::wrapper2() { - -//! [7] -QRectF rectangle(10.0, 20.0, 80.0, 60.0); - -QPainter painter(this); -painter.drawRect(rectangle); -//! [7] +void MyWidget::wrapper2() +{ + //! [7] + QRectF rectangle(10.0, 20.0, 80.0, 60.0); + QPainter painter(this); + painter.drawRect(rectangle); + //! [7] } // MyWidget::wrapper2 -void MyWidget::wrapper3() { - -//! [8] -QRectF rectangle(10.0, 20.0, 80.0, 60.0); - -QPainter painter(this); -painter.drawRoundedRect(rectangle, 20.0, 15.0); -//! [8] +void MyWidget::wrapper3() +{ + //! [8] + QRectF rectangle(10.0, 20.0, 80.0, 60.0); + QPainter painter(this); + painter.drawRoundedRect(rectangle, 20.0, 15.0); + //! [8] } // MyWidget::wrapper3 -void MyWidget::wrapper4() { - -//! [9] -QRectF rectangle(10.0, 20.0, 80.0, 60.0); - -QPainter painter(this); -painter.drawEllipse(rectangle); -//! [9] +void MyWidget::wrapper4() +{ + //! [9] + QRectF rectangle(10.0, 20.0, 80.0, 60.0); + QPainter painter(this); + painter.drawEllipse(rectangle); + //! [9] } // MyWidget::wrapper4 -void MyWidget::wrapper5() { - -//! [10] -QRectF rectangle(10.0, 20.0, 80.0, 60.0); -int startAngle = 30 * 16; -int spanAngle = 120 * 16; - -QPainter painter(this); -painter.drawArc(rectangle, startAngle, spanAngle); -//! [10] +void MyWidget::wrapper5() +{ + //! [10] + QRectF rectangle(10.0, 20.0, 80.0, 60.0); + int startAngle = 30 * 16; + int spanAngle = 120 * 16; + QPainter painter(this); + painter.drawArc(rectangle, startAngle, spanAngle); + //! [10] } // MyWidget::wrapper5 -void MyWidget::wrapper6() { - -//! [11] -QRectF rectangle(10.0, 20.0, 80.0, 60.0); -int startAngle = 30 * 16; -int spanAngle = 120 * 16; - -QPainter painter(this); -painter.drawPie(rectangle, startAngle, spanAngle); -//! [11] +void MyWidget::wrapper6() +{ + //! [11] + QRectF rectangle(10.0, 20.0, 80.0, 60.0); + int startAngle = 30 * 16; + int spanAngle = 120 * 16; + QPainter painter(this); + painter.drawPie(rectangle, startAngle, spanAngle); + //! [11] } // MyWidget::wrapper6 @@ -200,73 +199,74 @@ Q_UNUSED(rectangle); } // MyWidget::wrapper7 -void MyWidget::wrapper8() { - -//! [13] -static const QPointF points[3] = { - QPointF(10.0, 80.0), - QPointF(20.0, 10.0), - QPointF(80.0, 30.0), -}; - -QPainter painter(this); -painter.drawPolyline(points, 3); -//! [13] +void MyWidget::wrapper8() +{ + //! [13] + static const QPointF points[3] = { + QPointF(10.0, 80.0), + QPointF(20.0, 10.0), + QPointF(80.0, 30.0), + }; + QPainter painter(this); + painter.drawPolyline(points, 3); + //! [13] } // MyWidget::wrapper8 -void MyWidget::wrapper9() { -//! [14] -static const QPointF points[4] = { - QPointF(10.0, 80.0), - QPointF(20.0, 10.0), - QPointF(80.0, 30.0), - QPointF(90.0, 70.0) -}; - -QPainter painter(this); -painter.drawPolygon(points, 4); -//! [14] +void MyWidget::wrapper9() +{ + //! [14] + static const QPointF points[4] = { + QPointF(10.0, 80.0), + QPointF(20.0, 10.0), + QPointF(80.0, 30.0), + QPointF(90.0, 70.0) + }; + QPainter painter(this); + painter.drawPolygon(points, 4); + //! [14] } // MyWidget::wrapper9 -void MyWidget::wrapper10() { - -//! [15] -static const QPointF points[4] = { - QPointF(10.0, 80.0), - QPointF(20.0, 10.0), - QPointF(80.0, 30.0), - QPointF(90.0, 70.0) -}; - -QPainter painter(this); -painter.drawConvexPolygon(points, 4); -//! [15] - +void MyWidget::wrapper10() +{ + { + //! [15] + static const QPointF points[4] = { + QPointF(10.0, 80.0), + QPointF(20.0, 10.0), + QPointF(80.0, 30.0), + QPointF(90.0, 70.0) + }; -//! [16] -QRectF target(10.0, 20.0, 80.0, 60.0); -QRectF source(0.0, 0.0, 70.0, 40.0); -QPixmap pixmap(":myPixmap.png"); + QPainter painter(this); + painter.drawConvexPolygon(points, 4); + //! [15] + } -QPainter painter(this); -painter.drawPixmap(target, pixmap, source); -//! [16] + { + //! [16] + QRectF target(10.0, 20.0, 80.0, 60.0); + QRectF source(0.0, 0.0, 70.0, 40.0); + QPixmap pixmap(":myPixmap.png"); + QPainter painter(this); + painter.drawPixmap(target, pixmap, source); + //! [16] + } } // MyWidget::wrapper10 -void MyWidget::wrapper11() { -QRect rect; - -//! [17] -QPainter painter(this); -painter.drawText(rect, Qt::AlignCenter, tr("Qt\nProject")); -//! [17] +void MyWidget::wrapper11() +{ + QRect rect; + //! [17] + QPainter painter(this); + painter.drawText(rect, Qt::AlignCenter, tr("Qt\nProject")); + //! [17] } // MyWidget::wrapper11 @@ -275,101 +275,98 @@ QRectF fillRect(QRect rect, int background) { Q_UNUSED(background); return QRectF(); }; -void MyWidget::wrapper12() { -QRect rectangle; -//! [18] -QPicture picture; -QPointF point(10.0, 20.0); -picture.load("drawing.pic"); +void MyWidget::wrapper12() +{ + QRect rectangle; -QPainter painter(this); -painter.drawPicture(0, 0, picture); -//! [18] + //! [18] + QPicture picture; + QPointF point(10.0, 20.0); + picture.load("drawing.pic"); -Q_UNUSED(point); + QPainter painter(this); + painter.drawPicture(0, 0, picture); + //! [18] + Q_UNUSED(point); -//! [19] -fillRect(rectangle, background()); -//! [19] + //! [19] + fillRect(rectangle, background()); + //! [19] } // MyWidget::wrapper12 -void MyWidget::wrapper13() { - -//! [20] -QRectF target(10.0, 20.0, 80.0, 60.0); -QRectF source(0.0, 0.0, 70.0, 40.0); -QImage image(":/images/myImage.png"); - -QPainter painter(this); -painter.drawImage(target, image, source); -//! [20] +void MyWidget::wrapper13() +{ + //! [20] + QRectF target(10.0, 20.0, 80.0, 60.0); + QRectF source(0.0, 0.0, 70.0, 40.0); + QImage image(":/images/myImage.png"); + QPainter painter(this); + painter.drawImage(target, image, source); + //! [20] } // MyWidget::wrapper13 -void MyWidget::wrapper14() { - -//! [21] -QPainter painter(this); -painter.fillRect(0, 0, 128, 128, Qt::green); -painter.beginNativePainting(); - -glEnable(GL_SCISSOR_TEST); -glScissor(0, 0, 64, 64); +void MyWidget::wrapper14() +{ + //! [21] + QPainter painter(this); + painter.fillRect(0, 0, 128, 128, Qt::green); + painter.beginNativePainting(); -glClearColor(1, 0, 0, 1); -glClear(GL_COLOR_BUFFER_BIT); + glEnable(GL_SCISSOR_TEST); + glScissor(0, 0, 64, 64); -glDisable(GL_SCISSOR_TEST); + glClearColor(1, 0, 0, 1); + glClear(GL_COLOR_BUFFER_BIT); -painter.endNativePainting(); -//! [21] + glDisable(GL_SCISSOR_TEST); + painter.endNativePainting(); + //! [21] } // MyWidget::wrapper14 -void MyWidget::wrapper15() { - -//! [drawText] -QPainter painter(this); -QFont font = painter.font(); -font.setPixelSize(48); -painter.setFont(font); - -const QRect rectangle = QRect(0, 0, 100, 50); -QRect boundingRect; -painter.drawText(rectangle, 0, tr("Hello"), &boundingRect); - -QPen pen = painter.pen(); -pen.setStyle(Qt::DotLine); -painter.setPen(pen); -painter.drawRect(boundingRect.adjusted(0, 0, -pen.width(), -pen.width())); - -pen.setStyle(Qt::DashLine); -painter.setPen(pen); -painter.drawRect(rectangle.adjusted(0, 0, -pen.width(), -pen.width())); -//! [drawText] - - +void MyWidget::wrapper15() +{ + //! [drawText] + QPainter painter(this); + QFont font = painter.font(); + font.setPixelSize(48); + painter.setFont(font); + + const QRect rectangle = QRect(0, 0, 100, 50); + QRect boundingRect; + painter.drawText(rectangle, 0, tr("Hello"), &boundingRect); + + QPen pen = painter.pen(); + pen.setStyle(Qt::DotLine); + painter.setPen(pen); + painter.drawRect(boundingRect.adjusted(0, 0, -pen.width(), -pen.width())); + + pen.setStyle(Qt::DashLine); + painter.setPen(pen); + painter.drawRect(rectangle.adjusted(0, 0, -pen.width(), -pen.width())); + //! [drawText] } // MyWidget::wrapper15 void MyWidget::concentricCircles() { -//! [renderHint] + //! [renderHint] QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); -//! [renderHint] + //! [renderHint] int diameter = 50; -//! [floatBased] + //! [floatBased] painter.drawEllipse(QRectF(-diameter / 2.0, -diameter / 2.0, diameter, diameter)); -//! [floatBased] -//! [intBased] + //! [floatBased] + //! [intBased] painter.drawEllipse(QRect(-diameter / 2, -diameter / 2, diameter, diameter)); -//! [intBased] + //! [intBased] } // MyWidget::concentricCircles diff --git a/src/gui/doc/snippets/code/src_gui_painting_qpainterstateguard.cpp b/src/gui/doc/snippets/code/src_gui_painting_qpainterstateguard.cpp index adcccd60aa1..f35343ee1b4 100644 --- a/src/gui/doc/snippets/code/src_gui_painting_qpainterstateguard.cpp +++ b/src/gui/doc/snippets/code/src_gui_painting_qpainterstateguard.cpp @@ -42,7 +42,7 @@ void MyGuardWidget::paintEvent(QPaintEvent *) QPainter painter(this); painter.setPen(Qt::red); if (drawText) { - QPainterStateGuard guard(&painter) + QPainterStateGuard guard(&painter); painter.setPen(Qt::blue); painter.setFont(QFont("Arial", 30)); painter.drawText(rect(), Qt::AlignCenter, "Qt"); diff --git a/src/gui/doc/snippets/code/src_gui_util_qundostack.cpp b/src/gui/doc/snippets/code/src_gui_util_qundostack.cpp index fe3ced1d025..e297f4a0264 100644 --- a/src/gui/doc/snippets/code/src_gui_util_qundostack.cpp +++ b/src/gui/doc/snippets/code/src_gui_util_qundostack.cpp @@ -1,6 +1,8 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QUndoCommand> + //! [0] class AppendText : public QUndoCommand { @@ -11,36 +13,61 @@ public: { m_document->chop(m_text.length()); } void redo() override { m_document->append(m_text); } + bool mergeWith(const QUndoCommand *other) override; private: QString *m_document; QString m_text; }; //! [0] +struct MyCommand : public QUndoCommand +{ + MyCommand() { setText("My Command"); } + void undo() override {} + void redo() override {} + int id() const override { return 123; } // unique id for this command +}; -//! [1] -MyCommand *command1 = new MyCommand(); -stack->push(command1); -MyCommand *command2 = new MyCommand(); -stack->push(command2); +struct InsertText : public QUndoCommand +{ + InsertText(QString *doc, int idx, const QString &text, QUndoCommand *parent = nullptr); +}; -stack->undo(); +struct SetColor : public QUndoCommand +{ + SetColor(QString *doc, int idx, int len, const Qt::GlobalColor &color, QUndoCommand *parent = nullptr); +}; -MyCommand *command3 = new MyCommand(); -stack->push(command3); // command2 gets deleted -//! [1] +void examples(QUndoStack *stack, QString *document, int idx, const QString &text) +{ + { + //! [1] + MyCommand *command1 = new MyCommand(); + stack->push(command1); + MyCommand *command2 = new MyCommand(); + stack->push(command2); + + stack->undo(); + MyCommand *command3 = new MyCommand(); + stack->push(command3); // command2 gets deleted + //! [1] + } -//! [2] -QUndoCommand *insertRed = new QUndoCommand(); // an empty command -insertRed->setText("insert red text"); + { + QUndoStack stack; -new InsertText(document, idx, text, insertRed); // becomes child of insertRed -new SetColor(document, idx, text.length(), Qt::red, insertRed); + //! [2] + QUndoCommand *insertRed = new QUndoCommand(); // an empty command + insertRed->setText("insert red text"); -stack.push(insertRed); -//! [2] + new InsertText(document, idx, text, insertRed); // becomes child of insertRed + new SetColor(document, idx, text.length(), Qt::red, insertRed); + stack.push(insertRed); + //! [2] + } +} //! [3] bool AppendText::mergeWith(const QUndoCommand *other) @@ -52,21 +79,23 @@ bool AppendText::mergeWith(const QUndoCommand *other) } //! [3] - -//! [4] -stack.beginMacro("insert red text"); -stack.push(new InsertText(document, idx, text)); -stack.push(new SetColor(document, idx, text.length(), Qt::red)); -stack.endMacro(); // indexChanged() is emitted -//! [4] +void wrap( QUndoStack &stack, QString *document, int idx, const QString &text) +{ + //! [4] + stack.beginMacro("insert red text"); + stack.push(new InsertText(document, idx, text)); + stack.push(new SetColor(document, idx, text.length(), Qt::red)); + stack.endMacro(); // indexChanged() is emitted + //! [4] -//! [5] -QUndoCommand *insertRed = new QUndoCommand(); // an empty command -insertRed->setText("insert red text"); + //! [5] + QUndoCommand *insertRed = new QUndoCommand(); // an empty command + insertRed->setText("insert red text"); -new InsertText(document, idx, text, insertRed); // becomes child of insertRed -new SetColor(document, idx, text.length(), Qt::red, insertRed); + new InsertText(document, idx, text, insertRed); // becomes child of insertRed + new SetColor(document, idx, text.length(), Qt::red, insertRed); -stack.push(insertRed); -//! [5] + stack.push(insertRed); + //! [5] +} |
