summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNico Vertriest <nico.vertriest@qt.io>2020-02-03 15:49:30 +0100
committerPaul Wicking <paul.wicking@qt.io>2020-05-01 06:44:07 +0200
commita426326e99a76a92c8d0c870e19c67311a434483 (patch)
tree44d7ff8ae2dda7194484e0339cf35ceb27c5c583
parent94fe42a7538b1a27b148b3c09919e03a06404610 (diff)
Doc: Make snippets Qt Widgets compilable
Removed mainwindowsnippet.cpp and widgetdelegate.cpp from snippets.pro Task-number: QTBUG-81497 Change-Id: I40d1f34e64d958d2fb857dc8e468b9c40fff527c Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Kai Koehne <kai.koehne@qt.io> Reviewed-by: Topi Reiniƶ <topi.reinio@qt.io>
-rw-r--r--src/widgets/doc/snippets/customviewstyle.cpp7
-rw-r--r--src/widgets/doc/snippets/filedialogurls.cpp8
-rw-r--r--src/widgets/doc/snippets/graphicssceneadditemsnippet.cpp17
-rw-r--r--src/widgets/doc/snippets/graphicsview.cpp64
-rw-r--r--src/widgets/doc/snippets/graphicsview_snippet.cpp63
-rw-r--r--src/widgets/doc/snippets/mdiareasnippets.cpp3
-rw-r--r--src/widgets/doc/snippets/myscrollarea.cpp23
-rw-r--r--src/widgets/doc/snippets/snippets.pro16
-rw-r--r--src/widgets/doc/src/graphicsview.qdoc2
9 files changed, 138 insertions, 65 deletions
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 <QtWidgets>
-#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 <QtGui>
+#include <QFileDialog>
-int main(int argv, char **args)
+int loadFileDialog()
{
- QApplication app(argv, args);
-
//![0]
QList<QUrl> 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 <QtGui>
+#include <QGraphicsScene>
+#include <QGraphicsEllipseItem>
+#include <QStyleOptionGraphicsItem>
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 <QGraphicsView>
+#include <QOpenGLWidget>
+#include <QPrinter>
+#include <QPrintDialog>
+#include <QStandardItem>
+#include <QMimeData>
+#include <QDrag>
+#include <QGraphicsSceneMouseEvent>
+
+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 <QtGui>
+#include <QtWidgets>
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
+
diff --git a/src/widgets/doc/src/graphicsview.qdoc b/src/widgets/doc/src/graphicsview.qdoc
index 94c824c2ed..46a68adc56 100644
--- a/src/widgets/doc/src/graphicsview.qdoc
+++ b/src/widgets/doc/src/graphicsview.qdoc
@@ -327,7 +327,7 @@
Here is an example of how to implement zoom and rotate slots in a
subclass of QGraphicsView:
- \snippet graphicsview.cpp 2
+ \snippet graphicsview_snippet.cpp 2
The slots could be connected to \l{QToolButton}{QToolButtons} with
\l{QAbstractButton::autoRepeat}{autoRepeat} enabled.