From a426326e99a76a92c8d0c870e19c67311a434483 Mon Sep 17 00:00:00 2001 From: Nico Vertriest Date: Mon, 3 Feb 2020 15:49:30 +0100 Subject: Doc: Make snippets Qt Widgets compilable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removed mainwindowsnippet.cpp and widgetdelegate.cpp from snippets.pro Task-number: QTBUG-81497 Change-Id: I40d1f34e64d958d2fb857dc8e468b9c40fff527c Reviewed-by: Paul Wicking Reviewed-by: Kai Koehne Reviewed-by: Topi Reiniƶ --- src/widgets/doc/snippets/customviewstyle.cpp | 7 +-- src/widgets/doc/snippets/filedialogurls.cpp | 8 +-- .../doc/snippets/graphicssceneadditemsnippet.cpp | 17 ++---- src/widgets/doc/snippets/graphicsview.cpp | 64 ++++++++++++++-------- src/widgets/doc/snippets/graphicsview_snippet.cpp | 63 +++++++++++++++++++++ src/widgets/doc/snippets/mdiareasnippets.cpp | 3 +- src/widgets/doc/snippets/myscrollarea.cpp | 23 ++------ src/widgets/doc/snippets/snippets.pro | 16 ++++++ 8 files changed, 137 insertions(+), 64 deletions(-) create mode 100644 src/widgets/doc/snippets/graphicsview_snippet.cpp create mode 100644 src/widgets/doc/snippets/snippets.pro (limited to 'src/widgets/doc/snippets') diff --git a/src/widgets/doc/snippets/customviewstyle.cpp b/src/widgets/doc/snippets/customviewstyle.cpp index b9c10cb31d..1e4cf2b711 100644 --- a/src/widgets/doc/snippets/customviewstyle.cpp +++ b/src/widgets/doc/snippets/customviewstyle.cpp @@ -50,11 +50,10 @@ #include -#include "customviewstyle.h" +#include "./customstyle/customstyle.h" - - -void CustomViewStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const +void CustomStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option, + QPainter *painter, const QWidget *widget) const { //![0] diff --git a/src/widgets/doc/snippets/filedialogurls.cpp b/src/widgets/doc/snippets/filedialogurls.cpp index cd91e797e7..4b73a6c818 100644 --- a/src/widgets/doc/snippets/filedialogurls.cpp +++ b/src/widgets/doc/snippets/filedialogurls.cpp @@ -49,11 +49,10 @@ ****************************************************************************/ #include +#include -int main(int argv, char **args) +int loadFileDialog() { - QApplication app(argv, args); - //![0] QList urls; urls << QUrl::fromLocalFile("/Users/foo/Code/qt5") @@ -66,6 +65,5 @@ int main(int argv, char **args) // ... } //![0] - - return app.exec(); + return 1; } diff --git a/src/widgets/doc/snippets/graphicssceneadditemsnippet.cpp b/src/widgets/doc/snippets/graphicssceneadditemsnippet.cpp index 96e6bd650c..0ce135dc63 100644 --- a/src/widgets/doc/snippets/graphicssceneadditemsnippet.cpp +++ b/src/widgets/doc/snippets/graphicssceneadditemsnippet.cpp @@ -49,6 +49,9 @@ ****************************************************************************/ #include +#include +#include +#include class CustomScene : public QGraphicsScene { @@ -70,21 +73,9 @@ void CustomScene::drawItems(QPainter *painter, int numItems, for (int i = 0; i < numItems; ++i) { // Draw the item painter->save(); - painter->setMatrix(items[i]->sceneMatrix(), true); + painter->setTransform(items[i]->sceneTransform(), true); items[i]->paint(painter, &options[i], widget); painter->restore(); } } //! [0] - -int main(int argv, char **args) -{ - QApplication app(argv, args); - - CustomScene scene; - QGraphicsView view(&scene); - - view.show(); - - return app.exec(); -} diff --git a/src/widgets/doc/snippets/graphicsview.cpp b/src/widgets/doc/snippets/graphicsview.cpp index 9578f91eec..6262137c90 100644 --- a/src/widgets/doc/snippets/graphicsview.cpp +++ b/src/widgets/doc/snippets/graphicsview.cpp @@ -47,53 +47,70 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ - +#include +#include +#include +#include +#include +#include +#include +#include + +int main() +{ //! [0] QGraphicsScene scene; QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100)); -QGraphicsItem *item = scene.itemAt(50, 50); -// item == rect +QGraphicsItem *item = scene.itemAt(50, 50, QTransform()); //! [0] +Q_UNUSED(rect); +Q_UNUSED(item); +} +void myPopulateScene(QGraphicsScene *) +{ + // Intentionally left empty +} +void snippetThatUsesMyPopulateScene() +{ //! [1] QGraphicsScene scene; myPopulateScene(&scene); - QGraphicsView view(&scene); view.show(); //! [1] +} - -//! [2] -class View : public QGraphicsView +class CustomItem : public QStandardItem { -Q_OBJECT - ... -public slots: - void zoomIn() { scale(1.2, 1.2); } - void zoomOut() { scale(1 / 1.2, 1 / 1.2); } - void rotateLeft() { rotate(-10); } - void rotateRight() { rotate(10); } - ... +public: + using QStandardItem::QStandardItem; + + int type() const override { return UserType; } + void mousePressEvent(QGraphicsSceneMouseEvent *event); + QStandardItem *clone() const override { return new CustomItem; } }; -//! [2] +void printScene() +{ //! [3] QGraphicsScene scene; +QPrinter printer; scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green)); -QPrinter printer; if (QPrintDialog(&printer).exec() == QDialog::Accepted) { QPainter painter(&printer); painter.setRenderHint(QPainter::Antialiasing); scene.render(&painter); } //! [3] +} - +void pixmapScene() +{ //! [4] QGraphicsScene scene; scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green)); @@ -106,21 +123,21 @@ painter.end(); pixmap.save("scene.png"); //! [4] - +} //! [5] void CustomItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { QMimeData *data = new QMimeData; - data->setColor(Qt::green); - QDrag *drag = new QDrag(event->widget()); drag->setMimeData(data); - drag->start(); + drag->exec(); } //! [5] - +void viewScene() +{ +QGraphicsScene scene; //! [6] QGraphicsView view(&scene); QOpenGLWidget *gl = new QOpenGLWidget(); @@ -129,3 +146,4 @@ format.setSamples(4); gl->setFormat(format); view.setViewport(gl); //! [6] +} diff --git a/src/widgets/doc/snippets/graphicsview_snippet.cpp b/src/widgets/doc/snippets/graphicsview_snippet.cpp new file mode 100644 index 0000000000..9d058d0f6a --- /dev/null +++ b/src/widgets/doc/snippets/graphicsview_snippet.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation 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$ +** +****************************************************************************/ + +//! [2] +class View : public QGraphicsView +{ +Q_OBJECT + ... +public slots: + void zoomIn() { scale(1.2, 1.2); } + void zoomOut() { scale(1 / 1.2, 1 / 1.2); } + void rotateLeft() { rotate(-10); } + void rotateRight() { rotate(10); } + ... +}; +//! [2] diff --git a/src/widgets/doc/snippets/mdiareasnippets.cpp b/src/widgets/doc/snippets/mdiareasnippets.cpp index dec7aaa1e7..c1e2d37ccb 100644 --- a/src/widgets/doc/snippets/mdiareasnippets.cpp +++ b/src/widgets/doc/snippets/mdiareasnippets.cpp @@ -85,6 +85,7 @@ void addingSubWindowsExample() mdiArea.show(); } +/* int main(int argv, char **args) { QApplication app(argv, args); @@ -103,5 +104,5 @@ int main(int argv, char **args) return app.exec(); } - +*/ diff --git a/src/widgets/doc/snippets/myscrollarea.cpp b/src/widgets/doc/snippets/myscrollarea.cpp index dbf8da1603..8afe4a6834 100644 --- a/src/widgets/doc/snippets/myscrollarea.cpp +++ b/src/widgets/doc/snippets/myscrollarea.cpp @@ -48,7 +48,7 @@ ** ****************************************************************************/ -#include +#include class MyScrollArea : public QAbstractScrollArea { @@ -97,8 +97,10 @@ void MyScrollArea::updateWidgetPosition() //! [0] } -void MyScrollArea::scrollContentsBy(int /*dx*/, int /*dy*/) +void MyScrollArea::scrollContentsBy(int dx, int dy) { + Q_UNUSED(dx); + Q_UNUSED(dy); updateWidgetPosition(); } @@ -118,21 +120,6 @@ void MyScrollArea::updateArea() void MyScrollArea::resizeEvent(QResizeEvent *event) { + Q_UNUSED(event); updateArea(); } - -int main(int argv, char **args) -{ - QApplication app(argv, args); - - QPixmap pixmap("mypixmap.png"); - QLabel label; - label.setPixmap(pixmap); - MyScrollArea area(&label); - area.resize(300, 300); - area.show(); - - area.setWidget(&label); - - return app.exec(); -} diff --git a/src/widgets/doc/snippets/snippets.pro b/src/widgets/doc/snippets/snippets.pro new file mode 100644 index 0000000000..f1d3596f9d --- /dev/null +++ b/src/widgets/doc/snippets/snippets.pro @@ -0,0 +1,16 @@ +requires(qtHaveModule(widgets)) +requires(qtHaveModule(printsupport)) + +TEMPLATE = app + +TARGET = widgets_snippets + +QT += widgets printsupport + +SOURCES += customviewstyle.cpp \ + filedialogurls.cpp \ + graphicssceneadditemsnippet.cpp \ + graphicsview.cpp \ + mdiareasnippets.cpp \ + myscrollarea.cpp + -- cgit v1.2.3