37 lines
1.2 KiB
C++
37 lines
1.2 KiB
C++
// MyGraphicsView.cpp
|
||
#include "MyGraphicsView.h"
|
||
|
||
MyGraphicsView::MyGraphicsView(QGraphicsScene *scene, QWidget *parent)
|
||
: QGraphicsView(scene, parent)
|
||
{
|
||
// 在构造函数中,可以设置一些视图属性,比如视口更新模式
|
||
// setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
|
||
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||
// 让view的背景透明(可选),以便与窗口背景融合
|
||
this->setStyleSheet("background: transparent;");
|
||
this->setFrameShape(QFrame::NoFrame);
|
||
this->setContentsMargins(0, 0, 0, 0);
|
||
}
|
||
|
||
void MyGraphicsView::setGraphicsProxyWidget(QGraphicsProxyWidget* proxy)
|
||
{
|
||
m_proxy = proxy;
|
||
}
|
||
|
||
void MyGraphicsView::resizeEvent(QResizeEvent *event)
|
||
{
|
||
// 1. 首先调用父类的 resizeEvent,这是必须的[reference:4]!
|
||
QGraphicsView::resizeEvent(event);
|
||
|
||
// 2. 确保场景存在,然后进行适配
|
||
// if (scene()) {
|
||
// // 获取场景中所有 items 的边界矩形
|
||
// QRectF itemsRect = scene()->itemsBoundingRect();
|
||
// if (!itemsRect.isNull()) {
|
||
// // 将视图适配到 items 的边界矩形,并保持宽高比
|
||
// fitInView(itemsRect, Qt::KeepAspectRatio);
|
||
// }
|
||
// }
|
||
}
|