Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Scaling QPainter transform



  • I'm tring to edit the 40000 chip demo demo (for learning purposes), so the text on the chips doesn't scale when i zoom in/out. I have edited the code of chip.cpp like this:

     // Draw text
        if (lod >= 4) {
            QFont font("Times", 10);
            font.setStyleStrategy(QFont::ForceOutline);
    
            painter->setFont(font);
            painter->save();
            QTransform transform = painter->transform();
            transform.setMatrix(1,transform.m12(), transform.m13(), transform.m21(), 1, transform.m23(), transform.m31(), transform.m32(), transform.m33());
    
            painter->setWorldTransform(transform);
    
            painter->drawText(170, 180, QString("Model: VSC-2000 (Very Small Chip) at %1x%2").arg(x).arg(y));
            painter->drawText(170, 200, QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ"));
            painter->drawText(170, 220, QString("Manufacturer: Chip Manufacturer"));
            painter->restore();
        }
    }
    

    I've set the scale of the QPainter's transform to 1, however i'm getting weird results. Text appears to move away from the chips when i zoom out or rotate the view:
    chips.PNG
    What I am doing wrong?


  • Qt Champions 2017

    @LostBoy said in Scaling QPainter transform:

    What I am doing wrong?

    The diagonal elements are important both for the scale and the rotate transformation. Try using transform.scale() instead. Qt should be able to do the right thing. If the operations are not kept separate internally, however, i.e. the whole transform is kept as a single matrix, then the separate components can't be recreated back from the transformation matrix.


Log in to reply