2011-11-13 21 views
1

我正在顯示構建爲QGraphicsPixmapitem項目矩形的地圖(每個項目代表一個地圖圖塊)。因爲我的地圖非常大(大約30 MB的PNG文件),我希望只有當它們在QGraphicsView中對用戶可見時才能按需加載像素圖,並在它們變得不可見時進行卸載。如何獲取QGraphicsView的可見場景矩形?

有什麼辦法可以找出可見的場景矩形?

+2

我想這就是你要找的東西:http://stackoverflow.com/questions/1355446/get-visible-rectangle-of-qgraphicsview。 –

回答

1

QGraphicsView繼承QWidget :: geometry()函數。您可以使用它來確定其父級窗口小部件中的位置和大小。 (在它的構造函數之外)

QGrapicsScene可能比QGraphicsView大。默認的QGraphicsView將添加水平和垂直滾動條來容納QGraphicsScene。我想你會喜歡做這樣的事情:

//create a QGraphicsScene (for this example *scene) that is the size of your entire map. 
QGraphicsScene *scene=new QGraphicsScene(0,0,mapWidth,mapHeight); 
//create a QGraphicsView* named view that is the size of your visible area 
//I'm assuming visibleHeight and visibleWidth do not change (this is your viewing window) 
QGraphicsView *view=new QGraphicsView(0,0,visibleWidth,visibleHeight); 
view->setScene(scene); 

讓用戶控制觸發像sceneMoved(int,int)一些自定義的信號場景的x和y位置。之前重繪現場,調用插槽,檢查現場的新位置:

connect(this,SIGNAL(sceneMoved(int,int)),this,SLOT(drawScene(int,int))); 

void SomeClass::drawScene(int newX, int newY){ 
//if you already have a pointer to the scene do this, or call 
//QGraphicsView::scene(); 
    int oldX=scene->geometry()->x(); 
    int oldY=scene->geometry()->y(); 
    //now that you have your oldX, oldY, newX, and newY, visibleWidth, visibleHeight 
    //you can determine what you need to redraw, what you need to delete, and what can stay 
} 

還有很多的if..else的,但你明白了吧。我建議嘗試將您的地圖分割爲可見區域的大小。

+0

什麼是sceneMoved信號? ID不存在於Qt4中 – rednaks

+0

這是一個自定義信號,由用戶觸發。它確實需要澄清。 – buster

3

這給你一個可視景象矩形:

sceneRect = graphicsView.mapToScene(graphicsView.rect()).boundingRect() 

但如果是剪切或旋轉變換展示它給你看到景區的邊框。如果你沒有這樣的轉換(只是移位或縮放),返回的矩形就是確切的場景區域。

現在你真正的問題是有效地在場景中顯示一個巨大的瓷磚地圖?您可以在後臺加載圖塊,並首先評估您的Qt框架是否已經針對可見範圍之外的大圖像進行了優化。 30 MB也聽起來不那麼大,不適合內存。

相關問題