Results 1 to 7 of 7

Thread: Problem with window-viewport coordinate

  1. #1
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with window-viewport coordinate

    Hi. I have a problem with window-viewport coordinates. In my code I draw a polygon:

    Qt Code:
    1. static const int upPoints_p[4][2] = {
    2. { 49, -49 }, { -49, -49 },
    3. { -10, -10 }, { 10, -10 }
    4. };
    5.  
    6. QPolygon up_p(4, &upPoints_p[0][0]);
    7. painter.setWindow( -50, -50, 100, 100);
    8. painter.drawPolygon(up_p);
    To copy to clipboard, switch view to plain text mode 


    and next, I would like to check mousePressEvent:

    Qt Code:
    1. QPoint point = event->pos();
    2. if( up_p.containsPoint(point, Qt::OddEvenFill) )
    3. {
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    but I have different coordinate. Can someone help me fix my code?

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with window-viewport coordinate

    The event->pos() gives you co-ordinates in the widget world co-ordinates. You perhaps want co-ordinates of mouse click in way you have set the window.
    After having done setWindow() call painter.transform().inverted().map(event->pos()) in paint function. This will give you the co-ordinates in your world.

  3. #3
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with window-viewport coordinate

    Thanks for answer. So, I change my code:

    Qt Code:
    1. void Diagram::mousePressEvent(QMouseEvent *event)
    2. {
    3. QPoint point = event->pos();
    4.  
    5. QPainter painter(&pixmap);
    6. painter.setWindow(-50, -50, 100, 100);
    7.  
    8. QPoint out = painter.transform().inverted().map(event->pos());
    9.  
    10. qDebug() << point.x() << point.y();
    11. qDebug() << out.x() << out.y();
    12. }
    To copy to clipboard, switch view to plain text mode 

    but I still get the same value, for example:

    Qt Code:
    1. 188 158
    2. 188 158
    To copy to clipboard, switch view to plain text mode 

    and I can't check this condition

    Qt Code:
    1. up_p.containsPoint(out, Qt::OddEvenFill)
    To copy to clipboard, switch view to plain text mode 

    because I have polygons defiened like:

    Qt Code:
    1. static const int upPoints_p[4][2] = {
    2. { 49, -49 }, { -49, -49 },
    3. { -10, -10 }, { 10, -10 }
    4. };
    To copy to clipboard, switch view to plain text mode 

    So, what am I doing wrong?

  4. #4
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with window-viewport coordinate

    Hmm.. you are trying to use QPainter for which device in mousePressevent function handler? You must have also got some message like painter not active. It is because when you are trying to access qpainter in mousepressevent, painter doesn't scale in there. hence worldTransform matrix is identity. If you store the point in some variable in mousepressevent function handler, update the widget, and print coords in paint function you will get the right answer.
    If you want to store the painter use a pointer to qpainter and use that object.


    Added after 12 minutes:


    You can also store worldtransformationmatrix given by worldTransform() and use it map back to logical coordinates in mousepressevent.
    Last edited by pkj; 14th September 2011 at 16:38.

  5. #5
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with window-viewport coordinate

    Thanks for answer. I'm not sure I did this correctly, but this not working for me.

    Qt Code:
    1. #include <QWidget>
    2.  
    3. class Test : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit Test(QWidget *parent = 0);
    8.  
    9. protected:
    10. void paintEvent(QPaintEvent *);
    11. void mousePressEvent(QMouseEvent *);
    12.  
    13. private:
    14. QPoint f;
    15.  
    16. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2. #include "test.h"
    3.  
    4. Test::Test(QWidget *parent) :
    5. QWidget(parent){ }
    6.  
    7. void Test::paintEvent(QPaintEvent *)
    8. {
    9. QPainter painter(this);
    10. painter.setWindow(-50, -50, 100, 100);
    11.  
    12. qDebug() << f.x() << " " << f.y();
    13.  
    14. QPoint point = painter.transform().inverted().map(f);
    15. qDebug() << point.x() << " " << point.y();
    16.  
    17. }
    18.  
    19. void Test::mousePressEvent(QMouseEvent *event)
    20. {
    21. f = event->pos();
    22. update();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Does anyone know what is wrong with my code?

  6. #6
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with window-viewport coordinate

    Use painter.deviceTransform().inverted().map(f);

  7. #7
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with window-viewport coordinate

    Yes, it works! Thanks a lot!

Similar Threads

  1. QGraphicsView Framework coordinate mapping problem
    By zgulser in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2009, 12:30
  2. Replies: 1
    Last Post: 27th August 2009, 23:06
  3. coordinate problem
    By sai in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2009, 10:21
  4. Replies: 2
    Last Post: 26th February 2009, 10:12
  5. Brushes and window/viewport transformations
    By blukske in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2006, 13:59

Tags for this Thread

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.