Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tiling with QGraphicsScene and QGraphicsView

I'm creating a image visualizer that open large images(2gb+) in Qt. I'm doing this by breaking the large image into several tiles of 512X512. I then load a QGraphicsScene of the original image size and use addPixmap to add each tile onto the QGraphic Scene. So ultimately it looks like a huge image to the end user when in fact it is a continuous array of smaller images stuck together on the scene.First of is this a good approach?

Trying to load all the tiles onto the scene takes up a lot of memory. So I'm thinking of only loading the tiles that are visible in the view. I've already managed to subclass QGraphicsScene and override its drag event thus enabling me to know which tiles need to be loaded next based on movement. My problem is tracking movement on the scrollbars. Is there any way I can create an event that get called every time the scrollbar moves. Subclassing QGraphicsView in not an option.

like image 288
sleeping.ninja Avatar asked May 08 '11 13:05

sleeping.ninja


2 Answers

QGraphicsScene is smart enough not to render what isn't visible, so here's what you need to do:

Instead of loading and adding pixmaps, add classes that wrap the pixmap, and only load it when they are first rendered. (Computer scientists like to call this a "proxy pattern"). You could then unload the pixmap based on a timer. (They would be transparently re-loaded if unloaded too soon.) You could even notify this proxy path of the current zoom level, so that it loads lower resolution images when they will be rendered smaller.


Edit: here's some code to get you started. Note that everything that QGraphicsScene draws is a QGraphicsItem, (if you call ::addPixmap, it's converted to a ...GraphicsItem behind the scenes), so that's what you want to subclass:

(I haven't even compiled this, so "caveat lector", but it's doing the right thing ;)

class MyPixmap: public QGraphicsItem{
    public:
        // make sure to set `item` to nullptr in the constructor
        MyPixmap()
            : QGraphicsItem(...), item(nullptr){
        }

        // you will need to add a destructor
        // (and probably a copy constructor and assignment operator)

        QRectF boundingRect() const{
            // return the size
            return QRectF( ... );
        }

        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                   QWidget *widget){
            if(nullptr == item){
                // load item:
                item = new QGraphicsPixmapItem( ... );
            }
            item->paint(painter, option, widget);
        }

     private:
         // you'll probably want to store information about where you're
         // going to load the pixmap from, too

         QGraphicsPixmapItem *item;
};

then you can add your pixmaps to the QGraphicsScene using QGraphicsScene::addItem(...)

like image 135
James Avatar answered Sep 19 '22 14:09

James


Although an answer has already been chosen, I'd like to express my opinion.

I don't like the selected answer, especially because of that usage of timers. A timer to unload the pixmaps? Say that the user actually wants to take a good look at the image, and after a couple of seconds - bam, the image is unloaded, he will have to do something in order the image to reappear. Or may be you will put another timer, that loads the pixmaps after another couple of seconds? Or you will check among your thousand of items if they are visible? Not only is this very very irritating and wrong, but that means that your program will be using resources all the time. Say the user minimizes you program and plays a movie, he will wonder why on earth my movie is freezing every couple of seconds...

Well, if I misunderstood the proposed idea of using timers, execuse me.

Actually the idea that mmutz suggested is better. It reminded me of the Mandelbrot example. Take a look at it. Instead of calculating what to draw you can rewrite this part to loading that part of the image that you need to show.

In conclusion I will propose another solution using QGraphicsView in a much simpler way:

1) check the size of the image without loading the image (use QImageReader)

2) make your scene's size equal to that of the image

3) instead of using pixmap items reimplement the DrawBackground() function. One of the parameters will give you the new exposed rectangle - meaning that if the user scrolls just a little bit, you will load and draw only this new part(to load only part of an image use setClipRect() and then read() methods of the QImageReader class). If there are some transformations you can get them from the other parameter(which is QPainter) and apply them to the image before you draw it.

In my opinion the best solution will be to combine my solution with the threading shown in the Mandelbrot example.

The only problem that I can think of now is if the user zooms out with a big scale factor. Then you will need a lot of resources for some time to load and scale a huge image. Well I see now that there is some function of the QImageReader that I haven't tried yet - setScaledSize(), which maybe do just what we need - if you set a scale size and then load the image maybe it won't load first the entire image – try it. Another way is just to limit the scale factor, a thing that you should do anyway if you stick to the method with the pixmap items.

Hope this helps.

like image 35
zkunov Avatar answered Sep 19 '22 14:09

zkunov