QGraphicsView 是 Qt 中的一个用于显示图形场景的类。它基于 QGraphicsScene 和 QWidget 类,提供了一些方便的用户交互和视觉效果。
QGraphicsView 的主要功能包括:
- 显示一个 QGraphicsScene 对象,并支持缩放、平移和旋转。
- 提供了鼠标和键盘事件处理函数,以便实现交互操作。
- 支持多种视觉效果,如反锯齿、平滑缩放等。
- 可以将 QGraphicsScene 渲染成图片或 PDF 等格式。
下面是一些常用的 QGraphicsView 方法:
- setScene(QGraphicsScene * scene):设置 QGraphicsView 显示的场景。
- setRenderHint(QPainter::RenderHint hint, bool on = true):设置渲染选项,如反锯齿、平滑缩放等。
- setDragMode(QGraphicsView::DragMode mode):设置拖拽模式,如拖拽场景、选中区域等。
- setViewportUpdateMode(QGraphicsView::ViewportUpdateMode mode):设置视口更新方式,如只有在需要重绘时才更新视口。
- fitInView(const QRectF & rect, Qt::AspectRatioMode aspectRadioMode = Qt::KeepAspectRatio):将指定矩形框适应到视口内。
QGraphicsView 还提供了一些信号,如 sceneChanged()、mousePressEvent()、keyPressEvent() 等,可以与其他对象进行连接,以实现交互操作。
总的来说,QGraphicsView 是一个非常强大的图形显示类,可以用于构建各种类型的图形应用程序。