Results 1 to 8 of 8

Thread: Best place to apply a QMatrix transform to paint() event in an app

  1. #1
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Best place to apply a QMatrix transform to paint() event in an app

    Hello all,

    I am working on a mathematics tutor app and so I need to transform my coordinate space into traditional Cartesian Coordinates.

    I have found a solution here: http://stackoverflow.com/questions/4...painter-y-axis

    which I have added to one of my QGraphicsItem object's paint() event handlers for testing but I would like to apply it at a more global scale. I looked at the widget I am using as the QGraphicsView as the place to set this however it doesn't have a paint(), just these:
    Qt Code:
    1. virtual void drawBackground(QPainter * painter, const QRectF & rect)
    2. virtual void drawForeground(QPainter * painter, const QRectF & rect)
    To copy to clipboard, switch view to plain text mode 

    If I wanted to plug the code into my application so that EVERYTHING is drawn automagically in the coordinate system I need it to be, where is the best place to put it?

    Code I have added to my QGraphicsItem:
    Qt Code:
    1. void PhysVector::paint(QPainter *pPainter, const QStyleOptionGraphicsItem *pOption, QWidget *) {
    2.  
    3. // Setting the matrix rotates the drawing area to be a normal cartesian plane. Unfortunately
    4. // it also means that the original drawing has to be tweaked
    5. QMatrix matrix;
    6. matrix.scale(1, -1);
    7. pPainter -> setMatrix(matrix);
    To copy to clipboard, switch view to plain text mode 

    One further note I would like to make, is I would like to affect ONLY the drawing of objects, not the interaction of mouse clicks and what not. Unless they are inseparable I mean...
    Thank you!
    - Caolan
    Last edited by Caolan O'Domhnaill; 2nd October 2015 at 20:50.

  2. #2
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Best place to apply a QMatrix transform to paint() event in an app

    Is something like QGraphicsView::setTransform what you're looking for?

  3. #3
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Best place to apply a QMatrix transform to paint() event in an app

    based ont he link you gave, it transforms to view coordinates which as I understand it only affect coordinates system with respect to World and local coordinates. It does not do any kind of flip of the y-axis like the QMatrix example I used above seems to do.

    I could be wrong, I am new to this, however that is my interpretation.

  4. #4
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Best place to apply a QMatrix transform to paint() event in an app

    QTransform is a type of matrix that supports perspective transformations (including affine).
    QMatrix only supports affine transformations (translation, rotation, shear and scale).

    You can create a QTransform and use QTransform::scale( 1.0, -1.0 ) (to flip the vertical axis) and then assign it to the graphics view using setTransform.

    Then consider your graphics scene as in cartesian coordinates: treat all coordinates as in a cartesian space. When displayed through the graphics view, the transform of the graphics view will convert from cartesian to device coordinates.

  5. The following user says thank you to Kryzon for this useful post:

    Caolan O'Domhnaill (5th October 2015)

  6. #5
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Best place to apply a QMatrix transform to paint() event in an app

    GREAT! Thank you so very much!

    -Caolan.

  7. #6
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Best place to apply a QMatrix transform to paint() event in an app

    Aaaaaaaand now everything in my view is upside down... *facepalm*

  8. #7
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Best place to apply a QMatrix transform to paint() event in an app

    Here's a suggestion: translate that transform to the centre of the viewport. It should look more like a cartesian representation.
    Qt Code:
    1. QPoint viewportCentre = viewport().rect().center();
    2. QTransform::translate( viewportCentre.x(), viewportCentre.y() );
    To copy to clipboard, switch view to plain text mode 

    Make sure you are specifying coordinates in "cartesian" space: +X is to the right, +Y is up.

  9. The following user says thank you to Kryzon for this useful post:

    Caolan O'Domhnaill (13th October 2015)

  10. #8
    Join Date
    Sep 2015
    Posts
    50
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Best place to apply a QMatrix transform to paint() event in an app

    Quote Originally Posted by Kryzon View Post
    Here's a suggestion: translate that transform to the centre of the viewport. It should look more like a cartesian representation.
    Qt Code:
    1. QPoint viewportCentre = viewport().rect().center();
    2. QTransform::translate( viewportCentre.x(), viewportCentre.y() );
    To copy to clipboard, switch view to plain text mode 

    Make sure you are specifying coordinates in "cartesian" space: +X is to the right, +Y is up.


    I got it all to work. Thank you very much!

Similar Threads

  1. Timer event & paint event, priority
    By Teuniz in forum Qt Programming
    Replies: 0
    Last Post: 2nd February 2010, 13:33
  2. Paint event function in key press event
    By soumya in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2010, 12:40
  3. Replies: 0
    Last Post: 3rd March 2009, 15:38
  4. Where to apply transform? iterm or QPainter?
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2009, 08:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.