File indexing completed on 2026-06-24 08:33:08
0001
0002
0003
0004
0005 #ifndef QGRAPHICSVIEW_H
0006 #define QGRAPHICSVIEW_H
0007
0008 #include <QtWidgets/qtwidgetsglobal.h>
0009 #include <QtCore/qmetatype.h>
0010 #include <QtGui/qpainter.h>
0011 #include <QtWidgets/qscrollarea.h>
0012 #include <QtWidgets/qgraphicsscene.h>
0013
0014 QT_REQUIRE_CONFIG(graphicsview);
0015
0016 QT_BEGIN_NAMESPACE
0017
0018 class QGraphicsItem;
0019 class QPainterPath;
0020 class QPolygonF;
0021 class QStyleOptionGraphicsItem;
0022
0023 class QGraphicsViewPrivate;
0024 class Q_WIDGETS_EXPORT QGraphicsView : public QAbstractScrollArea
0025 {
0026 Q_OBJECT
0027 Q_FLAGS(QPainter::RenderHints CacheMode OptimizationFlags)
0028 Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush)
0029 Q_PROPERTY(QBrush foregroundBrush READ foregroundBrush WRITE setForegroundBrush)
0030 Q_PROPERTY(bool interactive READ isInteractive WRITE setInteractive)
0031 Q_PROPERTY(QRectF sceneRect READ sceneRect WRITE setSceneRect)
0032 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
0033 Q_PROPERTY(QPainter::RenderHints renderHints READ renderHints WRITE setRenderHints)
0034 Q_PROPERTY(DragMode dragMode READ dragMode WRITE setDragMode)
0035 Q_PROPERTY(CacheMode cacheMode READ cacheMode WRITE setCacheMode)
0036 Q_PROPERTY(ViewportAnchor transformationAnchor READ transformationAnchor
0037 WRITE setTransformationAnchor)
0038 Q_PROPERTY(ViewportAnchor resizeAnchor READ resizeAnchor WRITE setResizeAnchor)
0039 Q_PROPERTY(ViewportUpdateMode viewportUpdateMode READ viewportUpdateMode
0040 WRITE setViewportUpdateMode)
0041 #if QT_CONFIG(rubberband)
0042 Q_PROPERTY(Qt::ItemSelectionMode rubberBandSelectionMode READ rubberBandSelectionMode
0043 WRITE setRubberBandSelectionMode)
0044 #endif
0045 Q_PROPERTY(OptimizationFlags optimizationFlags READ optimizationFlags
0046 WRITE setOptimizationFlags)
0047
0048 public:
0049 enum ViewportAnchor {
0050 NoAnchor,
0051 AnchorViewCenter,
0052 AnchorUnderMouse
0053 };
0054 Q_ENUM(ViewportAnchor)
0055
0056 enum CacheModeFlag {
0057 CacheNone = 0x0,
0058 CacheBackground = 0x1
0059 };
0060 Q_DECLARE_FLAGS(CacheMode, CacheModeFlag)
0061
0062 enum DragMode {
0063 NoDrag,
0064 ScrollHandDrag,
0065 RubberBandDrag
0066 };
0067 Q_ENUM(DragMode)
0068
0069 enum ViewportUpdateMode {
0070 FullViewportUpdate,
0071 MinimalViewportUpdate,
0072 SmartViewportUpdate,
0073 NoViewportUpdate,
0074 BoundingRectViewportUpdate
0075 };
0076 Q_ENUM(ViewportUpdateMode)
0077
0078 enum OptimizationFlag {
0079 DontSavePainterState = 0x1,
0080 DontAdjustForAntialiasing = 0x2,
0081 IndirectPainting = 0x4
0082 };
0083 Q_DECLARE_FLAGS(OptimizationFlags, OptimizationFlag)
0084
0085 QGraphicsView(QWidget *parent = nullptr);
0086 QGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr);
0087 ~QGraphicsView();
0088
0089 QSize sizeHint() const override;
0090
0091 QPainter::RenderHints renderHints() const;
0092 void setRenderHint(QPainter::RenderHint hint, bool enabled = true);
0093 void setRenderHints(QPainter::RenderHints hints);
0094
0095 Qt::Alignment alignment() const;
0096 void setAlignment(Qt::Alignment alignment);
0097
0098 ViewportAnchor transformationAnchor() const;
0099 void setTransformationAnchor(ViewportAnchor anchor);
0100
0101 ViewportAnchor resizeAnchor() const;
0102 void setResizeAnchor(ViewportAnchor anchor);
0103
0104 ViewportUpdateMode viewportUpdateMode() const;
0105 void setViewportUpdateMode(ViewportUpdateMode mode);
0106
0107 OptimizationFlags optimizationFlags() const;
0108 void setOptimizationFlag(OptimizationFlag flag, bool enabled = true);
0109 void setOptimizationFlags(OptimizationFlags flags);
0110
0111 DragMode dragMode() const;
0112 void setDragMode(DragMode mode);
0113
0114 #if QT_CONFIG(rubberband)
0115 Qt::ItemSelectionMode rubberBandSelectionMode() const;
0116 void setRubberBandSelectionMode(Qt::ItemSelectionMode mode);
0117 QRect rubberBandRect() const;
0118 #endif
0119
0120 CacheMode cacheMode() const;
0121 void setCacheMode(CacheMode mode);
0122 void resetCachedContent();
0123
0124 bool isInteractive() const;
0125 void setInteractive(bool allowed);
0126
0127 QGraphicsScene *scene() const;
0128 void setScene(QGraphicsScene *scene);
0129
0130 QRectF sceneRect() const;
0131 void setSceneRect(const QRectF &rect);
0132 inline void setSceneRect(qreal x, qreal y, qreal w, qreal h);
0133
0134 QTransform transform() const;
0135 QTransform viewportTransform() const;
0136 bool isTransformed() const;
0137 void setTransform(const QTransform &matrix, bool combine = false);
0138 void resetTransform();
0139 void rotate(qreal angle);
0140 void scale(qreal sx, qreal sy);
0141 void shear(qreal sh, qreal sv);
0142 void translate(qreal dx, qreal dy);
0143
0144 void centerOn(const QPointF &pos);
0145 inline void centerOn(qreal x, qreal y);
0146 void centerOn(const QGraphicsItem *item);
0147 void ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50);
0148 inline void ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50);
0149 void ensureVisible(const QGraphicsItem *item, int xmargin = 50, int ymargin = 50);
0150 void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
0151 inline void fitInView(qreal x, qreal y, qreal w, qreal h,
0152 Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
0153 void fitInView(const QGraphicsItem *item,
0154 Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
0155
0156 void render(QPainter *painter, const QRectF &target = QRectF(), const QRect &source = QRect(),
0157 Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
0158
0159 QList<QGraphicsItem *> items() const;
0160 QList<QGraphicsItem *> items(const QPoint &pos) const;
0161 inline QList<QGraphicsItem *> items(int x, int y) const;
0162 QList<QGraphicsItem *> items(const QRect &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
0163 inline QList<QGraphicsItem *> items(int x, int y, int w, int h, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
0164 QList<QGraphicsItem *> items(const QPolygon &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
0165 QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
0166 QGraphicsItem *itemAt(const QPoint &pos) const;
0167 inline QGraphicsItem *itemAt(int x, int y) const;
0168
0169 QPointF mapToScene(const QPoint &point) const;
0170 QPolygonF mapToScene(const QRect &rect) const;
0171 QPolygonF mapToScene(const QPolygon &polygon) const;
0172 QPainterPath mapToScene(const QPainterPath &path) const;
0173 QPoint mapFromScene(const QPointF &point) const;
0174 QPolygon mapFromScene(const QRectF &rect) const;
0175 QPolygon mapFromScene(const QPolygonF &polygon) const;
0176 QPainterPath mapFromScene(const QPainterPath &path) const;
0177 inline QPointF mapToScene(int x, int y) const;
0178 inline QPolygonF mapToScene(int x, int y, int w, int h) const;
0179 inline QPoint mapFromScene(qreal x, qreal y) const;
0180 inline QPolygon mapFromScene(qreal x, qreal y, qreal w, qreal h) const;
0181
0182 QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
0183
0184 QBrush backgroundBrush() const;
0185 void setBackgroundBrush(const QBrush &brush);
0186
0187 QBrush foregroundBrush() const;
0188 void setForegroundBrush(const QBrush &brush);
0189
0190 public Q_SLOTS:
0191 void updateScene(const QList<QRectF> &rects);
0192 void invalidateScene(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers);
0193 void updateSceneRect(const QRectF &rect);
0194
0195 #if QT_CONFIG(rubberband)
0196 Q_SIGNALS:
0197 void rubberBandChanged(QRect viewportRect, QPointF fromScenePoint, QPointF toScenePoint);
0198 #endif
0199
0200 protected Q_SLOTS:
0201 void setupViewport(QWidget *widget) override;
0202
0203 protected:
0204 QGraphicsView(QGraphicsViewPrivate &, QWidget *parent = nullptr);
0205 bool event(QEvent *event) override;
0206 bool viewportEvent(QEvent *event) override;
0207
0208 #ifndef QT_NO_CONTEXTMENU
0209 void contextMenuEvent(QContextMenuEvent *event) override;
0210 #endif
0211 #if QT_CONFIG(draganddrop)
0212 void dragEnterEvent(QDragEnterEvent *event) override;
0213 void dragLeaveEvent(QDragLeaveEvent *event) override;
0214 void dragMoveEvent(QDragMoveEvent *event) override;
0215 void dropEvent(QDropEvent *event) override;
0216 #endif
0217 void focusInEvent(QFocusEvent *event) override;
0218 bool focusNextPrevChild(bool next) override;
0219 void focusOutEvent(QFocusEvent *event) override;
0220 void keyPressEvent(QKeyEvent *event) override;
0221 void keyReleaseEvent(QKeyEvent *event) override;
0222 void mouseDoubleClickEvent(QMouseEvent *event) override;
0223 void mousePressEvent(QMouseEvent *event) override;
0224 void mouseMoveEvent(QMouseEvent *event) override;
0225 void mouseReleaseEvent(QMouseEvent *event) override;
0226 #if QT_CONFIG(wheelevent)
0227 void wheelEvent(QWheelEvent *event) override;
0228 #endif
0229 void paintEvent(QPaintEvent *event) override;
0230 void resizeEvent(QResizeEvent *event) override;
0231 void scrollContentsBy(int dx, int dy) override;
0232 void showEvent(QShowEvent *event) override;
0233 void inputMethodEvent(QInputMethodEvent *event) override;
0234
0235 virtual void drawBackground(QPainter *painter, const QRectF &rect);
0236 virtual void drawForeground(QPainter *painter, const QRectF &rect);
0237 virtual void drawItems(QPainter *painter, int numItems,
0238 QGraphicsItem *items[],
0239 const QStyleOptionGraphicsItem options[]);
0240
0241 private:
0242 Q_DECLARE_PRIVATE(QGraphicsView)
0243 Q_DISABLE_COPY(QGraphicsView)
0244 #ifndef QT_NO_CURSOR
0245 Q_PRIVATE_SLOT(d_func(), void _q_setViewportCursor(const QCursor &))
0246 Q_PRIVATE_SLOT(d_func(), void _q_unsetViewportCursor())
0247 #endif
0248 friend class QGraphicsSceneWidget;
0249 friend class QGraphicsScene;
0250 friend class QGraphicsScenePrivate;
0251 friend class QGraphicsItemPrivate;
0252 };
0253
0254 Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::CacheMode)
0255 Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::OptimizationFlags)
0256
0257 inline void QGraphicsView::setSceneRect(qreal ax, qreal ay, qreal aw, qreal ah)
0258 { setSceneRect(QRectF(ax, ay, aw, ah)); }
0259 inline void QGraphicsView::centerOn(qreal ax, qreal ay)
0260 { centerOn(QPointF(ax, ay)); }
0261 inline void QGraphicsView::ensureVisible(qreal ax, qreal ay, qreal aw, qreal ah, int xmargin, int ymargin)
0262 { ensureVisible(QRectF(ax, ay, aw, ah), xmargin, ymargin); }
0263 inline void QGraphicsView::fitInView(qreal ax, qreal ay, qreal w, qreal h, Qt::AspectRatioMode mode)
0264 { fitInView(QRectF(ax, ay, w, h), mode); }
0265 inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay) const
0266 { return items(QPoint(ax, ay)); }
0267 inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay, int w, int h, Qt::ItemSelectionMode mode) const
0268 { return items(QRect(ax, ay, w, h), mode); }
0269 inline QGraphicsItem *QGraphicsView::itemAt(int ax, int ay) const
0270 { return itemAt(QPoint(ax, ay)); }
0271 inline QPointF QGraphicsView::mapToScene(int ax, int ay) const
0272 { return mapToScene(QPoint(ax, ay)); }
0273 inline QPolygonF QGraphicsView::mapToScene(int ax, int ay, int w, int h) const
0274 { return mapToScene(QRect(ax, ay, w, h)); }
0275 inline QPoint QGraphicsView::mapFromScene(qreal ax, qreal ay) const
0276 { return mapFromScene(QPointF(ax, ay)); }
0277 inline QPolygon QGraphicsView::mapFromScene(qreal ax, qreal ay, qreal w, qreal h) const
0278 { return mapFromScene(QRectF(ax, ay, w, h)); }
0279
0280 QT_END_NAMESPACE
0281
0282 #endif