I am trying to create an application to view photos. For this, I have subclassed QWidget (named it ImageWidget). I am loading this widget inside a subclass of QMainWindow.
To Display the image, I am using QGraphicsView and QGraphicsScene. When I maximise the window, the size of the image loaded into QGraphicsView does not change. I want to image to increase and decresase according to the viewport.
I tried using the QWidget::resizeEevent provided, and using
this->imageView->fitInView(imageScene->sceneRect(), Qt::KeepAspectRatio);
where 'this' refers to the ImageWidget object (subclass of QWidget).
The code I am trying is at Github: https://github.com/saurabhsood91/qt-photoviewer/blob/master/imagewidget.cpp
What might I be missing here?
onresize
event, do you meanQWidget::resizeEvent
? – thugaQWidget::resizeEvent
and using your code worked fine for me. I noticed you don't remove the oldQGraphicsPixmapItem
s when you add a new one, you should probably do that (QGraphicsScene::clear
) – thugacode
this->imageScene->clear();code
this->imageScene->addPixmap(QPixmap(this->items.at(this->currentIndex)));code
this->imageView->fitInView(this->imageScene->sceneRect(), Qt::KeepAspectRatio); imageView->setScene(this->imageScene); – saurabhsood91