[Interest] How to update QGraphicsView transform when scrolling view with wheel?

Patrick Stinson patrickkidd at gmail.com
Tue Jul 25 09:29:12 CEST 2017


    def viewportEvent(self, e):
        if e.type() == QEvent.TouchBegin or e.type() == QEvent.TouchUpdate or e.type() == QEvent.TouchEnd:
            points = e.touchPoints()
            if e.type() == QEvent.TouchBegin and not self.scene().dragItem:
                e.accept()
            if len(points) == 2 and e.type() == QEvent.TouchUpdate:
                e.accept()
                THRESHOLD = 10.0
                COEFF = 1.0
                orig = QLineF(points[0].startPos(), points[1].startPos())
                now = QLineF(points[0].pos(), points[1].pos())
                delta = abs(now.length() - orig.length())
                if self.zoomScaleStart is None and delta > THRESHOLD:
                    self.zoomScaleStart = self.scene().scaleFactor
                if self.zoomScaleStart:
                    if self.animTimer is None:
                        self.animTimer = self.startTimer(util.ANIM_TIMER_MS)
                    zoom = self.zoomScaleStart * (now.length() / orig.length()) * COEFF
                    self.zoomAbsolute(zoom)

                    # oldVValue = self.verticalScrollBar().value()                                                                
                    # vDiff = points[0].lastPos().y() - points[0].pos().y()                                                       
                    # newVValue = oldVValue + vDiff                                                                               
                    # self.verticalScrollBar().setValue(newVValue)                                                                

                    # oldHValue = self.horizontalScrollBar().value()                                                              
                    # hDiff = points[0].lastPos().x() - points[0].pos().x()                                                       
                    # newHValue = oldHValue + hDiffOA                                                                             
                    # self.horizontalScrollBar().setValue(newHValue)                                                              

            if e.type() == QEvent.TouchEnd or e.type() == QEvent.TouchCancel:
                self.zoomScaleStart = None
                if self.animTimer:
                    self.killTimer(self.animTimer)
                    self.animTimer = None
            return True
        return super().viewportEvent(e)

    def zoomAbsolute(self, x):
        if x < 0:
            x = .0000001
        if self.scene().scaleFactor != x:
            self.scene().setScaleFactor(x)
        self.scaleWaiting = True

    def _setScale(self, x):
        self.setTransform(QTransform.fromScale(x, x))

    def timerEvent(self, e):
        if self.scaleWaiting:
            x = self.scene().scaleFactor
            self._setScale(x)
            self.scaleWaiting = False
        if self.panWaiting:
            self.centerOn(self.panCenter)
            self.panWaiting = False


> On Jul 25, 2017, at 12:27 AM, Christian Gagneraud <chgans at gmail.com> wrote:
> 
> 
> 
> On 25/07/2017 7:24 pm, "Patrick Stinson" <patrickkidd at gmail.com <mailto:patrickkidd at gmail.com>> wrote:
> Hi there!
> 
> If I am setting a QGraphicsView's scale via setTransform() while scrolling up/down+left/right with two fingers on the trackpad, but the paint updates don’t come until after I stop moving my fingers which stops the wheel events. Is there a way to update the view with the new transform while scrolling
> 
> Can you show your event handlers, difficult to say without seeing except that it should work.
> 
> Chris
> 
> 
> 
> Thanks!
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org <mailto:Interest at qt-project.org>
> http://lists.qt-project.org/mailman/listinfo/interest <http://lists.qt-project.org/mailman/listinfo/interest>
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170725/9f8cfcbe/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1403 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170725/9f8cfcbe/attachment.bin>


More information about the Interest mailing list