0
votes

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?

1
With onresize event, do you mean QWidget::resizeEvent?thuga
yes. sorry for the error. I will update the questionsaurabhsood91
Reimplementing QWidget::resizeEvent and using your code worked fine for me. I noticed you don't remove the old QGraphicsPixmapItems when you add a new one, you should probably do that (QGraphicsScene::clear)thuga
It's not working for me: I tried the following code on codethis->imageScene->clear(); codethis->imageScene->addPixmap(QPixmap(this->items.at(this->currentIndex))); codethis->imageView->fitInView(this->imageScene->sceneRect(), Qt::KeepAspectRatio); imageView->setScene(this->imageScene);saurabhsood91
Ok. I got it working. Something was wrong with the code flow. Thanks. Could you set this as the answer? I can only upvote itsaurabhsood91

1 Answers

1
votes

Reimplementing QWidget::resizeEvent and using your code worked fine for me. I noticed you don't remove the old QGraphicsPixmapItems when you add a new one, you should probably do that (QGraphicsScene::clear) .

By the way you only need to set the scene to the view once. You do it in the constructor already so you don't need it in the ImageWidget::setImage method.