Congratulations to our 2022 Qt Champions!

Help understanding pinch/push code

  • I need some help understanding how this pinch/push code works. The logic is backwards in that a pinch is zooming in and a push is zooming out. It would seem to me that I can fix this by flipping x and y in the pinchAreaStarted() function but that is just a hunch. What's happening in the actual code is escaping me, here is the code:

    bool MapItem::pinchAreaStarted(QPointF, QPointF startCenter, QPointF, qreal scale, qreal, qreal angle, qreal, qreal, QPointF, QPointF, QPointF, QPointF, int, bool accepted)
        int x = (int)startCenter.x();
        int y = (int)(height() - startCenter.y());
        _.ui.pinch        = true;  _.ui.pinchStart        = { x, y };        = false; _.ui.pressStart        = { INT_MAX, INT_MAX };
        _.ui.pressAndHold = false; _.ui.pressAndHoldStart = { INT_MAX, INT_MAX };
        _.ui.dragging     = false;    
        OnZoom(_.ui.pinchStart, scale, angle, true);
        return accepted;
    void MapItem::OnZoom(QPoint, qreal scale, qreal, bool first)
        if (first) { Q_ASSERT(events.empty()); _.zoomStart = _.CurrentZoomLevel(); }
    #if 0
        qreal factor = scale < 1 ? 2 : 0.5;
        qreal zoom = _.zoomStart + (scale /*- 1.0*/) * factor;
        qreal factor = scale < 1 ? 1 : .1;
        qreal zoom = _.zoomStart + (scale < 1 ? scale - 1 : scale) * factor;
    void SetZoomLevel(qreal zoom) {
            pThis->events.enqueue({MapItem::Z, {zoom, NAN}});

    I do not understand the significance of the ( ._ )'s is that simply how you address QPoint variables?

  • Lifetime Qt Champion

    That looks somewhat odd ?
    If you place the cursor on the _ and press F2 what is it declared as ?

  • @mrjj Oh right.. okay so here's the declaration apparently it was called _ , not sure how I feel about that:

     // pimpl
        struct Data; Data& _;

  • Lifetime Qt Champion

    While valid, it seems like a odd name for it, normally the pimpl is named d
    However in this case it seems the name _ was used.

    Anyway, if you read it as
    it reads better

    It seems like a pimpl impl. but im not 100% sure its used as such.

  • @mrjj Agreed, either way I am not sure how these methods work exactly. I am just going to assume that switching:

    _.ui.pinchStart  = {x, y} 


    _.ui.pinchStart  = {y, x} 

    will reverse the logic for me. If that doesn't do it than I am not sure what will. Sort of sucks the hardware is being used. Usually, when I want to learn something new about c++, I can just test things until I get a better idea of what's happening.

  • Lifetime Qt Champion

    Yes that will contain no funky side effects.
    It simply say

    MyDataRef.ui.pinchStart = {y, x}

    so go ahead and check if it fixes your actual issue.

  • @mrjj Unfortunately that didn't work. I was able to invert the zoom for the scroll wheel. The only diffrence between the zoom for the scroll wheel and the one for pinch/push is:

    Used by pinch/push

        void SetZoomLevel(qreal zoom) {
            pThis->events.enqueue({MapItem::Z, {-zoom, NAN}});

    Used by scroll wheel

        void Zoom(bool in) {
            pThis->events.enqueue({MapItem::Z, { (in?1:-1) * std::numeric_limits<qreal>::max(), NAN}});

    I find it odd that both are using the same enqueue function but are passing seemingly different arguments, here is that function:

    inline void enqueue(const T &t) { QList<T>::append(t); }

    All I did to invert the scroll wheel functionality was to change:




    so I guess I will try sending the enqueue -zoom instead of zoom. I am really just grasping at straws here, perhaps this will work.

Log in to reply