简体   繁体   中英

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?

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM