[Interest] QGraphicsView stays big

Andre Haupt andre at bitwigglers.org
Thu Jun 7 16:16:56 CEST 2012


On Thu, Jun 07, 2012 at 06:22:56AM -0700, Waitman Gobble wrote:
> yeah I suspect it does create a memory leak, but it does seem to solve my size issue with QGraphicsScene;
> 
> 
> Per recommendation I've just tried this:
> 
> gs->clear();
> gs->setSceneRect(QRectF());
> gs->addPixmap(pm);
> 
> ..and unfortunately this does not appear to resize (shrink) the QGraphicsScene.
> 
> I agree it's probably better to run without a memory leak, so I'll keep trying.
I just tried this. Setting the sceneRect to an empty QRectF() does have
the same effect as not setting it at all (as documented). But setting the sceneRect
to the pixmaps rect certainly works. Does something like the following
work for you?

#include <QtGui/QApplication>
#include <QGraphicsView>
#include <QPixmap>
#include <QLayout>
#include <QPushButton>

class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget() {
        largePixmap = new QPixmap(3200, 2400);
        largePixmap->fill(Qt::blue);
        smallPixmap = new QPixmap(320, 240);
        smallPixmap->fill(Qt::red);

        scene = new QGraphicsScene(this);
        view = new QGraphicsView(scene, this);

        smallButton = new QPushButton("small pixmap", this);
        connect(smallButton, SIGNAL(clicked()), this, SLOT(setSmall()));

        largeButton = new QPushButton("large pixmap", this);
        connect(largeButton, SIGNAL(clicked()), this, SLOT(setLarge()));

        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(smallButton);
        layout->addWidget(largeButton);
        layout->addWidget(view);
    }

private slots:
    void setSmall() {
        scene->clear();
        scene->setSceneRect(smallPixmap->rect());
        scene->addPixmap(*smallPixmap);
    }

    void setLarge() {
        scene->clear();
        scene->setSceneRect(largePixmap->rect());
        scene->addPixmap(*largePixmap);
    }

private:
    QPixmap *largePixmap, *smallPixmap;
    QGraphicsView *view;
    QGraphicsScene *scene;
    QPushButton *smallButton, *largeButton;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Widget w;
    w.resize(800, 600);
    w.show();

    return a.exec();
}

#include "main.moc"





More information about the Interest mailing list