background qt 设置平铺,使用QGraphicsScene和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.

解决方案

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(...)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值