📄 qgraphicsview.cpp
字号:
view will not allow interaction, and any mouse or key events are ignored (i.e., it will act as a read-only view).*/bool QGraphicsView::isInteractive() const{ Q_D(const QGraphicsView); return d->sceneInteractionAllowed;}void QGraphicsView::setInteractive(bool allowed){ Q_D(QGraphicsView); d->sceneInteractionAllowed = allowed;}/*! Returns a pointer to the scene that is currently visualized in the view. If no scene is currently visualized, 0 is returned. \sa setScene()*/QGraphicsScene *QGraphicsView::scene() const{ Q_D(const QGraphicsView); return d->scene;}/*! Sets the current scene to \a scene. If \a scene is already being viewed, this function does nothing. When a scene is set on a view, the QGraphicsScene::changed() signal is automatically connected to this view's updateScene() slot, and the view's scroll bars are adjusted to fit the size of the scene.*/void QGraphicsView::setScene(QGraphicsScene *scene){ Q_D(QGraphicsView); if (d->scene == scene) return; if (d->scene) { disconnect(d->scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(updateScene(QList<QRectF>))); disconnect(d->scene, SIGNAL(sceneRectChanged(QRectF)), this, SLOT(updateSceneRect(QRectF))); d->scene->d_func()->views.removeAll(this); } if ((d->scene = scene)) { connect(d->scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(updateScene(QList<QRectF>))); connect(d->scene, SIGNAL(sceneRectChanged(QRectF)), this, SLOT(updateSceneRect(QRectF))); d->scene->d_func()->views << this; d->recalculateContentSize(); d->lastCenterPoint = sceneRect().center(); d->keepLastCenterPoint = true; } else { viewport()->update(); d->recalculateContentSize(); }}/*! \property QGraphicsView::sceneRect \brief the area of the scene visualized by this view. The scene rect defines the extent of the scene, and in the view's case, this means the area of the scene that you can navigate using the scroll bars. If unset, or if a null QRectF is set, this property has the same value as QGraphicsScene::sceneRect, and it changes with QGraphicsScene::sceneRect. Otherwise, the view's scene rect is unaffected by the scene. Note that although the scene supports a virtually unlimited size, the range of the scroll bars will never exceed the range of an integer (INT_MIN, INT_MAX). When the scene is larger than the scroll bars' values, you can choose to use translate() to navigate the scene instead. \sa QGraphicsScene::sceneRect*/QRectF QGraphicsView::sceneRect() const{ Q_D(const QGraphicsView); if (d->hasSceneRect) return d->sceneRect; if (d->scene) return d->scene->sceneRect(); return QRectF();}void QGraphicsView::setSceneRect(const QRectF &rect){ Q_D(QGraphicsView); d->hasSceneRect = !rect.isNull(); d->sceneRect = rect; d->recalculateContentSize();}/*! Returns the current transformation matrix for the view. If no current transformation is set, the identity matrix is returned. \sa setMatrix(), rotate(), scale(), shear(), translate()*/QMatrix QGraphicsView::matrix() const{ Q_D(const QGraphicsView); return d->matrix.toAffine();}/*! Sets the view's current transformation matrix to \a matrix. If \a combine is true, then \a matrix is combined with the current matrix; otherwise, \a matrix \e replaces the current matrix. \a combine is false by default. The transformation matrix tranforms the scene into view coordinates. Using the default transformation, provided by the identity matrix, one pixel in the view represents one unit in the scene (e.g., a 10x10 rectangular item is drawn using 10x10 pixels in the view). If a 2x2 scaling matrix is applied, the scene will be drawn in 1:2 (e.g., a 10x10 rectangular item is then drawn using 20x20 pixels in the view). Example: \code QGraphicsScene scene; scene.addText("GraphicsView rotated clockwise"); QGraphicsView view(&scene); view.rotate(90); // the text is rendered with a 90 degree clockwise rotation view.show(); \endcode To simplify interation with items using a transformed view, QGraphicsView provides mapTo... and mapFrom... functions that can translate between scene and view coordinates. For example, you can call mapToScene() to map a view coordinate to a floating point scene coordinate, or mapFromScene() to map from floating point scene coordinates to view coordinates. \sa matrix(), rotate(), scale(), shear(), translate()*/void QGraphicsView::setMatrix(const QMatrix &matrix, bool combine){ setTransform(QTransform(matrix), combine);}/*! Resets the view transformation matrix to the identity matrix.*/void QGraphicsView::resetMatrix(){ resetTransform();}/*! Rotates the current view transformation \a angle degrees clockwise. \sa setMatrix(), matrix(), scale(), shear(), translate()*/void QGraphicsView::rotate(qreal angle){ Q_D(QGraphicsView); QTransform matrix = d->matrix; matrix.rotate(angle); setTransform(matrix);}/*! Scales the current view transformation by (\a sx, \a sy). \sa setMatrix(), matrix(), rotate(), shear(), translate()*/void QGraphicsView::scale(qreal sx, qreal sy){ Q_D(QGraphicsView); QTransform matrix = d->matrix; matrix.scale(sx, sy); setTransform(matrix);}/*! Shears the current view transformation by (\a sh, \a sv). \sa setMatrix(), matrix(), rotate(), scale(), translate()*/void QGraphicsView::shear(qreal sh, qreal sv){ Q_D(QGraphicsView); QTransform matrix = d->matrix; matrix.shear(sh, sv); setTransform(matrix);}/*! Translates the current view transformation by (\a dx, \a dy). \sa setMatrix(), matrix(), rotate(), shear()*/void QGraphicsView::translate(qreal dx, qreal dy){ Q_D(QGraphicsView); QTransform matrix = d->matrix; matrix.translate(dx, dy); setTransform(matrix);}/*! Scrolls the contents of the viewport to ensure that the scene coordinate \a pos, is centered in the view. Because \a pos is a floating point coordinate, and the scroll bars operate on integer coordinates, the centering is only an approximation. \note If the item is close to or outside the border, it will be visible in the view, but not centered. \sa ensureVisible()*/void QGraphicsView::centerOn(const QPointF &pos){ Q_D(QGraphicsView); qreal width = viewport()->width(); qreal height = viewport()->height(); QPointF viewPoint = d->matrix.map(pos); QPointF oldCenterPoint = pos; if (!d->leftIndent) { if (isRightToLeft()) { qint64 horizontal = 0; horizontal += horizontalScrollBar()->minimum(); horizontal += horizontalScrollBar()->maximum(); horizontal -= int(viewPoint.x() - width / 2.0); horizontalScrollBar()->setValue(horizontal); } else { horizontalScrollBar()->setValue(int(viewPoint.x() - width / 2.0)); } } if (!d->topIndent) verticalScrollBar()->setValue(int(viewPoint.y() - height / 2.0)); d->lastCenterPoint = oldCenterPoint;}/*! \fn QGraphicsView::centerOn(qreal x, qreal y) \overload This function is provided for convenience. It's equivalent to calling centerOn(QPointF(\a x, \a y)).*//*! \overload Scrolls the contents of the viewport to ensure that \a item is centered in the view. \sa ensureVisible()*/void QGraphicsView::centerOn(const QGraphicsItem *item){ centerOn(item->sceneBoundingRect().center());}/*! Scrolls the contents of the viewport so that the scene rectangle \a rect is visible, with margins specified in pixels by \a xmargin and \a ymargin. If the specified rect cannot be reached, the contents are scrolled to the nearest valid position. The default value for both margins is 50 pixels. \sa centerOn()*/void QGraphicsView::ensureVisible(const QRectF &rect, int xmargin, int ymargin){ Q_D(QGraphicsView); Q_UNUSED(xmargin); Q_UNUSED(ymargin); qreal width = viewport()->width(); qreal height = viewport()->height(); QRectF viewRect = d->matrix.mapRect(rect); qreal left = d->horizontalScroll(); qreal right = left + width; qreal top = d->verticalScroll(); qreal bottom = top + height; if (viewRect.left() <= left + xmargin) { // need to scroll from the left if (!d->leftIndent) horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5)); } if (viewRect.right() >= right - xmargin) { // need to scroll from the right if (!d->leftIndent) horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5)); } if (viewRect.top() <= top + ymargin) { // need to scroll from the top if (!d->topIndent) verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5)); } if (viewRect.bottom() >= bottom - ymargin) { // need to scroll from the bottom if (!d->topIndent) verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5)); }}/*! \fn QGraphicsView::ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin, int ymargin) \overload This function is provided for convenience. It's equivalent to calling ensureVisible(QRectF(\a x, \a y, \a w, \a h), \a xmargin, \a ymargin).*//*! \overload Scrolls the contents of the viewport so that the center of item \a item is visible, with margins specified in pixels by \a xmargin and \a ymargin. If the specified point cannot be reached, the contents are scrolled to the nearest valid position. The default value for both margins is 50 pixels. \sa centerOn()*/void QGraphicsView::ensureVisible(const QGraphicsItem *item, int xmargin, int ymargin){ ensureVisible(item->sceneBoundingRect(), xmargin, ymargin);}/*! Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle \a rect fits inside the viewport. \a rect must be inside the scene rect; otherwise, fitInView() cannot guarantee that the whole rect is visible. This function keeps the view's rotation, translation, or shear. The view is scaled according to \a aspectRatioMode. \a rect will be centered in the view if it does not fit tightly. \sa setMatrix(), ensureVisible(), centerOn()*/void QGraphicsView::fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRatioMode){ Q_D(QGraphicsView); if (!d->scene || rect.isNull()) return; // Reset the view scale to 1:1. QRectF unity = d->matrix.mapRect(QRectF(0, 0, 1, 1)); scale(1 / unity.width(), 1 / unity.height()); // Find the ideal x / y scaling ratio to fit \a rect in the view. int margin = 2; QRectF viewRect = viewport()->rect().adjusted(margin, margin, -margin, -margin); QRectF sceneRect = d->matrix.mapRect(rect); qreal xratio = viewRect.width() / sceneRect.width(); qreal yratio = viewRect.height() / sceneRect.height(); // Respect the aspect ratio mode. switch (aspectRatioMode) { case Qt::KeepAspectRatio: xratio = yratio = qMin(xratio, yratio); break; case Qt::KeepAspectRatioByExpanding: xratio = yratio = qMax(xratio, yratio); break; case Qt::IgnoreAspectRatio: break; } // Scale and center on the center of \a rect. scale(xratio, yratio); centerOn(rect.center());}/*! \fn void QGraphicsView::fitInView(qreal x, qreal y, qreal w, qreal h, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -