QgraphicsView不适合QT中的窗口

发布于 2025-01-18 13:21:41 字数 325 浏览 2 评论 0原文

我有QgraphicsView,它具有多个Qgraphictem的。当我的视图首次加载并在屏幕上可见时,它不能完全适合屏幕。大约80%的视图可见。然后,我需要使用滚动来查看其余20%的视图。

在加载时,我的整个视图将如何可见?

我尝试以下方式:
(将所有项目添加到视图之后),

QRectF a = scene->sceneRect();      
 view->ensureVisible(a,200,200);        
    

但我的视图仍然可见80%。

I am having QGraphicsView, which has multiple QGraphicsItem's. When my view loads first time and gets visible on screen, it can not be fit fully in the screen. Around 80% of view gets visible. Then I need to use scrolling to see the remaining 20% of the view.

How my whole view will get visible at the time of loading ?

I tried following way :
(After adding all the items into view )

QRectF a = scene->sceneRect();      
 view->ensureVisible(a,200,200);        
    

But still my view is 80% visible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生来就爱笑 2025-01-25 13:21:41

首先,关闭滚动条,可以通过以下方式实现以下操作:

    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

qgraphicsView实例。

如果您将所有添加的项目都添加到场景中,请使用scenerect将所有添加的项目的bouding rect缩小到场景中,并且视图的几何形状 - 视图的可绘制窗口小部件由viewport()方法访问。 。

当您具有场景几何形状和视图几何形状时,您可以计算可以通过qgraphicsview :: scale()方法应用于视图的比例因子。

计算比例因子的算法(一个比例因子对于垂直轴和水平轴,因为我们不想在某些轴上伸展视图)如下:

if (sceneWidth > viewWidth)
    s = viewWidth / sceneWidth
    // check if scene scaled by scale fit in vertical
    if (s * sceneHeight > viewHeight)
        s = viewHeight / sceneHeight
// else if condition for checking sceneHeight > viewHeight
view->scale(s,s)

最小代码

class TRect : public QGraphicsItem
{
public:
    TRect(QGraphicsItem* parent = nullptr) : QGraphicsItem(parent) {}
    QRectF boundingRect() const override { return QRectF(0,0,20,20); }
    void paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*) override {
        painter->fillRect(QRectF(0,0,20,20), QColor(255,255,0));
    }
};

class TView : public QGraphicsView {
public:
    TView(QWidget* parent) : QGraphicsView(parent) {}
    void resizeEvent(QResizeEvent *e) noexcept override {
        QGraphicsView::resizeEvent(e);
        QRectF rc = scene()->sceneRect();
        float s = 1.0f;
        if (rc.width() > viewport()->width()) {
            s = (float)viewport()->width() / rc.width();
            float newHeight = s * rc.height();
            if (newHeight > viewport()->height())
                s = (float)viewport()->height() / rc.height();
        }
        else if (rc.height() > viewport()->height()) {
            s = (float)viewport()->height() / rc.height();
            float newWidth = s * rc.width();
            if (newWidth > viewport()->width())
                s = (float)viewport()->width() / rc.width();
        }
        this->scale(s,s);
    }
};

class TMainWindow : public QWidget  {
    TView* _view = nullptr;
    QGraphicsScene* _scene = nullptr;
public:
    TMainWindow() {
        QVBoxLayout* layout = new QVBoxLayout(this);
        _view = new TView(this);
        _view->setFixedSize(300,400);
        _scene = new QGraphicsScene(this);
        _view->setScene(_scene);
        _view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        _view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        for (int i = 0; i < 10; ++i)
        {
            TRect* rc = new TRect();
            rc->setPos(i*100,i*100);
            _scene->addItem(rc);
        }        
        layout->addWidget(_view);
    }
};

: TVIEW :: RESIZEEVENT您可以放入功能中并在需要时调用它:

  • 添加/删除的项目
  • 更改了视图大小

First of all, turn off the scroll bars, it can be achived by:

    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

called on QGraphicsView instance.

If you have all added items to the scene, use sceneRect to get bouding rect of all added items to the scene, and the geometry of the view - drawable widget of the view is accessed by viewport() method.

When you have the scene geometry and the view geometry you can compute the scale factor which can be applied to the view by QGraphicsView::scale() method.

The algo to compute the scale factor (one scale factor for both vertical and horizontal axis because we don't want to stretch content of view in some axis) is as follows:

if (sceneWidth > viewWidth)
    s = viewWidth / sceneWidth
    // check if scene scaled by scale fit in vertical
    if (s * sceneHeight > viewHeight)
        s = viewHeight / sceneHeight
// else if condition for checking sceneHeight > viewHeight
view->scale(s,s)

Minimal code:

class TRect : public QGraphicsItem
{
public:
    TRect(QGraphicsItem* parent = nullptr) : QGraphicsItem(parent) {}
    QRectF boundingRect() const override { return QRectF(0,0,20,20); }
    void paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*) override {
        painter->fillRect(QRectF(0,0,20,20), QColor(255,255,0));
    }
};

class TView : public QGraphicsView {
public:
    TView(QWidget* parent) : QGraphicsView(parent) {}
    void resizeEvent(QResizeEvent *e) noexcept override {
        QGraphicsView::resizeEvent(e);
        QRectF rc = scene()->sceneRect();
        float s = 1.0f;
        if (rc.width() > viewport()->width()) {
            s = (float)viewport()->width() / rc.width();
            float newHeight = s * rc.height();
            if (newHeight > viewport()->height())
                s = (float)viewport()->height() / rc.height();
        }
        else if (rc.height() > viewport()->height()) {
            s = (float)viewport()->height() / rc.height();
            float newWidth = s * rc.width();
            if (newWidth > viewport()->width())
                s = (float)viewport()->width() / rc.width();
        }
        this->scale(s,s);
    }
};

class TMainWindow : public QWidget  {
    TView* _view = nullptr;
    QGraphicsScene* _scene = nullptr;
public:
    TMainWindow() {
        QVBoxLayout* layout = new QVBoxLayout(this);
        _view = new TView(this);
        _view->setFixedSize(300,400);
        _scene = new QGraphicsScene(this);
        _view->setScene(_scene);
        _view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        _view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        for (int i = 0; i < 10; ++i)
        {
            TRect* rc = new TRect();
            rc->setPos(i*100,i*100);
            _scene->addItem(rc);
        }        
        layout->addWidget(_view);
    }
};

The code contained in TView::resizeEvent you can put in the function and call it when it is required:

  • added/removed items to the scene
  • changed the view size
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文