[英]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
. 我已经在Qt上创建了图像编辑器,但是在
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. 当我打开图像时,我调用
fitInView
使其非常适合graphicsView
,这就是问题所在:当我最大化窗口时,由于设置了水平布局,因此图形视图的大小会发生变化,但是图像不会更改大小。
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? 当窗口最大化时如何调用
fitInView
?
Quite easy, just override the resizeEvent. 非常简单,只需覆盖resizeEvent。
This working code relies on QGraphicsView and QGraphicsScene, zoom works as well ;) 此工作代码依赖于QGraphicsView和QGraphicsScene,缩放效果也不错;)
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.