我正在构建具有N.2 QGraphicsView的用户界面,
只能在一个QGraphicsView上放大。
我正在将QWheelEvent + CTRL
用于一个QGraphicsView和QWheelEvent
+ 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; }
}
答案 0 :(得分:0)
您必须了解Single responsibility principle,它表明每个班级都有自己的责任,在这种情况下,它将避免造成很多混乱。缩放的责任在于QGraphicsView,而不是MainWindow之一。
为此,将创建一个继承自QGraphicsView的新类,并实现缩放功能:
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中显示的许多代码示例仅用于显示功能,而不一定用于项目的实现,因为此处压缩的空间很少,有时会失去可读性。例如,我基于您所指出的答案的想法,但我已经对其进行了正确划分,以得到清晰的代码。