diff --git a/logPlus/MyGraphicsView.cpp b/logPlus/MyGraphicsView.cpp new file mode 100644 index 0000000..2b1865e --- /dev/null +++ b/logPlus/MyGraphicsView.cpp @@ -0,0 +1,36 @@ +// 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); +// } +// } +} diff --git a/logPlus/MyGraphicsView.h b/logPlus/MyGraphicsView.h new file mode 100644 index 0000000..84440f6 --- /dev/null +++ b/logPlus/MyGraphicsView.h @@ -0,0 +1,24 @@ +// MyGraphicsView.h +#ifndef MYGRAPHICSVIEW_H +#define MYGRAPHICSVIEW_H + +#include +#include + +class MyGraphicsView : public QGraphicsView +{ + Q_OBJECT +public: + // 构造函数,可根据需要传入 proxy 等参数 + explicit MyGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr); + + void setGraphicsProxyWidget(QGraphicsProxyWidget* proxy); +protected: + // 重写 resizeEvent 方法 + void resizeEvent(QResizeEvent *event) override; + +private: + QGraphicsProxyWidget *m_proxy; // 用于存储您之前添加的 proxy,以便在 resizeEvent 中访问 +}; + +#endif // MYGRAPHICSVIEW_H \ No newline at end of file