How to `fitInView` when resizing window?

I have created an image editor on Qt but I have an issue displaying the image on the graphicsView.

When I open the image, I call fitInView so that it fits nicely to the graphicsView and here lies the problem: when I maximize the window the graphics view size changes since I have set a horizontal layout, but the image doesn't change size.

Here some images about what I'm talking about:

If I open the file when the window is already maximized, it's all good.

How can I call fitInView when the window get maximized?

Answers


Quite easy, just override the resizeEvent.

This working code relies on QGraphicsView and QGraphicsScene, zoom works as well ;)

const double ZOOM_FACTOR = 1.5;
const double ZOOM_MIN = 1.0;

void ImageViewer::resizeEvent(QResizeEvent* event)
{
    if(ZOOM_MIN < m_current_zoom)
    {
        m_graph_view->setTransform(QTransform::fromScale(m_current_zoom, m_current_zoom));
    }
    else
    {
        m_graph_view->fitInView(0, 0, m_graph_scene->width(), m_graph_scene->height(), Qt::KeepAspectRatio);
    }
}


void ImageViewer::zoomIn()
{
    m_current_zoom = m_current_zoom*ZOOM_FACTOR;
    m_graph_scene->setSceneRect(m_graph_scene->itemsBoundingRect());
    m_graph_view->setTransform(QTransform::fromScale(m_current_zoom, m_current_zoom));
}


void ImageViewer::zoomOut()
{
    m_current_zoom = m_current_zoom/ZOOM_FACTOR;
    m_graph_scene->setSceneRect(m_graph_scene->itemsBoundingRect());

    if(ZOOM_MIN < m_current_zoom)
    {
        m_graph_view->setTransform(QTransform::fromScale(m_current_zoom, m_current_zoom));
    }
    else
    {
        m_graph_view->fitInView(0, 0, m_graph_scene->width(), m_graph_scene->height(), Qt::KeepAspectRatio);
    }
}

Need Your Help

How to Duplicate an AppleScript function in Objective-C?

objective-c cocoa macos applescript

This AppleScript code gives the name of files that have been dropped onto the script. How do I do the same in an Objective-C app? Would an application written in Objective-C be able to boot the JAR...

Intellij-idea: Using Eclipse Keymap on Mac OS X

keyboard-shortcuts intellij-idea

When using IntelliJ with the Eclipse Keymap, all the shortcuts are set to use Ctrl, instead of Command. Just like Eclipse on Windows.