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>
This commit is contained in:
parent
94fe42a753
commit
a426326e99
@ -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]
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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]
|
||||
}
|
||||
|
63
src/widgets/doc/snippets/graphicsview_snippet.cpp
Normal file
63
src/widgets/doc/snippets/graphicsview_snippet.cpp
Normal file
@ -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]
|
@ -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();
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
16
src/widgets/doc/snippets/snippets.pro
Normal file
16
src/widgets/doc/snippets/snippets.pro
Normal file
@ -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
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user