Problem with window-viewport coordinate
Hi. I have a problem with window-viewport coordinates. In my code I draw a polygon:
Code:
static const int upPoints_p[4][2] = {
{ 49, -49 }, { -49, -49 },
{ -10, -10 }, { 10, -10 }
};
painter.setWindow( -50, -50, 100, 100);
painter.drawPolygon(up_p);
and next, I would like to check mousePressEvent:
Code:
if( up_p.containsPoint(point, Qt::OddEvenFill) )
{
}
but I have different coordinate. Can someone help me fix my code?
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.
Re: Problem with window-viewport coordinate
Thanks for answer. So, I change my code:
Code:
{
painter.setWindow(-50, -50, 100, 100);
QPoint out
= painter.
transform().
inverted().
map(event
->pos
());
qDebug() << point.x() << point.y();
qDebug() << out.x() << out.y();
}
but I still get the same value, for example:
and I can't check this condition
Code:
up_p.containsPoint(out, Qt::OddEvenFill)
because I have polygons defiened like:
Code:
static const int upPoints_p[4][2] = {
{ 49, -49 }, { -49, -49 },
{ -10, -10 }, { 10, -10 }
};
So, what am I doing wrong?
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.
Re: Problem with window-viewport coordinate
Thanks for answer. I'm not sure I did this correctly, but this not working for me.
Code:
#include <QWidget>
{
Q_OBJECT
public:
explicit Test
(QWidget *parent
= 0);
protected:
private:
};
Code:
#include <QtGui>
#include "test.h"
{
painter.setWindow(-50, -50, 100, 100);
qDebug() << f.x() << " " << f.y();
QPoint point
= painter.
transform().
inverted().
map(f
);
qDebug() << point.x() << " " << point.y();
}
{
f = event->pos();
update();
}
Does anyone know what is wrong with my code?
Re: Problem with window-viewport coordinate
Use painter.deviceTransform().inverted().map(f);
Re: Problem with window-viewport coordinate
Yes, it works! Thanks a lot!