This commit is contained in:
DESKTOP-450PEFP\mainc 2026-06-12 11:57:17 +08:00
parent 06ed288246
commit a20328b496
2 changed files with 60 additions and 0 deletions

View File

@ -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);
// }
// }
}

24
logPlus/MyGraphicsView.h Normal file
View File

@ -0,0 +1,24 @@
// MyGraphicsView.h
#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H
#include <QGraphicsView>
#include <QResizeEvent>
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