Qt中实现fitInView()后无法放大
我正在从事QT项目,而我要做的就是在后台拥有图像(可以是PNG或JPG)。
我创建了一个带有Qgraphicsview和Qgraphicsscene场景的视图。
QGraphicsPixmapItem * image = new QGraphicsPixmapItem(QPixmap("...../world_map.png"));
int imageWidth = image->pixmap().width();
int imageHeight = image->pixmap().height();
image->setOffset(- imageWidth / 2, -imageHeight / 2);
image->setPos(0, 0);
QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(-imageWidth / 2, -imageHeight / 2, imageWidth, imageHeight);
QGraphicsView * gv = new QGraphicsView();
gv->setScene(scene);
gv->scene()->addItem(image);
但我希望整个图像在保持纵横比的同时适合视图。因此,我制作了一个从QgraphicsView继承的自定义类,并写下了以下内容:
void MyView::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
fitInView(sceneRect(), Qt::KeepAspectRatio);
}
现在,我得到以下输出: < img src =“ https://i.sstatic.net/zvvvt.png” alt =“在此处输入图像说明”>
这是可取的,但我现在无法放大视图。我只能放大。 PS-我写了一个鼠标Wheelevent函数以放大和缩小。
如何在设施中实施缩放?
编辑:这就是我实现Zoom In/Out的方式:
void MyView::wheelEvent(QWheelEvent *e)
{
static const double factor = 1.1;
static double currentScale = 1.0;
static const double scaleMin = 1.0;
ViewportAnchor oldAnchor = transformationAnchor();
setTransformationAnchor(QGraphicsView::AnchorUnderMouse); // set focus to mouse coords
//if (e->delta() > 0)
if (e->angleDelta().y() > 0){
scale(factor, factor);
currentScale *= factor;
}
else if (currentScale > scaleMin){
scale(1 / factor, 1 / factor);
currentScale /= factor;
}
setTransformationAnchor(oldAnchor); // reset anchor
}
I'm working on a project in Qt and the thing I've to do is to have an image in the background(can be png or jpg).
I've created a view with a scene with QGraphicsView and QGraphicsScene.
QGraphicsPixmapItem * image = new QGraphicsPixmapItem(QPixmap("...../world_map.png"));
int imageWidth = image->pixmap().width();
int imageHeight = image->pixmap().height();
image->setOffset(- imageWidth / 2, -imageHeight / 2);
image->setPos(0, 0);
QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(-imageWidth / 2, -imageHeight / 2, imageWidth, imageHeight);
QGraphicsView * gv = new QGraphicsView();
gv->setScene(scene);
gv->scene()->addItem(image);
But I wanted the whole image to fit the view while maintaining the aspect ratio. So, I made a custom class inherited from QGraphicsView and wrote the following:
void MyView::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
fitInView(sceneRect(), Qt::KeepAspectRatio);
}
Now, I got the following output:
This is desirable but I can't zoom in on the view now. I can only zoom out.
P.S. - I wrote a mouseWheelEvent function to zoom in and out.
What can be done to implement zooming in facility??
Edit : This is how I implemented zoom in/out:
void MyView::wheelEvent(QWheelEvent *e)
{
static const double factor = 1.1;
static double currentScale = 1.0;
static const double scaleMin = 1.0;
ViewportAnchor oldAnchor = transformationAnchor();
setTransformationAnchor(QGraphicsView::AnchorUnderMouse); // set focus to mouse coords
//if (e->delta() > 0)
if (e->angleDelta().y() > 0){
scale(factor, factor);
currentScale *= factor;
}
else if (currentScale > scaleMin){
scale(1 / factor, 1 / factor);
currentScale /= factor;
}
setTransformationAnchor(oldAnchor); // reset anchor
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题。 fitinview中的某些内容使变换中的缩放比例的上限。如果在缩放时打印qgraphicsview :: transform(),则会发现M11和M22的值不能大于1(调用FitinView之后)。
解决方案1:在FitinView之后致电RESETTRANSFOM()。不推荐,因为它重置打电话。
解决方案2:不要使用FitinView,而是写自己的FitinView版本。
请注意,在我的实施中,我知道我在场景中只有一个项目。我还更改了场景的矩形大小,这对于其他人来说可能不是必需的。 ImageViwer是QgraphicsView的子类。
I encountered the same problem. Something in fitInView makes the upper limit of scaling in transform to 1. If you print QGraphicsView::transform() while scaling, you will find values of m11 and m22 cannot be greater than 1(after calling fitInView).
Solution 1: call resetTransfrom() after fitInView. Not recommended because it resets calling.
Solution 2: Don't use fitInView, I write my own version of fitInView instead.
Note, in my implementation, I know I only have one item in the scene. I also change scene rect size, which may not be necessary for others. ImageViwer is child class of qgraphicsview.