0

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?

5
  • With onresize event, do you mean QWidget::resizeEvent?
    – thuga
    Jan 28, 2015 at 9:29
  • yes. sorry for the error. I will update the question Jan 28, 2015 at 9:30
  • 1
    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
    Jan 28, 2015 at 9:37
  • 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); Jan 28, 2015 at 14:41
  • Ok. I got it working. Something was wrong with the code flow. Thanks. Could you set this as the answer? I can only upvote it Jan 28, 2015 at 17:57

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.