Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_gui_graphicsview_qgraphicsview.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QtWidgets>
5#include <QPrinter>
6
7void examples()
8{
9 {
10 //! [0]
11 QGraphicsScene scene;
12 scene.addText("Hello, world!");
13
14 QGraphicsView view(&scene);
15 view.show();
16 //! [0]
17 }
18
19 {
20 //! [1]
21 QGraphicsScene scene;
22 scene.addRect(QRectF(-10, -10, 20, 20));
23
24 QGraphicsView view(&scene);
25 view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
26 view.show();
27 //! [1]
28 }
29
30 {
31 //! [2]
32 QGraphicsView view;
33 view.setBackgroundBrush(QImage(":/images/backgroundtile.png"));
34 view.setCacheMode(QGraphicsView::CacheBackground);
35 //! [2]
36 }
37
38 {
39 //! [4]
40 QGraphicsScene scene;
41 scene.addItem(someItem);
42 //...
43
44 QGraphicsView view(&scene);
45 view.show();
46 //...
47
48 QPrinter printer(QPrinter::HighResolution);
49 printer.setDocName("Example");
50 QPainter painter(&printer);
51
52 // print, fitting the viewport contents into a full page
53 view.render(&painter);
54
55 // print the upper half of the viewport into the lower.
56 // half of the page.
57 QRect viewport = view.viewport()->rect();
58 view.render(&painter,
59 QRectF(0, printer.height() / 2,
60 printer.width(), printer.height() / 2),
61 viewport.adjusted(0, 0, 0, -viewport.height() / 2));
62
63 //! [4]
64 }
65
66 {
67 //! [7]
68 QGraphicsScene scene;
69 scene.addText("GraphicsView rotated clockwise");
70
71 QGraphicsView view(&scene);
72 view.rotate(90); // the text is rendered with a 90 degree clockwise rotation
73 view.show();
74 //! [7]
75 }
76}
77
79{
80public:
81 void mousePressEvent(QMouseEvent *event) override;
82};
83
84//! [5_6_declaration]
85void CustomView::mousePressEvent(QMouseEvent *event)
86{
87//! [5_6_declaration]
88
89 {
90 //! [5]
91 qDebug() << "There are" << items(event->pos()).size()
92 << "items at position" << mapToScene(event->pos());
93 //! [5]
94 }
95
96 {
97 //! [6]
98 if (QGraphicsItem *item = itemAt(event->pos())) {
99 qDebug() << "You clicked on item" << item;
100 } else {
101 qDebug("You didn't click on an item.");
102 }
103 //! [6]
104 }
105
106//! [5_6_end]
107}
108//! [5_6_end]
void mousePressEvent(QMouseEvent *event) override
[5_6_declaration]
bool examples()
[3]