5

I have a QGraphicsScene "scene" and QGraphicsView "graphicsView".

I have a drawing method. When I need redraw all the graphics, I call this method. Everything is OK. But I realized that scene->clear() doesn't change the sceneRect.

Also I tried:

graphicsView->items().clear();
scene->clear();
graphicsView->viewport()->update();

After that, if I get the sceneRect by

QRectF bound = scene->sceneRect();
qDebug() << bound.width();
qDebug() << bound.height();

I expect the bound.width and bound.height to be '0'. But they aren't. I see the previous values everytime. How to clear sceneRect when I clear the scene itself?

It gives some problems that sceneRect remains the same, while using graphicsView->fitInView() method.I use following code:

QRectF bounds = scene->sceneRect();
bounds.setWidth(bounds.width()*1.007);          // to give some margins
bounds.setHeight(bounds.height());              // same as above
graphicsView->fitInView(bounds);

Although I completely cleared the scene and added only one rather small rectangle, the rectangle didn't fit into view because of sceneRect remains too big.

I hope I could explain my problem.

Danh
  • 5,916
  • 7
  • 30
  • 45
mehmetfa
  • 199
  • 3
  • 15

2 Answers2

3

From the Qt Docs (emphasis mine):

This property holds the scene rectangle; the bounding rectangle of the scene

The scene rectangle defines the extent of the scene. It is primarily used by QGraphicsView to determine the view's default scrollable area, and by QGraphicsScene to manage item indexing.

If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).

Therefore, the only way to shrink the sceneRect is to use setSceneRect.

Umbral Reaper
  • 325
  • 4
  • 9
1

The better question is why do you need to set scene rectangle? In case you have a smaller scene don't set it. Instead add items to the scene and fit in view based on items bounding rectangle as in my example below:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsRectItem>
#include <QPointF>
#include <QDebug>
#include <qglobal.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    _scene = new QGraphicsScene(this);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->setScene(_scene);

    connect(ui->button, SIGNAL(released()), this, SLOT(_handleRelease()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

int MainWindow::_random(int min, int max)
{
    return qrand() % ((max + 1) - min) + min;
}

void MainWindow::_handleRelease()
{

    _scene->clear();

    QGraphicsRectItem* pRect1 = _scene->addRect(0, 0, _random(50,100), _random(50,100));
    QGraphicsRectItem* pRect2 = _scene->addRect(0, 0, _random(20,50), _random(20,50));

    pRect1->setPos(QPoint(40,40));
    pRect2->setPos(QPoint(20,20));

    ui->graphicsView->fitInView(_scene->itemsBoundingRect(),Qt::KeepAspectRatio);
}

In case you have a large scene with hundreds of items this approach will be slow because:

If the scene rect is unset, QGraphicsScene will use the bounding area of all items, as returned by itemsBoundingRect(), as the scene rect. However, itemsBoundingRect() is a relatively time consuming function, as it operates by collecting positional information for every item on the scene. Because of this, you should always set the scene rect when operating on large scenes.

Alexander Tyapkov
  • 4,837
  • 5
  • 39
  • 65
  • Using graphicsView->fitInView(scene->itemsBoundingRect()) instead of graphicsView->fitInView(scene->sceneRect()) solved my problem. But sceneRect is still too big. Deleting items doesn't change the sceneRect. I think this issue will produce some problems in the future for me :) – mehmetfa Dec 25 '15 at 08:25
  • @mehmetfa It is not required to set scene rectangle after creation of the scene. If it is not set then it will be always calculated based on bounding rectangle of items inside. Did you try to delete the line where you set scene rectangle? – Alexander Tyapkov Dec 25 '15 at 08:36
  • @mehmetfa if your question is solved please mark the question as solved. – Alexander Tyapkov Dec 25 '15 at 08:38
  • Actually, I didn't set sceneRect. As you said, it is calculated based on items inside. But it is always growing by adding, but not decreasing by deleting items. Only scene->itemsBoundingRect is decreasing by deleting items. – mehmetfa Dec 25 '15 at 08:49
  • @mehmetfa it is strange. In my example scene rectangle stays the same and does not increase in size. – Alexander Tyapkov Dec 25 '15 at 09:11