在同一UI中使用N.2 QGraphicsView时,QWheelEvent不起作用



我正在构建一个具有N.2 QGraphicsView的用户界面只能缩小一个QGraphicsView。我使用QWheelEvent+CTRL进行一个QGraphicsView和QWheelEvent+第二个QGraphicsView的ALT

我阅读了一些非常有用的文章来开发这个程序,比如这个和这但所有这些都是为了仅放大一个QGraphicsView。所以很抱歉再次遇到这个问题,但我一直在努力在过去的几天里,我尝试为两个不同的人做这件事Q图形视图。。

我包含了一段代码(我主要使用的是邮递)进行缩放。请让我知道我做错了什么。

主窗口.h

private:
Qt::KeyboardModifiers _modifiers;
double _zoom_factor_base;
QPointF target_scene_pos, target_viewport_pos;
bool eventFilter(QObject* object, QEvent* event);
Qt::KeyboardModifiers _modifiersRight;
double _zoom_factor_base_right;
QPointF target_scene_pos_right, target_viewport_pos_right;
bool altDown = false;
signals:
void zoomed();
void zoomedRight();
protected:
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{    
ui->setupUi(this);
ui->leftView->viewport()->installEventFilter(this);
ui->leftView->setMouseTracking(true);
_modifiers = Qt::ControlModifier;
_zoom_factor_base = 1.0015;
ui->rightView->viewport()->installEventFilter(this);
ui->rightView->setMouseTracking(true);
_modifiersRight = Qt::ControlModifier;
_zoom_factor_base_right = 1.0015;
}
void MainWindow::gentle_zoom(double factor)
{
ui->leftView->scale(factor, factor);
ui->leftView->centerOn(target_scene_pos);
QPointF delta_viewport_pos = target_viewport_pos - QPointF(ui->leftView->viewport()->width() / 2.0,
ui->leftView->viewport()->height() / 2.0);
QPointF viewport_center =
ui->leftView->mapFromScene(target_scene_pos) - delta_viewport_pos;
ui->leftView->centerOn(ui->leftView >mapToScene(viewport_center.toPoint()));
emit zoomed();
}
void MainWindow::set_modifiers(Qt::KeyboardModifiers modifiers)
{
_modifiers = modifiers;
}
void MainWindow::set_zoom_factor_base(double value)
{
_zoom_factor_base = value;
}
void MainWindow::gentle_zoom_right(double factor)
{
ui->rightView->scale(factor, factor);
ui->rightView->centerOn(target_scene_pos_right);
QPointF delta_viewport_pos = target_viewport_pos_right -
QPointF(ui->rightView->viewport()->width() / 2.0,
ui->rightView->viewport()->height() / 2.0);
QPointF viewport_center = ui->rightView->mapFromScene(target_scene_pos_right) - delta_viewport_pos;
ui->rightView->centerOn(ui->rightView >mapToScene(viewport_center.toPoint())); 
emit zoomedRight();
}
void MainWindow::set_modifiers_right(Qt::KeyboardModifiers modifiers)
{
_modifiersRight = modifiers;
}
void MainWindow::set_zoom_factor_base_right(double value)
{
_zoom_factor_base_right = value;
}
bool MainWindow::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 = ui->leftView->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_right, angle);
gentle_zoom(factor);
return true;
}
}
}
if(altDown) // I repeat the same procedure but for the rightView
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
QPointF delta = target_viewport_pos_right - mouse_event->pos();
if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5)
{
target_viewport_pos_right = mouse_event->pos();
target_scene_pos_right = ui->rightView->mapToScene(mouse_event->pos());
}
}
else if (event->type() == QEvent::Wheel)
{
QWheelEvent* wheel_event = static_cast<QWheelEvent*>(event);
if (QApplication::keyboardModifiers() == _modifiersRight)
{
if (wheel_event->orientation() == Qt::Vertical)
{
double angle = wheel_event->angleDelta().y();
double factor = qPow(_zoom_factor_base_right, angle);
gentle_zoom_right(factor);
return true;
}
}
}
}
Q_UNUSED(object)
return false;
}
void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Alt) { altDown = false; }
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Alt) { altDown = true; }
}

您必须了解"单一责任"原则,它表明每个类都有自己的责任,在这种情况下,它可以避免出现很多混乱。缩放的职责是QGraphicsView,而不是MainWindow。

为此,创建了一个继承自QGraphicsView的新类,并实现了缩放功能:

图形视图.h

#ifndef GRAPHICSVIEW_H
#define GRAPHICSVIEW_H
#include <QGraphicsView>
class GraphicsView : public QGraphicsView
{
public:
GraphicsView(QWidget *parent=nullptr);
void setModifiers(const Qt::KeyboardModifiers &modifiers);
protected:
void wheelEvent(QWheelEvent *event) override;
private:
void applyZoom(double factor, const QPoint &fixedViewPos);
Qt::KeyboardModifiers m_modifiers;
const double base = 1.0015;
};
#endif // GRAPHICSVIEW_H

graphicsview.cpp

#include "graphicsview.h"
#include <QWheelEvent>
#include <QtMath>
GraphicsView::GraphicsView(QWidget *parent):
QGraphicsView(parent)
{}
void GraphicsView::setModifiers(const Qt::KeyboardModifiers &modifiers)
{
m_modifiers = modifiers;
}
void GraphicsView::wheelEvent(QWheelEvent *event)
{
if(event->modifiers() == m_modifiers){
double angle = event->orientation() == Qt::Vertical ? event->angleDelta().y(): event->angleDelta().x();
double factor = qPow(base, angle);
applyZoom(factor, event->pos());
}
}
void GraphicsView::applyZoom(double factor, const QPoint & fixedViewPos)
{
QPointF fixedScenePos = mapToScene(fixedViewPos);
centerOn(fixedScenePos);
scale(factor, factor);
QPointF delta = mapToScene(fixedViewPos) - mapToScene(viewport()->rect().center());
centerOn(fixedScenePos - delta);
}

然后使用Qt Designer提供的小部件升级插入GUI。然后设置值:

ui->rightView->setModifiers(Qt::AltModifier);
ui->leftView->setModifiers(Qt::ControlModifier);

完整的例子可以在这里找到。


另一方面,SO中显示的许多代码示例只是为了显示功能,不一定用于项目的实现,因为这里的空间很小会被压缩,有时会失去可读性。例如,我基于你指出的答案的想法,但我已经正确地划分了它,以获得一个干净的代码。

相关内容

  • 没有找到相关文章

最新更新