gpt4 book ai didi

c++ - QGraphicsView使用鼠标滚轮在鼠标位置下放大和缩小

转载 作者:IT老高 更新时间:2023-10-28 13:01:53 33 4
gpt4 key购买 nike

我有一个应用程序,在屏幕中间有一个 QGraphicsView 窗口。我希望能够使用鼠标滚轮滚动放大和缩小。

目前我已经重新实现了 QGraphicsView 并覆盖了鼠标滚动功能,使其不会滚动图像(就像默认情况下一样)。

void MyQGraphicsView::wheelEvent(QWheelEvent *event)
{
if(event->delta() > 0)
{
emit mouseWheelZoom(true);
}
else
{
emit mouseWheelZoom(false);
}
}

所以当我滚动时,如果鼠标滚轮向前,我会发出一个信号 true 如果鼠标滚轮向后,我会发出信号。

然后,我将此信号连接到处理我的 GUI 内容的类中的插槽(缩放功能见下文)。现在基本上我认为我的缩放功能根本不是最好的方法我已经看到一些人使用覆盖的 wheelevent 函数来设置比例的例子,但我真的找不到完整的答案。

因此,我已经这样做了,但无论如何它都不完美,所以我正在寻找对其进行一些调整或在车轮事件函数中使用比例的工作示例。

我在构造函数中将m_zoom_level初始化为0

void Display::zoomfunction(bool zoom)
{
QMatrix matrix;

if(zoom && m_zoom_level < 500)
{
m_zoom_level = m_zoom_level + 10;
ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
matrix.scale(m_zoom_level, m_zoom_level);

ui->graphicsView->setMatrix(matrix);
ui->graphicsView->scale(1,-1);
}
else if(!zoom)
{
m_zoom_level = m_zoom_level - 10;
ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
matrix.scale(m_zoom_level, m_zoom_level);

ui->graphicsView->setMatrix(matrix);
ui->graphicsView->scale(1,-1);
}
}

正如您在上面看到的,我正在使用 QMatrix 并对其进行缩放并将其设置为 Graphicsview 并将转换 anchor 设置为鼠标下,但如果我在滚动加载它只会开始放大(我认为这与 int 循环有关)。

正如我所说,对此提供帮助或鼠标下缩放的一个很好的例子会很棒。

最佳答案

这种缩放有点棘手。让我分享我自己的类(class)。

标题:

#include <QObject>
#include <QGraphicsView>

/*!
* This class adds ability to zoom QGraphicsView using mouse wheel. The point under cursor
* remains motionless while it's possible.
*
* Note that it becomes not possible when the scene's
* size is not large enough comparing to the viewport size. QGraphicsView centers the picture
* when it's smaller than the view. And QGraphicsView's scrolls boundaries don't allow to
* put any picture point at any viewport position.
*
* When the user starts scrolling, this class remembers original scene position and
* keeps it until scrolling is completed. It's better than getting original scene position at
* each scrolling step because that approach leads to position errors due to before-mentioned
* positioning restrictions.
*
* When zommed using scroll, this class emits zoomed() signal.
*
* Usage:
*
* new Graphics_view_zoom(view);
*
* The object will be deleted automatically when the view is deleted.
*
* You can set keyboard modifiers used for zooming using set_modified(). Zooming will be
* performed only on exact match of modifiers combination. The default modifier is Ctrl.
*
* You can change zoom velocity by calling set_zoom_factor_base().
* Zoom coefficient is calculated as zoom_factor_base^angle_delta
* (see QWheelEvent::angleDelta).
* The default zoom factor base is 1.0015.
*/
class Graphics_view_zoom : public QObject {
Q_OBJECT
public:
Graphics_view_zoom(QGraphicsView* view);
void gentle_zoom(double factor);
void set_modifiers(Qt::KeyboardModifiers modifiers);
void set_zoom_factor_base(double value);

private:
QGraphicsView* _view;
Qt::KeyboardModifiers _modifiers;
double _zoom_factor_base;
QPointF target_scene_pos, target_viewport_pos;
bool eventFilter(QObject* object, QEvent* event);

signals:
void zoomed();
};

来源:

#include "Graphics_view_zoom.h"
#include <QMouseEvent>
#include <QApplication>
#include <QScrollBar>
#include <qmath.h>

Graphics_view_zoom::Graphics_view_zoom(QGraphicsView* view)
: QObject(view), _view(view)
{
_view->viewport()->installEventFilter(this);
_view->setMouseTracking(true);
_modifiers = Qt::ControlModifier;
_zoom_factor_base = 1.0015;
}

void Graphics_view_zoom::gentle_zoom(double factor) {
_view->scale(factor, factor);
_view->centerOn(target_scene_pos);
QPointF delta_viewport_pos = target_viewport_pos - QPointF(_view->viewport()->width() / 2.0,
_view->viewport()->height() / 2.0);
QPointF viewport_center = _view->mapFromScene(target_scene_pos) - delta_viewport_pos;
_view->centerOn(_view->mapToScene(viewport_center.toPoint()));
emit zoomed();
}

void Graphics_view_zoom::set_modifiers(Qt::KeyboardModifiers modifiers) {
_modifiers = modifiers;

}

void Graphics_view_zoom::set_zoom_factor_base(double value) {
_zoom_factor_base = value;
}

bool Graphics_view_zoom::eventFilter(QObject *object, QEvent *event) {
if (event->type() == QEvent::MouseMove) {
QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
QPointF delta = target_viewport_pos - mouse_event->pos();
if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5) {
target_viewport_pos = mouse_event->pos();
target_scene_pos = _view->mapToScene(mouse_event->pos());
}
} else if (event->type() == QEvent::Wheel) {
QWheelEvent* wheel_event = static_cast<QWheelEvent*>(event);
if (QApplication::keyboardModifiers() == _modifiers) {
if (wheel_event->orientation() == Qt::Vertical) {
double angle = wheel_event->angleDelta().y();
double factor = qPow(_zoom_factor_base, angle);
gentle_zoom(factor);
return true;
}
}
}
Q_UNUSED(object)
return false;
}

使用示例:

Graphics_view_zoom* z = new Graphics_view_zoom(ui->graphicsView);
z->set_modifiers(Qt::NoModifier);

关于c++ - QGraphicsView使用鼠标滚轮在鼠标位置下放大和缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19113532/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com