Horizontal scrolling qgraphicsview always has a vertical delta

I have subclassed QGraphicsView for a custom canvas that will be used in a CAD application. I successfully redefined QGraphicsView :: wheelEvent to check the keyboard modifiers for the control key and, if I press the control key, increase it. I am trying to perform horizontal scrolling when the user holds shift and uses the wheel.

The problem is that horizontal scrolling also always scrolls at 0.279. Not a huge problem, but very annoying, and this indicates that something else is wrong.

So here are the questions:

  • Is it right to implement horizontal scrolling? If not, then what?
  • How to remove this delta 0.279?

Thanks in advance. Code and output sample below

void myView::zoom(int delta) { double factor = pow(1.2, delta/abs(delta)); this->scale(factor, factor); } void myView::scrollHorizontal(int level) { QPointF center = mapToScene(viewport()->rect().center()); qDebug() << "center: " << center.x() << ", " << center.y(); centerOn(QPointF(center.x() - level, center.y())); } void myView::wheelEvent(QWheelEvent *event) { //qDebug() << "delta: " << event->delta(); if (event->modifiers() & Qt::ControlModifier) { this->zoom(event->delta()); } else if (event->modifiers() & Qt::ShiftModifier) { this->scrollHorizontal(event->delta()); } else QGraphicsView::wheelEvent(event); } 

sample output from the qDebug () string in scrollHorizontal when on the left edge of the scene:

 center: 261.5 , 615.654 center: 261.5 , 615.375 center: 261.5 , 615.096 center: 261.5 , 614.817 center: 261.5 , 614.538 center: 261.5 , 614.259 center: 261.5 , 613.98 center: 261.5 , 613.701 center: 261.5 , 613.421 
+6
source share
1 answer

Your problem is that mapToScene() processing an integer, not a floating-point. The rounding error is always indicated the same for a specific viewport size, so you always add some delta.

Do you want to directly change the scrollbar property value or send the event to a horizontal scrollbar. I implemented the latter, it is quite simple.

On a Mac, you must completely disable the inertia of the scroll bar for your application. Otherwise, as soon as you release the modifier and pull your finger off the trackpad / scroll wheel, vertical scrolling will continue due to inertia. That would make Mac users suck, and they hate you for that :)

screenshot

gview-scroll.pro

 QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = gview-scroll TEMPLATE = app macx { LIBS += -framework Foundation OBJECTIVE_SOURCES += helper.m } SOURCES += main.cpp 

helper.m

 //helper.m #include <Foundation/NSUserDefaults.h> #include <Foundation/NSDictionary.h> #include <Foundation/NSString.h> void disableMomentumScroll(void) { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"NO" forKey:@"AppleMomentumScrollSupported"]; [defaults registerDefaults:appDefaults]; } 

main.cpp

 //main.cpp #include <QApplication> #include <QGraphicsScene> #include <QGraphicsView> #include <QtCore/qmath.h> #include <QScrollBar> #include <QWheelEvent> #include <QDebug> class View : public QGraphicsView { public: void zoom(int delta) { double factor = qPow(1.2, delta/qAbs(delta)); scale(factor, factor); } void wheelEvent(QWheelEvent *event) { if (event->modifiers() & Qt::ControlModifier) { zoom(event->delta()); } else if (event->modifiers() & Qt::ShiftModifier) { horizontalScrollBar()->event(event); } else { QGraphicsView::wheelEvent(event); } } public: explicit View(QWidget *parent=0) : QGraphicsView(parent) {} explicit View(QGraphicsScene *scene, QWidget *parent=0) : QGraphicsView(scene, parent) {} }; #ifndef Q_OS_MAC void disableMomentumScroll() {} #else extern "C" { void disableMomentumScroll(); } #endif int main(int argc, char *argv[]) { QApplication a(argc, argv); disableMomentumScroll(); QGraphicsScene s; s.addEllipse(-50, -50, 100, 100, QPen(Qt::red), QBrush(Qt::gray)); View w(&s); w.show(); return a.exec(); } 
+5
source

Source: https://habr.com/ru/post/944683/


All Articles