I'm having a problem with an unwanted viewport (QGraphicsView
) update caused by calling QStackedWidget::setCurrentIndex()
, the QStackedWidget
instance is not part of the scene.
This does not happen on all scenes, it seems that only the bigger ones are immune to this issue.
I can't find anything related to this in my code and I don't really know what you need to answer my problem so I'm very sorry for the lack of information and please tell me what you need and what I should check first.
ui->inspectorWidget
is a 6 pages QStackedWidget
instance.
ui->map
and ui->map3D
are QGraphicsView
instances.
scene = ui->map->scene
is a 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");
}
}
Okay I found what was causing the issue by disabling any ui->map->fitInView() or ui->map->centerOn() and re-enabling these lines one by one to isolate the one updating the viewport.
Here is the function that was causing the issue :
void Class::ensureVisibleCheckedChanged(QTreeWidgetItem *item, int)
{
if(item) {
foreach(NetElement *element, elements) {
if(element->friendlyName == item->text(0)) {
ui->map->centerOn(element);//Where the viewport was changed
if(item->checkState(0) == Qt::Checked)
element->setVisible(true);
else
element->setVisible(false);
}
}
}
}
I'm not the one who wrote this function, it is never called anywhere and I can't find anything on it in Qt documentation but at least I can work on this now.
EDIT : I found what caused it to be called : it was connected to a signal in Qt GUI designer, sneaky slot :)