我遇到了QGraphicsView
由调用引起的不需要的视口 () 更新的问题QStackedWidget::setCurrentIndex()
,该QStackedWidget
实例不是场景的一部分。
这并非在所有场景中都会发生,似乎只有较大的场景才能不受此问题的影响。
我在我的代码中找不到与此相关的任何内容,而且我真的不知道您需要什么来回答我的问题,因此对于缺乏信息感到非常抱歉,请告诉我您需要什么以及我应该首先检查什么.
ui->inspectorWidget
是一个 6 页的QStackedWidget
实例。
ui->map
并且ui->map3D
是QGraphicsView
实例。
scene = ui->map->scene
是一个QGraphicsScene
。
void Class::on_toggle3D_released()
{
//3D to 2D
if(ui->map3D->isVisible())
{
myWindow::on = false;//Prevents OpenGL rendering
for(...)
{
//Coordinates calculations
//Adding a rectangle to the scene
QGraphicsRectItem *item = new QGraphicsRectItem(x,y,w,h);
item->setBrush(QBrush(r,g,b,255)));
scene.addItem(item);
}
//QRectF visibleRec = ui->map->mapToScene(ui->map->viewport()->rect()).boundingRect();
ui->inspectorWidget->setCurrentIndex(0);//Where the viewport is changed
//ui->map->setSceneRect(visibleRec); <- a first attempt to solve this
ui->map->setVisible(true);
ui->map3D->setVisible(false);
ui->toggle3D->setText("3D");
ui->map3D->viewport()->releaseKeyboard();
}
//2D to 3D
else
{
//2D rectangles cleaning
foreach(QGraphicsItem * item,scene.items())
{
if(item->type() == QGraphicsRectItem::Type)
{
scene.removeItem(item);
}
}
scene.items().clear();
//Creating texture from 2D view, that's why I need the view not to change after this step
QImage img(ui->map->width(), ui->map->height(),QImage::Format_ARGB32_Premultiplied);
QPainter p(&img);
ui->map->viewport()->render(&p);
p.end();
img.save("tmp.png");
//Updates scene and view information in QGLWidget class
((myWindow *)ui->map3D->viewport())->setSceneWidth(scene.width());
((myWindow *)ui->map3D->viewport())->setSceneHeight(scene.height());
((myWindow *)(ui->map3D->viewport()))->setViewport(ui->map->mapToScene(ui->map->viewport()->rect()).boundingRect());
myWindow::reload = true;//Texture reloading
myWindow::on = true;//Allows OpenGL rendering
//QRectF visibleRect = ui->map->mapToScene(ui->map->viewport()->rect()).boundingRect();
ui->inspectorWidget->setCurrentIndex(5);//Where the viewport is changed
//ui->map->setSceneRect(visibleRect); <- the same attempt
ui->map3D->viewport()->grabKeyboard();
ui->map3D->setVisible(true);
ui->map->setVisible(false);
ui->toggle3D->setText("2D");
}
}