在同一个 UI 中使用 N.2 QGraphicsView 时 QWheelEvent 不工作
QWheelEvent not working when using N.2 QGraphicsView in the same UI
我正在构建一个具有 N.2 QGraphicsView 的用户界面,我正在
只能在一个 QGraphicsView 上放大缩小。
我正在为一个 QGraphicsView 和 QWheelEvent 使用 QWheelEvent + CTRL
+ ALT
第二个 QGraphicsView。
我阅读了非常有用的文章来开发这个程序,例如
this 和
this
但它们都只用于放大一个 QGraphicsView。所以
很抱歉再次写这个问题,但我一直在努力
过去几天很多,因为我试图为两个不同的人做这件事
QGraphicsView..
我包含了一段代码(我主要使用了 this 中的代码
post
) 进行缩放。请让我知道我做错了什么。
mainwindow.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; }
}
你一定要了解Single responsibility principle,它表示每个class都有自己的职责,这样可以避免很多乱七八糟的事情。缩放是 QGraphicsView 的职责,不是 MainWindow 的职责之一。
为此,创建了一个继承自 QGraphicsView 的新 class,我们实现了缩放功能:
graphicsview.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 提供的小部件 promotion 将其插入到 GUI 中。然后设置值:
ui->rightView->setModifiers(Qt::AltModifier);
ui->leftView->setModifiers(Qt::ControlModifier);
可以找到完整的示例 here。
另一方面,SO 中显示的许多代码示例只是为了展示功能,并不一定用于项目的实施,因为这里 space 很少被压缩,有时会失去可读性.例如,我根据您指出的答案的想法,但我将其正确划分以获得干净的代码。
我正在构建一个具有 N.2 QGraphicsView 的用户界面,我正在
只能在一个 QGraphicsView 上放大缩小。
我正在为一个 QGraphicsView 和 QWheelEvent 使用 QWheelEvent + CTRL
+ ALT
第二个 QGraphicsView。
我阅读了非常有用的文章来开发这个程序,例如 this 和 this 但它们都只用于放大一个 QGraphicsView。所以 很抱歉再次写这个问题,但我一直在努力 过去几天很多,因为我试图为两个不同的人做这件事 QGraphicsView..
我包含了一段代码(我主要使用了 this 中的代码 post ) 进行缩放。请让我知道我做错了什么。
mainwindow.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; }
}
你一定要了解Single responsibility principle,它表示每个class都有自己的职责,这样可以避免很多乱七八糟的事情。缩放是 QGraphicsView 的职责,不是 MainWindow 的职责之一。
为此,创建了一个继承自 QGraphicsView 的新 class,我们实现了缩放功能:
graphicsview.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 提供的小部件 promotion 将其插入到 GUI 中。然后设置值:
ui->rightView->setModifiers(Qt::AltModifier);
ui->leftView->setModifiers(Qt::ControlModifier);
可以找到完整的示例 here。
另一方面,SO 中显示的许多代码示例只是为了展示功能,并不一定用于项目的实施,因为这里 space 很少被压缩,有时会失去可读性.例如,我根据您指出的答案的想法,但我将其正确划分以获得干净的代码。