QGraphicsView 继承了 QWidget::geometry() 函数。您可以使用它来确定其在其父窗口小部件中的位置和大小。 (在其构造函数之外)
QGraphicsScene 可以大于 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,但你明白了。我建议尝试将您的地图分割成可见区域大小的正方形。